1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 __author__ = "Olof Svensson"
34 __contact__ = "svensson@esrf.fr"
35 __license__ = "LGPLv3+"
36 __copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
37
38 import time, threading, sys
39
40
42 """
43 This file takes care of creating a log file and writes messages to the log file.
44 """
45
46 - def __init__(self, _pyStrLogFileName=None):
47 """
48 @param _pyStrLogFileName: Optional path to or name of the log file.
49 @type _pyStrLogFileName: python string
50 """
51 self.__pyStrLogFileName = _pyStrLogFileName
52 self.__pyFileLog = None
53 self.__pyListLogCache = []
54 self.__iMaxLogCache = 50
55 self.__bIsLogFileOn = True
56
57
59 """
60 This private method creates the log file. If the log file name or path is not set
61 a name of the type "EDNA_YYMMDD-HHMM.log" is used.
62 """
63 if (self.__bIsLogFileOn):
64 if (self.__pyStrLogFileName is None):
65 self.__pyStrLogFileName = "EDNA_%s.log" % time.strftime("%Y%m%d-%H%M%S", time.localtime(time.time()))
66 self.__pyFileLog = file(self.__pyStrLogFileName, "w")
67 for pyStrLogMessage in self.__pyListLogCache:
68 self.write(pyStrLogMessage)
69
70
71 - def write(self, _pyStrLogMessage):
72 """
73 This method writes a message to the log file. If the log file name is not set
74 messages are cached up to the limit __iMaxLogCache defined in the constructor.
75
76 Once a file name of the log file has been defined all cached log messages are
77 flushed to the log file.
78
79 This allow log messages to be written to the log file even if the log messages
80 are issued before the name of the log file has been determined by the EDNA
81 application.
82
83 @param _pyStrLogMessage: a log message
84 @type _pyStrLogMessage: python string
85 """
86 if (self.__bIsLogFileOn):
87 if (self.__pyFileLog is None):
88 self.__pyListLogCache.append(_pyStrLogMessage)
89 if (len(self.__pyListLogCache) > self.__iMaxLogCache):
90 self.__createLogFile()
91 else:
92 self.__pyFileLog.write(time.strftime("%Y%m%d-%H%M%S", time.localtime(time.time())) + _pyStrLogMessage)
93 self.__pyFileLog.flush()
94
95
97 """
98 @param _pyStrLogFilePath: The name or path of the log file
99 @type _pyStrLogMessage: python string
100 """
101 self.__pyStrLogFileName = _pyStrLogFilePath
102 self.__createLogFile()
103
104
106 """
107 This method turns off logging to a file.
108 """
109 self.__bIsLogFileOn = False
110