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
22 from . import printcolors
25 '''Class that handle log mechanism
27 def __init__(self, config, silent_sysstd=False):
30 :param config pyconf.Config: The global configuration.
31 :param silent_sysstd boolean: if True, do not write anything in terminal.
34 self.default_level = 3
35 self.silentSysStd = silent_sysstd
37 # Construct log file location. There are two cases. With an application an without any application.
38 logFileName = config.VARS.datehour + "_" + config.VARS.command + ".xml"
39 if 'APPLICATION' in config:
40 logFilePath = os.path.join(config.APPLICATION.out_dir, 'LOGS', logFileName)
42 logFilePath = os.path.join(config.VARS.personalDir, 'LOGS', logFileName)
43 src.ensure_path_exists(os.path.dirname(logFilePath))
45 self.logFileName = logFileName
46 self.logFilePath = logFilePath
47 # Open the file for writing
48 self.logFile = open(logFilePath, 'w')
51 def write(self, message, level=None, screenOnly=False):
52 '''the function used in the commands that will print in the terminal and the log file.
54 :param message str: The message to print.
55 :param level int: The output level corresponding to the message 0 < level < 6.
56 :param screenOnly boolean: if True, do not write in log file.
58 # do not write message starting with \r to log file
59 if self.logFile and not message.startswith("\r") and not screenOnly:
60 self.logFile.write(printcolors.cleancolor(message))
62 # get user or option output level
63 current_output_level = self.config.USER.output_level
64 if not ('isatty' in dir(sys.stdout) and sys.stdout.isatty()):
65 # clean the message color if the terminal is redirected by user
66 # ex: sat compile appli > log.txt
67 message = printcolors.cleancolor(message)
69 # Print message regarding the output level value
71 if level <= current_output_level and not self.silentSysStd:
72 sys.stdout.write(message)
74 if self.default_level <= current_output_level and not self.silentSysStd:
75 sys.stdout.write(message)
77 def error(self, message):
80 :param message str: The message to print.
82 # Print in the log file
84 self.logFile.write(_('ERROR:') + message)
86 # Print in the terminal and clean colors if the terminal is redirected by user
87 if not ('isatty' in dir(sys.stderr) and sys.stderr.isatty()):
88 sys.stderr.write(printcolors.printcError(_('ERROR:') + message))
90 sys.stderr.write(_('ERROR:') + message)
93 '''Flush terminal and file