]> SALOME platform Git repositories - tools/sat.git/blob - src/logger.py
Salome HOME
log file is a xml file
[tools/sat.git] / src / logger.py
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #  Copyright (C) 2010-2012  CEA/DEN
4 #
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.
9 #
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.
14 #
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
18
19 import sys
20 import os
21
22 import src
23 from . import printcolors
24 from . import xmlManager
25
26 class Logger(object):
27     '''Class that handle log mechanism
28     '''
29     def __init__(self, config, silent_sysstd=False):
30         '''Initialization
31         
32         :param config pyconf.Config: The global configuration.
33         :param silent_sysstd boolean: if True, do not write anything in terminal.
34         '''
35         self.config = config
36         self.default_level = 3
37         self.silentSysStd = silent_sysstd
38         
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)
43         else:
44             logFilePath = os.path.join(config.VARS.personalDir, 'LOGS', logFileName)
45         src.ensure_path_exists(os.path.dirname(logFilePath))
46         
47         self.logFileName = logFileName
48         self.logFilePath = logFilePath   
49         
50         self.xmlFile = xmlManager.xmlLogFile(logFilePath, config.VARS.command)
51         self.putInitialXMLFields()
52         
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="")
61
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.
64         
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.
68         '''
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))
72
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)
79         
80         # Print message regarding the output level value
81         if level:
82             if level <= current_output_level and not self.silentSysStd:
83                 sys.stdout.write(message)
84         else:
85             if self.default_level <= current_output_level and not self.silentSysStd:
86                 sys.stdout.write(message)
87
88     def error(self, message):
89         '''Print an error.
90         
91         :param message str: The message to print.
92         '''
93         # Print in the log file
94         self.xmlFile.append_node("traces", _('ERROR:') + message)
95
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))
99         else:
100             sys.stderr.write(_('ERROR:') + message)
101
102     def flush(self):
103         '''Flush terminal
104         '''
105         sys.stdout.flush()
106         
107     def endWrite(self):
108         self.xmlFile.write_tree()