Format the name in the log file always with the same length
authorJonatan Schlag <jonatan.schlag@ipfire.org>
Wed, 9 May 2018 11:32:32 +0000 (13:32 +0200)
committerJonatan Schlag <jonatan.schlag@ipfire.org>
Wed, 9 May 2018 11:32:32 +0000 (13:32 +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
src/nitsi/virtual_environ.py

index 8f90651..7990732 100644 (file)
@@ -38,7 +38,7 @@ def init_logging(path):
 
 
 class TestFormatter(logging.Formatter):
 
 
 class TestFormatter(logging.Formatter):
-    def __init__(self, start_time=None, name=None):
+    def __init__(self, start_time=None, name=None, longest_machine_name=10):
         super().__init__(fmt="[%(asctime)s] %(message)s")
         logger.debug("Initiating TestFormatter for")
         if start_time == None:
         super().__init__(fmt="[%(asctime)s] %(message)s")
         logger.debug("Initiating TestFormatter for")
         if start_time == None:
@@ -51,6 +51,8 @@ class TestFormatter(logging.Formatter):
         else:
             self.name = name
 
         else:
             self.name = name
 
+        self.longest_machine_name = longest_machine_name
+
     def converter(self, recordtime):
 
         # This returns a timestamp relatively to the time when we started
     def converter(self, recordtime):
 
         # This returns a timestamp relatively to the time when we started
@@ -61,7 +63,10 @@ class TestFormatter(logging.Formatter):
         return time.gmtime(recordtime)
 
     def format(self, record):
         return time.gmtime(recordtime)
 
     def format(self, record):
-        return "[{}][{}] {}".format(self.formatTime(record), self.name, record.getMessage())
+        return "[{}][{:^{align}}] {}".format(self.formatTime(record),
+                                                self.name,
+                                                record.getMessage(),
+                                                align=self.longest_machine_name)
 
     def formatTime(self, record, datefmt=None):
         ct = self.converter(record.created)
 
     def formatTime(self, record, datefmt=None):
         ct = self.converter(record.created)
index 230e855..795a0b8 100644 (file)
@@ -117,13 +117,14 @@ class machine():
 
         #serial_con.close()
 
 
         #serial_con.close()
 
-    def login(self, log_file, log_start_time=None):
+    def login(self, log_file, log_start_time=None, longest_machine_name=10):
         try:
             self.serial_con = serial_connection(self.get_serial_device(),
                                 username=self.username,
                                 log_file=log_file,
                                 log_start_time=log_start_time,
         try:
             self.serial_con = serial_connection(self.get_serial_device(),
                                 username=self.username,
                                 log_file=log_file,
                                 log_start_time=log_start_time,
-                                name=self.name)
+                                name=self.name,
+                                longest_machine_name=longest_machine_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 5e60be3..f9c04fe 100644 (file)
@@ -13,7 +13,7 @@ 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, log_file=None, name=None, log_start_time=None):
+    def __init__(self, device, username=None, log_file=None, name=None, log_start_time=None, longest_machine_name=10):
         self.buffer = b""
         self.back_at_prompt_pattern =  None
         self.username = username
         self.buffer = b""
         self.back_at_prompt_pattern =  None
         self.username = username
@@ -27,7 +27,9 @@ class serial_connection():
         log_file_handler = logging.FileHandler(self.log_file)
         log_file_handler.setLevel(logging.INFO)
         log_file_handler.terminator = ""
         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=log_start_time)
+        formatter = TestFormatter(name=self.name,
+                                    start_time=log_start_time,
+                                    longest_machine_name=longest_machine_name)
         log_file_handler.setFormatter(formatter)
         self.log_output.addHandler(log_file_handler)
 
         log_file_handler.setFormatter(formatter)
         self.log_output.addHandler(log_file_handler)
 
index 413518e..ecbe5f0 100755 (executable)
@@ -78,10 +78,15 @@ class test():
         # Time to which all serial output log entries are relativ
         log_start_time = time.time()
 
         # Time to which all serial output log entries are relativ
         log_start_time = time.time()
 
+        # Number of chars of the longest machine name
+        longest_machine_name = self.virtual_environ.longest_machine_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.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("{}/test.log".format(self.log_path), log_start_time)
+            self.virtual_machines[name].login("{}/test.log".format(self.log_path),
+                                                log_start_time=log_start_time,
+                                                longest_machine_name=longest_machine_name)
 
     def load_recipe(self):
         self.log.info("Going to load the recipe")
 
     def load_recipe(self):
         self.log.info("Going to load the recipe")
index 586d433..3dc86fd 100644 (file)
@@ -45,6 +45,9 @@ class virtual_environ():
         self.log.debug(self.machines)
         self.log.debug(self.networks)
 
         self.log.debug(self.machines)
         self.log.debug(self.networks)
 
+        # Number of characters of the longest machine name
+        self._longest_machine_name = 0
+
         self.uri = self.config["DEFAULT"]["uri"]
 
         try:
         self.uri = self.config["DEFAULT"]["uri"]
 
         try:
@@ -82,4 +85,15 @@ class virtual_environ():
 
     @property
     def network_names(self):
 
     @property
     def network_names(self):
-        return self.networks
\ No newline at end of file
+        return self.networks
+
+    @property
+    def longest_machine_name(self):
+        if self._longest_machine_name:
+            return self._longest_machine_name
+        else:
+            for _machine in self.machines:
+                if len(_machine) > self._longest_machine_name:
+                    self._longest_machine_name = len(_machine)
+
+            return self._longest_machine_name
\ No newline at end of file