Module EDLogFile
[hide private]
[frames] | no frames]

Source Code for Module EDLogFile

  1  # 
  2  #    Project: The EDNA Kernel 
  3  #             http://www.edna-site.org 
  4  # 
  5  #    File: "$Id$" 
  6  # 
  7  #    Copyright (C) 2008-2009 European Synchrotron Radiation Facility 
  8  #                            Grenoble, France 
  9  # 
 10  #    Principal authors: Olof Svensson (svensson@esrf.fr)  
 11  # 
 12  #    This program is free software: you can redistribute it and/or modify 
 13  #    it under the terms of the GNU Lesser General Public License as published 
 14  #    by the Free Software Foundation, either version 3 of the License, or 
 15  #    (at your option) any later version. 
 16  # 
 17  #    This program is distributed in the hope that it will be useful, 
 18  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 19  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 20  #    GNU Lesser General Public License for more details. 
 21  # 
 22  #    You should have received a copy of the GNU General Public License 
 23  #    and the GNU Lesser General Public License  along with this program.   
 24  #    If not, see <http://www.gnu.org/licenses/>. 
 25  # 
 26   
 27  # 
 28  # This class has been inspired by the corresponding AALib class  
 29  # (20090518-PyAALib-JyAALib-111) and modified according to the needs  
 30  # for the EDNA project. 
 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   
41 -class EDLogFile(object):
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
58 - def __createLogFile(self):
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
96 - def setLogFileName(self, _pyStrLogFilePath):
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
105 - def setLogFileOff(self):
106 """ 107 This method turns off logging to a file. 108 """ 109 self.__bIsLogFileOn = False
110