Add logging to test.log of all serial connection output
authorJonatan Schlag <jonatan.schlag@ipfire.org>
Wed, 9 May 2018 07:42:28 +0000 (09:42 +0200)
committerJonatan Schlag <jonatan.schlag@ipfire.org>
Wed, 9 May 2018 07:42:28 +0000 (09:42 +0200)
Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
src/nitsi/logger.py
src/nitsi/machine.py
src/nitsi/serial_connection.py
src/nitsi/test.py

index f492bda..8f90651 100644 (file)
@@ -35,3 +35,36 @@ def init_logging(path):
         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
index 0d790ae..d0170a0 100644 (file)
@@ -117,9 +117,9 @@ class machine():
 
         #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")
index f2f14bf..6328e4f 100644 (file)
@@ -8,17 +8,29 @@ from time import sleep
 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
@@ -80,6 +92,7 @@ class serial_connection():
 
     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
index f67ceb5..96ce841 100755 (executable)
@@ -24,6 +24,8 @@ class test():
 
         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))
@@ -74,7 +76,7 @@ class test():
         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")