3 # Copyright (C) 2010-2012 CEA/DEN
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 from . import printcolors
24 from . import xmlManager
27 '''Class that handle log mechanism
29 def __init__(self, config, silent_sysstd=False):
32 :param config pyconf.Config: The global configuration.
33 :param silent_sysstd boolean: if True, do not write anything in terminal.
36 self.default_level = 3
37 self.silentSysStd = silent_sysstd
39 # Construct log file location. There are two cases. With an application an without any application.
40 logFileName = config.VARS.datehour + "_" + config.VARS.command + ".xml"
41 if 'APPLICATION' in config:
42 logFilePath = os.path.join(config.APPLICATION.out_dir, 'LOGS', logFileName)
44 logFilePath = os.path.join(config.VARS.personalDir, 'LOGS', logFileName)
45 src.ensure_path_exists(os.path.dirname(logFilePath))
47 self.logFileName = logFileName
48 self.logFilePath = logFilePath
50 self.xmlFile = xmlManager.xmlLogFile(logFilePath, config.VARS.command)
51 self.putInitialXMLFields()
53 def putInitialXMLFields(self):
54 self.xmlFile.add_simple_node("field", text=self.config.VARS.command , attrib={"name" : "command"})
55 self.xmlFile.add_simple_node("field", text=self.config.INTERNAL.sat_version , attrib={"name" : "satversion"})
56 self.xmlFile.add_simple_node("field", text=self.config.VARS.hostname , attrib={"name" : "hostname"})
57 self.xmlFile.add_simple_node("field", text=self.config.VARS.dist , attrib={"name" : "OS"})
58 self.xmlFile.add_simple_node("field", text=self.config.VARS.user , attrib={"name" : "user"})
59 self.xmlFile.add_simple_node("field", text=self.config.VARS.datehour , attrib={"name" : "beginTime"})
60 self.xmlFile.add_simple_node("traces",text="")
62 def write(self, message, level=None, screenOnly=False):
63 '''the function used in the commands that will print in the terminal and the log file.
65 :param message str: The message to print.
66 :param level int: The output level corresponding to the message 0 < level < 6.
67 :param screenOnly boolean: if True, do not write in log file.
69 # do not write message starting with \r to log file
70 if not message.startswith("\r") and not screenOnly:
71 self.xmlFile.append_node("traces", printcolors.cleancolor(message))
73 # get user or option output level
74 current_output_level = self.config.USER.output_level
75 if not ('isatty' in dir(sys.stdout) and sys.stdout.isatty()):
76 # clean the message color if the terminal is redirected by user
77 # ex: sat compile appli > log.txt
78 message = printcolors.cleancolor(message)
80 # Print message regarding the output level value
82 if level <= current_output_level and not self.silentSysStd:
83 sys.stdout.write(message)
85 if self.default_level <= current_output_level and not self.silentSysStd:
86 sys.stdout.write(message)
88 def error(self, message):
91 :param message str: The message to print.
93 # Print in the log file
94 self.xmlFile.append_node("traces", _('ERROR:') + message)
96 # Print in the terminal and clean colors if the terminal is redirected by user
97 if not ('isatty' in dir(sys.stderr) and sys.stderr.isatty()):
98 sys.stderr.write(printcolors.printcError(_('ERROR:') + message))
100 sys.stderr.write(_('ERROR:') + message)
108 self.xmlFile.write_tree()