X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Flogger.py;h=eb2ee58b283a4b3e5e3a227e301ce1822f23f586;hb=f7deb9bb641eef1f8ad8930ec6b39ded6c9e9359;hp=834d6f0dce70e7a92e5ff09f2b8e0492867c90da;hpb=1201537c5ca6555ff8eb69438371c98b26384727;p=tools%2Fsat.git diff --git a/src/logger.py b/src/logger.py index 834d6f0..eb2ee58 100644 --- a/src/logger.py +++ b/src/logger.py @@ -27,12 +27,17 @@ import src from . import printcolors from . import xmlManager -logCommandFileExpression = "^[0-9]{8}_+[0-9]{6}_+.*\.xml$" +log_macro_command_file_expression = "^[0-9]{8}_+[0-9]{6}_+.*\.xml$" +log_all_command_file_expression = "^.*[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,20 +49,33 @@ 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)) + # The path of the log files (one for sat traces, and the other for + # the system commands traces) self.logFileName = logFileName self.logFilePath = logFilePath self.txtFileName = txtFileName self.txtFilePath = txtFilePath + + # The list of all log files corresponding to the current command and + # the commands called by the current command + self.l_logFiles = [logFilePath, txtFilePath] + # Initialize xml instance and put first fields # like beginTime, user, command, etc... self.xmlFile = xmlManager.XmlLogFile(logFilePath, "SATcommand", @@ -65,6 +83,8 @@ class Logger(object): self.put_initial_xml_fields() # Initialize the txt file for reading self.logTxtFile = open(str(self.txtFilePath), 'w') + # If the option all_in_terminal was called, all the system commands + # are redirected to the terminal if all_in_terminal: self.logTxtFile = sys.__stdout__ @@ -98,8 +118,34 @@ class Logger(object): attrib={"application" : self.config.VARS.application}) # The initialization of the trace node self.xmlFile.add_simple_node("Log",text="") + # The system commands logs self.xmlFile.add_simple_node("OutLog", text=os.path.join("OUT", self.txtFileName)) + # The initialization of the node where + # to put the links to the other sat commands that can be called by any + # 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 @@ -129,6 +175,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. @@ -171,7 +218,7 @@ class Logger(object): seconds = total_time - hours*3600 - minutes*60 # Add the fields corresponding to the end time # and the total time of command - endtime = dt.strftime('%d/%Y/%m %Hh%Mm%Ss') + endtime = dt.strftime('%Y/%m/%d %Hh%Mm%Ss') self.xmlFile.append_node_attrib("Site", attrib={"endTime" : endtime}) self.xmlFile.append_node_attrib("Site", attrib={"TotalTime" : "%ih%im%is" % (hours, minutes, seconds)}) @@ -183,7 +230,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 @@ -240,24 +287,31 @@ def show_command_log(logFilePath, cmd, application, notShownCommands): # When the command is not in notShownCommands, no need to go further : # Do not show if cmd in notShownCommands: - return False, None + return False, None, 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, None + if 'application' in logFileXml.xmlroot.keys(): appliLog = logFileXml.xmlroot.get('application') + launched_cmd = logFileXml.xmlroot.find('Site').attrib['launchedCommand'] # if it corresponds, then the log has to be shown if appliLog == application: - return True, appliLog + return True, appliLog, launched_cmd elif application != 'None': - return False, appliLog + return False, appliLog, launched_cmd - return True, appliLog + return True, appliLog, launched_cmd if application == 'None': - return True, None + return True, None, None - return False, None + return False, None, None def list_log_file(dirPath, expression): '''Find all files corresponding to expression in dirPath @@ -273,17 +327,32 @@ def list_log_file(dirPath, expression): sExpr = expression oExpr = re.compile(sExpr) if oExpr.search(fileName): + file_name = fileName + if fileName.startswith("micro_"): + file_name = fileName[len("micro_"):] # get date and hour and format it - date_hour_cmd = fileName.split('_') - date_not_formated = date_hour_cmd[0] + date_hour_cmd_host = file_name.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] + date_not_formated[4:6], + date_not_formated[0:4]) + 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')] + hour_not_formated[2:4], + hour_not_formated[4:6]) + 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 = []): @@ -299,9 +368,9 @@ def update_hat_xml(logDir, application=None, notShownCommands = []): "LOGlist", {"application" : application}) # 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: - showLog, cmdAppli = show_command_log(filePath, cmd, + lLogFile = list_log_file(logDir, log_macro_command_file_expression) + for filePath, __, date, __, hour, cmd, __ in lLogFile: + showLog, cmdAppli, full_cmd = show_command_log(filePath, cmd, application, notShownCommands) #if cmd not in notShownCommands: if showLog: @@ -311,7 +380,8 @@ def update_hat_xml(logDir, application=None, notShownCommands = []): attrib = {"date" : date, "hour" : hour, "cmd" : cmd, - "application" : cmdAppli}) + "application" : cmdAppli, + "full_command" : full_cmd}) # Write the file on the hard drive xmlHat.write_tree('hat.xsl') \ No newline at end of file