os.mkdir(time_dir)
return time_dir
+
+
+class TestFormatter(logging.Formatter):
+ def __init__(self, start_time=None, name=None):
+ super().__init__(fmt="[%(asctime)s] %(message)s")
+ logger.debug("Initiating TestFormatter for")
+ if start_time == None:
+ self.starttime = time.time()
+ else:
+ self.starttime = start_time
+
+ if name == None:
+ self.name = ""
+ else:
+ self.name = name
+
+ def converter(self, recordtime):
+
+ # This returns a timestamp relatively to the time when we started
+ # the test.
+
+ recordtime -= self.starttime
+
+ return time.gmtime(recordtime)
+
+ def format(self, record):
+ return "[{}][{}] {}".format(self.formatTime(record), self.name, record.getMessage())
+
+ def formatTime(self, record, datefmt=None):
+ ct = self.converter(record.created)
+ t = time.strftime("%H:%M:%S", ct)
+ s = "{}.{:03d}".format(t, round(record.msecs,None))
+ return s
#serial_con.close()
- def login(self):
+ def login(self, log_file):
try:
- self.serial_con = serial_connection(self.get_serial_device(), username=self.username)
+ self.serial_con = serial_connection(self.get_serial_device(), username=self.username, log_file=log_file, name=self.name)
self.serial_con.login(self.password)
except BaseException as e:
self.log.error("Could not connect to the domain via serial console")
import sys
import logging
+from nitsi.logger import TestFormatter
+
logger = logging.getLogger("nitsi.serial")
class serial_connection():
- def __init__(self, device, username=None):
+ def __init__(self, device, username=None, log_file=None, name=None):
self.buffer = b""
self.back_at_prompt_pattern = None
self.username = username
- self.log = logger.getChild(os.path.basename(device))
+ self.name = name
+ self.log_file = log_file
+ self.log = logger.getChild(name)
self.log.setLevel(logging.INFO)
self.con = serial.Serial(device)
+ self.log_output = self.log.getChild("output")
+ log_file_handler = logging.FileHandler(self.log_file)
+ log_file_handler.setLevel(logging.INFO)
+ log_file_handler.terminator = ""
+ formatter = TestFormatter(name=self.name, start_time=None)
+ log_file_handler.setFormatter(formatter)
+ self.log_output.addHandler(log_file_handler)
+
def read(self, size=1):
if len(self.buffer) >= size:
# throw away first size bytes in buffer
def log_console_line(self, line):
self.log.debug("Get in function log_console_line()")
+ self.log_output.info(line)
sys.stdout.write(line)
@property
self.log.debug("Path of this test is: {}".format(self.path))
+ self.log_path = log_path
+
self.settings_file = "{}/settings".format(self.path)
if not os.path.isfile(self.settings_file):
self.log.error("No such file: {}".format(self.settings_file))
self.log.debug("Try to login on all machines")
for name in self.virtual_environ.machine_names:
self.log.debug("Try to login on {}".format(name))
- self.virtual_machines[name].login()
+ self.virtual_machines[name].login("{}/test.log".format(self.log_path))
def load_recipe(self):
self.log.info("Going to load the recipe")