]> SALOME platform Git repositories - tools/sat.git/blob - src/logger.py
Salome HOME
a30e2ab3ab8bf49bce25da73220cac99cd7e64cd
[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 import src
22 from . import printcolors
23
24 class Logger(object):
25     '''Class that handle log mechanism
26     '''
27     def __init__(self, config, silent_sysstd=False):
28         '''Initialization
29         
30         :param config pyconf.Config: The global configuration.
31         :param silent_sysstd boolean: if True, do not write anything in terminal.
32         '''
33         self.config = config
34         self.default_level = 3
35         self.silentSysStd = silent_sysstd
36         
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)
41         else:
42             logFilePath = os.path.join(config.VARS.personalDir, 'LOGS', logFileName)
43         src.ensure_path_exists(os.path.dirname(logFilePath))
44         
45         self.logFileName = logFileName
46         self.logFilePath = logFilePath
47         # Open the file for writing
48         self.logFile = open(logFilePath, 'w')
49         
50
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.
53         
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.
57         '''
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))
61
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)
68         
69         # Print message regarding the output level value
70         if level:
71             if level <= current_output_level and not self.silentSysStd:
72                 sys.stdout.write(message)
73         else:
74             if self.default_level <= current_output_level and not self.silentSysStd:
75                 sys.stdout.write(message)
76
77     def error(self, message):
78         '''Print an error.
79         
80         :param message str: The message to print.
81         '''
82         # Print in the log file
83         if self.logFile:
84             self.logFile.write(_('ERROR:') + message)
85
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))
89         else:
90             sys.stderr.write(_('ERROR:') + message)
91
92     def flush(self):
93         '''Flush terminal and file
94         '''
95         if self.logFile:
96             self.logFile.flush()
97         sys.stdout.flush()