def setupTestLogger(log_path, prefix=None, filename=None, log_severity="INFO", create_symlink=True): """Customizes the root logger for a test run. Args: log_path: Location of the report file. prefix: A prefix for each log line in terminal. filename: Name of the files. The default is the time the objects are requested. create_symlink: bool. determines whether to create the symlink or not. set to True as default. Returns: A string, abs path to the created log file. """ if filename is None: filename = getLogFileTimestamp() utils.create_dir(log_path) logger = _initiateTestLogger(log_path, prefix, filename, log_severity) if create_symlink and isSymlinkSupported(): createLatestLogAlias(log_path) return os.path.join(log_path, filename)
def _initiateTestLogger(log_path, prefix=None, filename=None, log_severity="INFO"): """Customizes the root logger for a test run. The logger object has a stream handler and a file handler. Both handler logs data according to the log severty level. Args: log_path: Location of the log file. prefix: A prefix for each log line in terminal. filename: Name of the log file. The default is the time the logger is requested. log_severity: string, set the log severity level, default is INFO. This value affects console stream log handler and the python runner part of TradeFed host_log. """ log = logging.getLogger() # Clean up any remaining handlers. killTestLogger(log) log.propagate = False log.setLevel(logging.DEBUG) # Log into stream terminal_format = log_line_format if prefix: terminal_format = "[{}] {}".format(prefix, log_line_format) c_formatter = logging.Formatter(terminal_format, log_line_time_format) ch = logging.StreamHandler(sys.stdout) ch.setFormatter(c_formatter) ch.setLevel(log_severity_map.get(log_severity, logging.INFO)) log.addHandler(ch) # Log everything to file f_formatter = logging.Formatter(log_line_format, log_line_time_format) # All the logs of this test class go into one directory if filename is None: filename = getLogFileTimestamp() utils.create_dir(log_path) default_log_levels = ('ERROR', 'INFO', 'DEBUG') for level in default_log_levels: idx = filename.rfind('.') if idx < 0: idx = len(filename) addLogFile(log_path=log_path, filename=filename[:idx] + '_' + level + filename[idx:], log_severity=level) log.log_path = log_path logging.log_path = log_path
def setupTestLogger(log_path, prefix=None, filename=None, log_severity="INFO"): """Customizes the root logger for a test run. Args: log_path: Location of the report file. prefix: A prefix for each log line in terminal. filename: Name of the files. The default is the time the objects are requested. """ if filename is None: filename = getLogFileTimestamp() utils.create_dir(log_path) logger = _initiateTestLogger(log_path, prefix, filename, log_severity) if isSymlinkSupported(): createLatestLogAlias(log_path)
def takeBugReport(self, test_name, begin_time): """Takes a bug report on the device and stores it in a file. Args: test_name: Name of the test case that triggered this bug report. begin_time: Logline format timestamp taken when the test started. """ br_path = os.path.join(self.log_path, "BugReports") utils.create_dir(br_path) base_name = ",%s,%s.txt" % (begin_time, self.serial) test_name_len = utils.MAX_FILENAME_LEN - len(base_name) out_name = test_name[:test_name_len] + base_name full_out_path = os.path.join(br_path, out_name.replace(' ', '\ ')) self.log.info("Taking bugreport for %s on %s", test_name, self.serial) self.adb.bugreport(" > %s" % full_out_path) self.log.info("Bugreport for %s taken at %s", test_name, full_out_path)
def _initiateTestLogger(log_path, prefix=None, filename=None, log_severity="INFO"): """Customizes the root logger for a test run. The logger object has a stream handler and a file handler. Both handler logs data according to the log severty level. Args: log_path: Location of the log file. prefix: A prefix for each log line in terminal. filename: Name of the log file. The default is the time the logger is requested. log_severity: string, set the log severity level, default is INFO. """ log = logging.getLogger() # Clean up any remaining handlers. killTestLogger(log) log.propagate = False log.setLevel(log_severity_map.get(log_severity, logging.INFO)) # Log info to stream terminal_format = log_line_format if prefix: terminal_format = "[{}] {}".format(prefix, log_line_format) c_formatter = logging.Formatter(terminal_format, log_line_time_format) ch = logging.StreamHandler(sys.stdout) ch.setFormatter(c_formatter) ch.setLevel(log_severity_map.get(log_severity, logging.INFO)) # Log everything to file f_formatter = logging.Formatter(log_line_format, log_line_time_format) # All the logs of this test class go into one directory if filename is None: filename = getLogFileTimestamp() utils.create_dir(log_path) fh = logging.FileHandler(os.path.join(log_path, 'test_run_details.txt')) fh.setFormatter(f_formatter) fh.setLevel(log_severity_map.get(log_severity, logging.INFO)) log.addHandler(ch) log.addHandler(fh) log.log_path = log_path logging.log_path = log_path
def startAdbLogcat(self): """Starts a standing adb logcat collection in separate subprocesses and save the logcat in a file. """ if self.isAdbLogcatOn: raise AndroidDeviceError(("Android device %s already has an adb " "logcat thread going on. Cannot start " "another one.") % self.serial) f_name = "adblog,%s,%s.txt" % (self.model, self.serial) utils.create_dir(self.log_path) logcat_file_path = os.path.join(self.log_path, f_name) try: extra_params = self.adb_logcat_param except AttributeError: extra_params = "-b all" cmd = "adb -s %s logcat -v threadtime %s >> %s" % ( self.serial, extra_params, logcat_file_path) self.adb_logcat_process = utils.start_standing_subprocess(cmd) self.adb_logcat_file_path = logcat_file_path