Ejemplo n.º 1
0
def calc_busload(single_test):
    messages = single_test.dbc.messages
    b11 = 47
    b29 = 65
    bitr = int(single_test.cnf['baudrate'])
    overhead = (b11 if single_test.cnf['id_size'] == 'Force 11 bits' else b29)
    auto_size = False
    if single_test.cnf['id_size'] == 'Auto':
        auto_size = True
    sum_message_load = 0.0
    message_count = 0.0
    bit_stuff = int(single_test.cnf['bit_stuffing']) / 100
    output_list = []
    output_not_used_list = []
    output_ignored = []
    message_time = 0

    for message in messages:
        message_time = 0
        message_load = 0
        if message.name not in single_test.erase_message:
            if message.cycle > 0:
                ml = message.size
                if auto_size:
                    overhead = (b11 if int(message.id) < 2100 else b29)
                message_time = ((ml * 8 + overhead) +
                                ((ml * 8 + overhead - 13) * bit_stuff)) / bitr
                message_load = (message_time / message.cycle)
                sum_message_load += message_load
                message_count += 1
                output_list.append(
                    MessageOut(message.name, message.id, message.size,
                               message.cycle, message_time, message_load))
                event_log(output_list[-1])
            else:
                output_not_used_list.append(
                    MessageOut(message.name, message.id, message.size,
                               message.cycle, message_time, message_load))
                event_log(output_not_used_list[-1])
        else:
            # eventLog("[-] message %s doesn't used in busload calc" % message.name)
            output_not_used_list.append(
                MessageOut(message.name, message.id, message.size,
                           message.cycle, message_time, message_load))
            event_log(output_not_used_list[-1])

    result = Statistic(sum_message_load * 100, message_count, len(messages))

    result_output = [output_list, output_not_used_list, result, output_ignored]
    event_log('---> busload: %0.6f' % sum_message_load, single_test)
    single_test.result = result_output
Ejemplo n.º 2
0
    def __init__(self, config, module):
        self.config = config
        self.module = module
        self.handlers = None
        self.__setup_log(module)
        self.user_logger_enabled = False
        self.user_log_bytes = 0
        self.header_params = None

        self.message_out = MessageOut(self.module, self.config)

        self.console_buffer = []
        self.console_buffer_lock = threading.Lock()

        ## flush stdout to avoid out of order logging
        sys.stdout.flush()
Ejemplo n.º 3
0
    def __init__(self, config, module):
        self.config = config
        self.module = module
        self.handlers = None
        self.log = None
        self.__setup_log(module)
        self.user_log_bytes = 0

        self.message_out = MessageOut(self.module, self.config)

        ## flush stdout to avoid out of order logging
        sys.stdout.flush()
Ejemplo n.º 4
0
class AppLogger(object):
    logtype = {
        'SYSTEM' : 10,
        'USER' : 20,
        'GLOBAL' : 30
    }

    loglevel = {
        'DEBUG': 10,
        'INFO': 20,
        'WARN': 30,
        'ERROR': 40,
        'CRITICAL': 50,
    }

    def __init__(self, config, module):
        self.config = config
        self.module = module
        self.handlers = None
        self.__setup_log(module)
        self.user_logger_enabled = False
        self.user_log_bytes = 0
        self.header_params = None

        self.message_out = MessageOut(self.module, self.config)

        self.console_buffer = []
        self.console_buffer_lock = threading.Lock()

        ## flush stdout to avoid out of order logging
        sys.stdout.flush()

    def init_user_logger(self, header_params):
        self.user_logger_enabled = True
        ## user params get published as-is in the message
        ## user params should be key-value pairs
        if type(header_params) is dict:
            self.header_params = header_params

    def debug(self, message, logtype=logtype['SYSTEM']):
        self.log.debug(message)
        if logtype == self.logtype['USER']:
            self.__publish_user_system_buffer(message, self.loglevel['DEBUG'])

        if logtype == self.logtype['SYSTEM']:
            self.__publish_system_buffer(message, self.loglevel['DEBUG'])

    def info(self, message, logtype=logtype['SYSTEM']):
        self.log.info(message)
        if logtype == self.logtype['USER']:
            self.__publish_user_system_buffer(message, self.loglevel['INFO'])

        if logtype == self.logtype['SYSTEM']:
            self.__publish_system_buffer(message, self.loglevel['INFO'])

    def warn(self, message, exc_info=None, logtype=logtype['SYSTEM']):
        self.log.warn(message, exc_info=exc_info)
        if logtype == self.logtype['USER']:
            self.__publish_user_system_buffer(message, self.loglevel['WARN'])

        if logtype == self.logtype['SYSTEM']:
            self.__publish_system_buffer(message, self.loglevel['WARN'])

    def error(self, message, exc_info=None, logtype=logtype['SYSTEM']):
        self.log.error(message, exc_info=exc_info)
        if logtype == self.logtype['USER']:
            self.__publish_user_system_buffer(message, self.loglevel['ERROR'])

        if logtype == self.logtype['SYSTEM']:
            self.__publish_system_buffer(message, self.loglevel['ERROR'])

    def critical(self, message, logtype=logtype['SYSTEM']):
        self.log.critical(message)
        if logtype == self.logtype['USER']:
            self.__publish_user_system_buffer(
                message, self.loglevel['CRITICAL'])

        if logtype == self.logtype['SYSTEM']:
            self.__publish_system_buffer(message, self.loglevel['CRITICAL'])

    def remove_handler(self, handler):
        self.log.removeHandler(handler)

    def __get_timestamp(self):
        return int(time.time() * 1000000)

    def __publish_system_buffer(self, message, level):
        if not self.config['SYSTEM_LOGGING_ENABLED']:
            ## DO NOT use self.log, it will cause recursion
            return

    def append_console_buffer(self, console_out):
        with self.console_buffer_lock:
            self.console_buffer.append(console_out)

        if len(self.console_buffer) > self.config['CONSOLE_BUFFER_LENGTH']:
            self.flush_console_buffer()

    def __publish_user_system_buffer(self, message, level):
        if not self.config['USER_SYSTEM_LOGGING_ENABLED']:
            return
        self.log.debug('Publishing logs: USER SYSTEM')
        system_output_line = {
            'consoleSequenceNumber' : self.__get_timestamp(),
            'output': message,
        }
        self.log.debug(system_output_line)
        user_system_message = {
            'headers' : {
                'type' : self.logtype['GLOBAL'],
                'level' : level,
                'step' : self.config['STEP_NAME'],
                'messageDate': datetime.datetime.utcnow().isoformat(),
            },
            'updateSequenceNumber': self.__get_timestamp(),
            'consoleLogBytes': sys.getsizeof(message),
            'module': self.module,
            'timestamp': self.__get_timestamp(),
            'console': [system_output_line],
        }

        for header_param in self.header_params:
            user_system_message['headers'][header_param] = \
                self.header_params[header_param]

    def flush_console_buffer(self):
        self.log.info('Flushing console buffer')
        if len(self.console_buffer) == 0:
            self.log.debug('No console output to flush')
        else:
            with self.console_buffer_lock:
                self.log.debug('Flushing {0} console logs'.format(
                    len(self.console_buffer)))

                console_message = {
                    'headers' : {
                        'step' : self.config['STEP_NAME'],
                        'timestamp': self.__get_timestamp(),
                    },
                    'updateSequenceNumber': self.__get_timestamp(),
                    'consoleLogBytes': self.user_log_bytes,
                    'console' : self.console_buffer,
                }

                for header_param in self.header_params:
                    console_message['headers'][header_param] = \
                        self.header_params[header_param]
                console_message['headers']['consoleType'] = 'build'

                self.message_out.console(console_message)

                del self.console_buffer
                self.console_buffer = []

    def __setup_log(self, module_name):
        module_name = os.path.basename(module_name)
        module_name = module_name.split('.')[0]
        print 'Setting up logging for module : {0} \n'.format(module_name)

        logging.basicConfig(level=logging.INFO)
        project_root = self.config['HOME']
        log_dir = (project_root + '/logs').replace('//', '/')
        if not os.path.exists(log_dir):
            os.makedirs(log_dir)
        logger_name = self.module
        log_file = self.__get_log_file_name(logger_name, 'log', log_dir)
        with open(log_file, 'a'):
            os.utime(log_file, None)

        log_module_name = '{0} - {1}'.format(logger_name, module_name)
        self.log = logging.getLogger(log_module_name)

        self.log.propagate = True
        self.log.setLevel(logging.DEBUG)
        #handler = logging.FileHandler(log_file)
        handler = logging.handlers.RotatingFileHandler(
            log_file, maxBytes=10000000, backupCount=2)
        handler.setLevel(logging.DEBUG)
        formatter = logging.Formatter(
            '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        handler.setFormatter(formatter)
        self.log.addHandler(handler)
        self.handlers = self.log.handlers
        self.log.info('Log Config Setup successful')

    def __get_log_file_name(self, name, ext, folder):
        new_name = '{0}/{1}.{2}'.format(folder, name, ext)
        return new_name

    def log_command_op(self, output):
        console_out = {
            'consoleId': str(uuid.uuid4()),
            'parentConsoleId': '',
            'type': 'msg',
            'message' : output,
            'msgTimestamp': self.__get_timestamp(),
            'completed' : True
        }
        self.console_buffer.append(console_out)

    def log_command_err(self, err):
        console_out = {
            'consoleId': str(uuid.uuid4()),
            'parentConsoleId': '',
            'type': 'msg',
            'message' : err,
            'msgTimestamp': self.__get_timestamp(),
            'completed' : False
        }
        self.console_buffer.append(console_out)