Esempio n. 1
0
class HistoryClient(Client):
    def __init__(self):
        self.__log = None
        self.__log_configured = 0
        Client.__init__(self)
    def configure(self,config):
        set_attribute(self,'minimum_size',100,config,int)
        set_attribute(self,'maximum_size',200,config,int)
        Client.configure(self,config)
    def configuration(self):
        config = Client.configuration(self)
        get_attribute(self,'minimum_size',config,str)
        get_attribute(self,'maximum_size',config,str)
        return config
    def start(self):
        self.__log_configured = 0
        self.__log = TrimmingLog(self.parent.name + '_alarm_history')
        self.register_event(NewAlarmsEvent,self._log_alarms)
        Client.start(self)
    def stop(self):
        self.unregister_event(NewAlarmsEvent)
        Client.stop(self)
        self.__log = None
        self.__log_configured = 0
    def _log_alarms(self,event):
        if self.__log is None:
            raise ENotStarted('History log is None')
        if not self.__log_configured:
            self.__log.configure(event.alarms()[0].field_names(),
                                 self.minimum_size,self.maximum_size)
            self.__log_configured = 1
        for alarm in event:
            self.__log.add_entry(alarm.as_list())
    def newest_alarm_entry(self):
        return self.__log.get_last_record()
    def newest_alarm_values(self):
        return self.__log._get_last_row()
    def newest_alarm(self):
        entry = self.newest_alarm_entry()
        if not entry:
            return None
        alarm = Alarm()
        alarm.from_dictionary(entry)
        return alarm
    def newest_n_alarm_entries(self,count):
        sequence = len(self.__log)
        return self.__log.get_range('_seq',sequence - count,sequence)
    def newest_n_alarm_values(self,count):
        sequence = len(self.__log)
        return self.__log.get_range_values('_seq',sequence - count,sequence)
    def newest_n_alarms(self,count):
        alarms = []
        configs = self.newest_n_alarm_entries(count)
        for config in configs:
            alarm = Alarm()
            alarm.from_dictionary(config)
            alarms.append(alarm)
        return alarms
    def alarm_field_names(self):
        return self.__log.get_column_names()
Esempio n. 2
0
def display_log():
    args = _parse_args()
    if args['help']:
        _display_help()
        return
    _display_header()
    log = TrimmingLog('msglog')
    start = 0
    try:
        if args['tail_lines'] is not None or args['follow']:
            tail_lines = 10
            if args['tail_lines'] != None:
                tail_lines = int(args['tail_lines'])
            assert tail_lines >= -1, (
                "tail_lines must be greater than or eaual to -1")
            if tail_lines == -1:
                start = int(log.get_first_record()['_seq'])
            else:
                start = int(log.get_last_record()['_seq']) - tail_lines + 1
        else:
            start = int(log.get_first_record()['_seq'])
    except AssertionError:
        raise # Reraise the original exception.
    except:
        pass
    end = args['head_lines']
    assert end is None or end >= 0, (
        "head_lines must be None or it must be greater than or eaual to 0")
    entries = ()
    try:
        if end is not None:
            end += start
            entries = log[start:end]
        else:
            end = start
            entries = log[start:]
            end += len(entries)
    except ENoData:
        entries = []
        pass
    for entry in entries:
        _display_message(entry, args['color'], args['match'])
    if args['follow']:
        # If the file doesn't exist, there doesn't seem to be an obvious
        # way to gracefully recover at this point.  Just inform the user
        # that the file doesn't exist, and bail.  While we are at it,
        # we can make sure that the file does exist so that the next
        # time we are run, we have half a chance.
        if not os.path.exists(log.filename):
            print 'Error: %s does not exist.  Please restart.' % log.filename
            try:
                fd = open(log.filename, 'a+')
                fd.close()
            except:
                pass
            return
        _initialize_stat(log.filename)
        restart = end
        while 1:
            try:
                entries = log[restart:]
                restart += len(entries)
                for entry in entries:
                    _display_message(entry, args['color'], args['match'])
            except ENoData:
                pass
            # Sleep until either the file has changed, or
            # a certain maximum amount of time has elasped.
            _sleep_until_file_changes_with_timeout(log.filename,
                                                   max_wait_time)