Salome HOME
jobs report, display error when jobs are missing somewhere in the week
[tools/sat.git] / src / logger.py
index a87a7719019f714b92458dfcc98e3f846029233d..9f85f0072944e473c1bbeefb1e503d84723b694e 100644 (file)
@@ -32,7 +32,11 @@ logCommandFileExpression = "^[0-9]{8}_+[0-9]{6}_+.*\.xml$"
 class Logger(object):
     '''Class to handle log mechanism.
     '''
-    def __init__(self, config, silent_sysstd=False, all_in_terminal=False):
+    def __init__(self,
+                 config,
+                 silent_sysstd=False,
+                 all_in_terminal=False,
+                 micro_command = False):
         '''Initialization
         
         :param config pyconf.Config: The global configuration.
@@ -44,12 +48,18 @@ class Logger(object):
         self.silentSysStd = silent_sysstd
         
         # Construct xml log file location for sat prints.
-        logFileName = config.VARS.datehour + "_" + config.VARS.command + ".xml"
-        logFilePath = os.path.join(config.SITE.log.log_dir, logFileName)
+        prefix = ""
+        if micro_command:
+            prefix = "micro_"
+        hour_command_host = (config.VARS.datehour + "_" + 
+                             config.VARS.command + "_" + 
+                             config.VARS.hostname)
+        logFileName = prefix + hour_command_host + ".xml"
+        logFilePath = os.path.join(config.USER.log_dir, logFileName)
         # Construct txt file location in order to log 
         # the external commands calls (cmake, make, git clone, etc...)
-        txtFileName = config.VARS.datehour + "_" + config.VARS.command + ".txt"
-        txtFilePath = os.path.join(config.SITE.log.log_dir, "OUT", txtFileName)
+        txtFileName = prefix + hour_command_host + ".txt"
+        txtFilePath = os.path.join(config.USER.log_dir, "OUT", txtFileName)
         
         src.ensure_path_exists(os.path.dirname(logFilePath))
         src.ensure_path_exists(os.path.dirname(txtFilePath))
@@ -115,6 +125,27 @@ class Logger(object):
         # command 
         self.xmlFile.add_simple_node("Links")
 
+    def add_link(self,
+                 log_file_name,
+                 command_name,
+                 command_res,
+                 full_launched_command):
+        '''Add a link to another log file.
+        
+        :param log_file_name str: The file name of the link.
+        :param command_name str: The name of the command linked.
+        :param command_res str: The result of the command linked. "0" or "1"
+        :parma full_launched_command str: The full lanch command 
+                                          ("sat command ...")
+        '''
+        xmlLinks = self.xmlFile.xmlroot.find("Links")
+        src.xmlManager.add_simple_node(xmlLinks,
+                                       "link", 
+                                       text = log_file_name,
+                                       attrib = {"command" : command_name,
+                                                 "passed" : command_res,
+                                           "launchedCommand" : full_launched_command})
+
     def write(self, message, level=None, screenOnly=False):
         '''the function used in the commands 
         that will print in the terminal and the log file.
@@ -143,6 +174,7 @@ class Logger(object):
         else:
             if self.default_level <= current_output_verbose_level and not self.silentSysStd:
                 sys.stdout.write(message)
+        self.flush()
 
     def error(self, message):
         '''Print an error.
@@ -197,7 +229,7 @@ class Logger(object):
         self.xmlFile.write_tree(stylesheet = "command.xsl")
         
         # Dump the config in a pyconf file in the log directory
-        logDir = self.config.SITE.log.log_dir
+        logDir = self.config.USER.log_dir
         dumpedPyconfFileName = (self.config.VARS.datehour 
                                 + "_" 
                                 + self.config.VARS.command 
@@ -257,7 +289,12 @@ def show_command_log(logFilePath, cmd, application, notShownCommands):
         return False, None
  
     # Get the application of the log file
-    logFileXml = src.xmlManager.ReadXmlFile(logFilePath)
+    try:
+        logFileXml = src.xmlManager.ReadXmlFile(logFilePath)
+    except Exception as e:
+        msg = _("WARNING: the log file %s cannot be read:" % logFilePath)
+        sys.stdout.write(printcolors.printcWarning("%s\n%s\n" % (msg, e)))
+        return False, None
 
     if 'application' in logFileXml.xmlroot.keys():
         appliLog = logFileXml.xmlroot.get('application')
@@ -289,18 +326,28 @@ def list_log_file(dirPath, expression):
         oExpr = re.compile(sExpr)
         if oExpr.search(fileName):
             # get date and hour and format it
-            date_hour_cmd = fileName.split('_')
-            date_not_formated = date_hour_cmd[0]
+            date_hour_cmd_host = fileName.split('_')
+            date_not_formated = date_hour_cmd_host[0]
             date = "%s/%s/%s" % (date_not_formated[6:8], 
                                  date_not_formated[4:6], 
                                  date_not_formated[0:4])
-            hour_not_formated = date_hour_cmd[1]
+            hour_not_formated = date_hour_cmd_host[1]
             hour = "%s:%s:%s" % (hour_not_formated[0:2], 
                                  hour_not_formated[2:4], 
                                  hour_not_formated[4:6])
-            cmd = date_hour_cmd[2][:-len('.xml')]
+            if len(date_hour_cmd_host) < 4:
+                cmd = date_hour_cmd_host[2][:-len('.xml')]
+                host = ""
+            else:
+                cmd = date_hour_cmd_host[2]
+                host = date_hour_cmd_host[3][:-len('.xml')]
             lRes.append((os.path.join(dirPath, fileName), 
-                         date_not_formated, date, hour_not_formated, hour, cmd))
+                         date_not_formated,
+                         date,
+                         hour_not_formated,
+                         hour,
+                         cmd,
+                         host))
     return lRes
 
 def update_hat_xml(logDir, application=None, notShownCommands = []):
@@ -317,7 +364,7 @@ def update_hat_xml(logDir, application=None, notShownCommands = []):
     # parse the log directory to find all the command logs, 
     # then add it to the xml file
     lLogFile = list_log_file(logDir, logCommandFileExpression)
-    for filePath, _, date, _, hour, cmd in lLogFile:
+    for filePath, __, date, __, hour, cmd, __ in lLogFile:
         showLog, cmdAppli = show_command_log(filePath, cmd,
                                               application, notShownCommands)
         #if cmd not in notShownCommands: