Changeset 355:872f6018c59e for src/mlx


Ignore:
Timestamp:
12/12/12 19:49:47 (12 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

The delayed change logger now checks whether the user returns to the last logged value (#147)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/mlx/checks.py

    r354 r355  
    337337        self._firstChange = None
    338338        self._lastChangeState = None
     339        self._lastLoggedValue = None
     340
     341        self._logState = lambda flight, logger, state, forced = False: \
     342            StateChangeLogger.logState(self, flight, logger, state,
     343                                       forced = forced)
     344        self.logState = lambda flight, logger, state, forced = False: \
     345            DelayedChangeMixin.logState(self, flight, logger, state,
     346                                        forced = forced)
    339347
    340348    def _changed(self, oldState, state):
     
    345353        newValue = self._getValue(state)
    346354        if self._isDifferent(self._oldValue, newValue):
    347             if self._firstChange is None:
    348                 self._firstChange = state.timestamp
    349             self._lastChangeState = state
     355            if self._lastLoggedValue is not None and \
     356               not self._isDifferent(self._lastLoggedValue, newValue):
     357                self._firstChange = None
     358            else:
     359                if self._firstChange is None:
     360                    self._firstChange = state.timestamp
     361                self._lastChangeState = state
    350362            self._oldValue = newValue
    351363
     
    370382        This default implementation checks for simple equality."""
    371383        return oldValue!=newValue
     384
     385    def logState(self, flight, logger, state, forced):
     386        """Log the given state.
     387
     388        The value belonging to that state is recorded in _lastLoggedValue.
     389
     390        It calls _logState to perform the real logging."""
     391        self._lastLoggedValue = self._getValue(state)
     392        self._logState(flight, logger, state, forced = forced)
    372393
    373394#---------------------------------------------------------------------------------------
     
    408429
    409430class ForceableLoggerMixin(object):
    410     """A mixin for loggers that can be forced to log a certain state.
    411 
    412     The last logged state is always maintained, and when checking for a change,
    413     that state is compared to the current one (which may actually be the same,
    414     if a forced logging was performed for that state).
    415 
    416     Children should implement the following functions:
    417     - _hasChanged(oldState, state): the real check for a change
    418     - _logState(flight, logger, state, forced): the real logging function
    419     """
    420     def __init__(self):
    421         """Construct the mixin."""
    422         self._lastLoggedState = None
    423 
     431    """A mixin for loggers that can be forced to log a certain state."""
    424432    def forceLog(self, flight, logger, state):
    425433        """Force logging the given state."""
    426434        self.logState(flight, logger, state, forced = True)
    427 
    428     def logState(self, flight, logger, state, forced = False):
    429         """Log the state.
    430 
    431         It calls _logState to perform the real logging, and saves the given
    432         state as the last logged one."""
    433         self._logState(flight, logger, state, forced)
    434         self._lastLoggedState = state
    435 
    436     def _changed(self, oldState, state):
    437         """Check if the state has changed.
    438 
    439         This function calls _hasChanged for the real check, and replaces
    440         oldState with the stored last logged state, if any."""
    441         if self._lastLoggedState is not None:
    442             oldState = self._lastLoggedState
    443         return self._hasChanged(oldState, state)
    444435
    445436#---------------------------------------------------------------------------------------
     
    469460    """Logger for NAV radios.
    470461
    471     It also logs the OBS frequency set."""
     462    It also logs the OBS radial set."""
    472463    @staticmethod
    473464    def getMessage(logName, frequency, obs):
     
    481472        StateChangeLogger.__init__(self, logInitial = True)
    482473        DelayedChangeMixin.__init__(self)
    483         ForceableLoggerMixin.__init__(self)
    484 
    485         self.logState = lambda flight, logger, state, forced = False: \
    486             ForceableLoggerMixin.logState(self, flight, logger, state,
    487                                           forced = forced)
     474
    488475        self._getLogTimestamp = \
    489476            lambda state, forced: \
    490477            DelayedChangeMixin._getLogTimestamp(self, state, forced)
    491         self._changed = lambda oldState, state: \
    492             ForceableLoggerMixin._changed(self, oldState, state)
    493         self._hasChanged = lambda oldState, state: \
    494             DelayedChangeMixin._changed(self, oldState, state)
    495         self._logState = lambda flight, logger, state, forced: \
    496              StateChangeLogger.logState(self, flight, logger, state,
    497                                         forced = forced)
    498478
    499479        self._attrName = attrName
     
    548528                                          "%s: %%s" % (logName,),
    549529                                          minDelay = 3.0, maxDelay = 10.0)
    550         ForceableLoggerMixin.__init__(self)
    551 
    552         self.logState = lambda flight, logger, state, forced = False: \
    553             ForceableLoggerMixin.logState(self, flight, logger, state,
    554                                           forced = forced)
    555         self._changed = lambda oldState, state: \
    556             ForceableLoggerMixin._changed(self, oldState, state)
    557         self._hasChanged = lambda oldState, state: \
    558             DelayedChangeMixin._changed(self, oldState, state)
    559         self._logState = lambda flight, logger, state, forced: \
    560              StateChangeLogger.logState(self, flight, logger, state, forced)
    561530
    562531#---------------------------------------------------------------------------------------
     
    725694        DelayedChangeMixin.__init__(self)
    726695        self._lastLoggedState = None
     696        self.logState = lambda flight, logger, state:\
     697            APLogger.logState(self, flight, logger, state)
    727698
    728699    def _getValue(self, state):
Note: See TracChangeset for help on using the changeset viewer.