]> git.ipfire.org Git - nitsi.git/commitdiff
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 f492bda92a59bb9573e53c7f0d9fbad26d74ab54..8f90651e6c8296e04ffe1ddd5ae1919aaba1595e 100644 (file)
@@ -35,3 +35,36 @@ def init_logging(path):
         os.mkdir(time_dir)
 
     return time_dir
         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 0d790ae0eab5be5c4f5ba059daee6aeab47dca0c..d0170a0cc3e7f2107da1c0921f5d951610b22aad 100644 (file)
@@ -117,9 +117,9 @@ class machine():
 
         #serial_con.close()
 
 
         #serial_con.close()
 
-    def login(self):
+    def login(self, log_file):
         try:
         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")
             self.serial_con.login(self.password)
         except BaseException as e:
             self.log.error("Could not connect to the domain via serial console")
index f2f14bf8a51a96cebde1e3393d96a8aecfb167b9..6328e4f143b2c22353bbc3717d56adb499a0728d 100644 (file)
@@ -8,17 +8,29 @@ from time import sleep
 import sys
 import logging
 
 import sys
 import logging
 
+from nitsi.logger import TestFormatter
+
 logger = logging.getLogger("nitsi.serial")
 
 class serial_connection():
 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.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.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 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()")
 
     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
         sys.stdout.write(line)
 
     @property
index f67ceb5e29b1d7da1cea9fc2437d13591a78dfac..96ce841083b01a965d8ecc2b3b9f79be4414b7df 100755 (executable)
@@ -24,6 +24,8 @@ class test():
 
         self.log.debug("Path of this test is: {}".format(self.path))
 
 
         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.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.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")
 
     def load_recipe(self):
         self.log.info("Going to load the recipe")