| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | |
|---|
| 3 | """ Panohead remote control. |
|---|
| 4 | |
|---|
| 5 | License |
|---|
| 6 | ======= |
|---|
| 7 | |
|---|
| 8 | - B{papywizard} (U{http://trac.gbiloba.org/papywizard}) is Copyright: |
|---|
| 9 | - (C) 2007-2009 Frédéric Mantegazza |
|---|
| 10 | |
|---|
| 11 | This software is governed by the B{CeCILL} license under French law and |
|---|
| 12 | abiding by the rules of distribution of free software. You can use, |
|---|
| 13 | modify and/or redistribute the software under the terms of the CeCILL |
|---|
| 14 | license as circulated by CEA, CNRS and INRIA at the following URL |
|---|
| 15 | U{http://www.cecill.info}. |
|---|
| 16 | |
|---|
| 17 | As a counterpart to the access to the source code and rights to copy, |
|---|
| 18 | modify and redistribute granted by the license, users are provided only |
|---|
| 19 | with a limited warranty and the software's author, the holder of the |
|---|
| 20 | economic rights, and the successive licensors have only limited |
|---|
| 21 | liability. |
|---|
| 22 | |
|---|
| 23 | In this respect, the user's attention is drawn to the risks associated |
|---|
| 24 | with loading, using, modifying and/or developing or reproducing the |
|---|
| 25 | software by the user in light of its specific status of free software, |
|---|
| 26 | that may mean that it is complicated to manipulate, and that also |
|---|
| 27 | therefore means that it is reserved for developers and experienced |
|---|
| 28 | professionals having in-depth computer knowledge. Users are therefore |
|---|
| 29 | encouraged to load and test the software's suitability as regards their |
|---|
| 30 | requirements in conditions enabling the security of their systems and/or |
|---|
| 31 | data to be ensured and, more generally, to use and operate it in the |
|---|
| 32 | same conditions as regards security. |
|---|
| 33 | |
|---|
| 34 | The fact that you are presently reading this means that you have had |
|---|
| 35 | knowledge of the CeCILL license and that you accept its terms. |
|---|
| 36 | |
|---|
| 37 | Module purpose |
|---|
| 38 | ============== |
|---|
| 39 | |
|---|
| 40 | Logging |
|---|
| 41 | |
|---|
| 42 | Implements |
|---|
| 43 | ========== |
|---|
| 44 | |
|---|
| 45 | - DefaultFormatter |
|---|
| 46 | - ColorFormatter |
|---|
| 47 | - Logger |
|---|
| 48 | |
|---|
| 49 | @author: Frédéric Mantegazza |
|---|
| 50 | @copyright: (C) 2007-2009 Frédéric Mantegazza |
|---|
| 51 | @license: CeCILL |
|---|
| 52 | """ |
|---|
| 53 | |
|---|
| 54 | __revision__ = "$Id$" |
|---|
| 55 | |
|---|
| 56 | import logging |
|---|
| 57 | import logging.handlers |
|---|
| 58 | import StringIO |
|---|
| 59 | import traceback |
|---|
| 60 | import os.path |
|---|
| 61 | |
|---|
| 62 | from PyQt4 import QtCore |
|---|
| 63 | |
|---|
| 64 | from papywizard.common import config |
|---|
| 65 | from papywizard.common.loggingFormatter import DefaultFormatter, ColorFormatter, \ |
|---|
| 66 | SpaceFormatter, SpaceColorFormatter |
|---|
| 67 | |
|---|
| 68 | logger = None |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | class LoggerObject(QtCore.QObject): |
|---|
| 72 | """ Logger object. |
|---|
| 73 | """ |
|---|
| 74 | def __init__(self, defaultStreamHandler, defaultFileHandler): |
|---|
| 75 | """ Init object. |
|---|
| 76 | """ |
|---|
| 77 | logging.TRACE = logging.DEBUG - 5 |
|---|
| 78 | logging.EXCEPTION = logging.ERROR + 5 |
|---|
| 79 | logging.raiseExceptions = 0 |
|---|
| 80 | logging.addLevelName(logging.TRACE, "TRACE") |
|---|
| 81 | logging.addLevelName(logging.EXCEPTION, "EXCEPTION") |
|---|
| 82 | |
|---|
| 83 | # Formatters |
|---|
| 84 | #defaultFormatter = DefaultFormatter(config.LOGGER_FORMAT) |
|---|
| 85 | spaceFormatter = SpaceFormatter(config.LOGGER_FORMAT) |
|---|
| 86 | #colorFormatter = ColorFormatter(config.LOGGER_FORMAT) |
|---|
| 87 | spaceColorFormatter = SpaceColorFormatter(config.LOGGER_FORMAT) |
|---|
| 88 | |
|---|
| 89 | # Logger |
|---|
| 90 | self.__logger = logging.getLogger('papywizard') |
|---|
| 91 | self.__logger.setLevel(logging.TRACE) |
|---|
| 92 | |
|---|
| 93 | # Handlers |
|---|
| 94 | if defaultStreamHandler: |
|---|
| 95 | stdoutStreamHandler = logging.StreamHandler() |
|---|
| 96 | #stdoutStreamHandler.setFormatter(colorFormatter) |
|---|
| 97 | stdoutStreamHandler.setFormatter(spaceColorFormatter) |
|---|
| 98 | self.__logger.addHandler(stdoutStreamHandler) |
|---|
| 99 | if defaultFileHandler: |
|---|
| 100 | loggerFilename = os.path.join(config.DATA_STORAGE_DIR, config.LOGGER_FILENAME) |
|---|
| 101 | fileHandler = logging.handlers.RotatingFileHandler(loggerFilename, 'w', |
|---|
| 102 | config.LOGGER_MAX_BYTES, |
|---|
| 103 | config.LOGGER_BACKUP_COUNT) |
|---|
| 104 | fileHandler.setFormatter(spaceFormatter) |
|---|
| 105 | self.__logger.addHandler(fileHandler) |
|---|
| 106 | |
|---|
| 107 | def addStreamHandler(self, stream, formatter=DefaultFormatter): |
|---|
| 108 | """ Add a new stream handler. |
|---|
| 109 | |
|---|
| 110 | Can be used to register a new GUI handler. |
|---|
| 111 | |
|---|
| 112 | @param stream: open stream where to write logs |
|---|
| 113 | @type: stream: ??? |
|---|
| 114 | |
|---|
| 115 | @param formatter: associated formatter |
|---|
| 116 | @type formatter: L{DefaultFormatter<common.loggingFormatter>} |
|---|
| 117 | """ |
|---|
| 118 | handler = logging.StreamHandler(stream) |
|---|
| 119 | handler.setFormatter(formatter(config.LOGGER_FORMAT)) |
|---|
| 120 | self.__logger.addHandler(handler) |
|---|
| 121 | |
|---|
| 122 | def setLevel(self, level): |
|---|
| 123 | """ Change logging level. |
|---|
| 124 | |
|---|
| 125 | @param level: new level, in ('trace', 'debug', 'info', 'warning', 'error', 'exception', 'critical') |
|---|
| 126 | @type level: str |
|---|
| 127 | """ |
|---|
| 128 | loggerLevels = ('trace', 'debug', 'info', 'warning', 'error', 'exception', 'critical') |
|---|
| 129 | if level not in loggerLevels: |
|---|
| 130 | raise ValueError("Logger level must be in %s" % loggerLevels) |
|---|
| 131 | levels = {'trace': logging.TRACE, |
|---|
| 132 | 'debug': logging.DEBUG, |
|---|
| 133 | 'info': logging.INFO, |
|---|
| 134 | 'warning': logging.WARNING, |
|---|
| 135 | 'error': logging.ERROR, |
|---|
| 136 | 'exception': logging.EXCEPTION, |
|---|
| 137 | 'critical': logging.CRITICAL} |
|---|
| 138 | self.__logger.setLevel(levels[level]) |
|---|
| 139 | |
|---|
| 140 | def trace(self, message): |
|---|
| 141 | """ Logs a message with level TRACE. |
|---|
| 142 | |
|---|
| 143 | @param message: message to log |
|---|
| 144 | @type message: string |
|---|
| 145 | """ |
|---|
| 146 | self.__logger.log(logging.TRACE, unicode(message)) |
|---|
| 147 | |
|---|
| 148 | def debug(self, message): |
|---|
| 149 | """ Logs a message with level DEBUG. |
|---|
| 150 | |
|---|
| 151 | @param message: message to log |
|---|
| 152 | @type message: string |
|---|
| 153 | """ |
|---|
| 154 | self.__logger.debug(unicode(message)) |
|---|
| 155 | |
|---|
| 156 | def info(self, message): |
|---|
| 157 | """ Logs a message with level INFO. |
|---|
| 158 | |
|---|
| 159 | @param message: message to log |
|---|
| 160 | @type message: string |
|---|
| 161 | """ |
|---|
| 162 | self.__logger.info(unicode(message)) |
|---|
| 163 | |
|---|
| 164 | def warning(self, message): |
|---|
| 165 | """ Logs a message with level WARNING. |
|---|
| 166 | |
|---|
| 167 | @param message: message to log |
|---|
| 168 | @type message: string |
|---|
| 169 | """ |
|---|
| 170 | self.__logger.warning(unicode(message)) |
|---|
| 171 | |
|---|
| 172 | def error(self, message): |
|---|
| 173 | """ Logs a message with level ERROR. |
|---|
| 174 | |
|---|
| 175 | @param message: message to log |
|---|
| 176 | @type message: string |
|---|
| 177 | """ |
|---|
| 178 | self.__logger.error(unicode(message)) |
|---|
| 179 | |
|---|
| 180 | def critical(self, message): |
|---|
| 181 | """ Logs a message with level CRITICAL. |
|---|
| 182 | |
|---|
| 183 | @param message: message to log |
|---|
| 184 | @type message: string |
|---|
| 185 | """ |
|---|
| 186 | self.__logger.critical(unicode(message)) |
|---|
| 187 | |
|---|
| 188 | def exception(self, message="", debug=False): |
|---|
| 189 | """ Logs a message within an exception. |
|---|
| 190 | |
|---|
| 191 | @param message: message to log |
|---|
| 192 | @type message: string |
|---|
| 193 | |
|---|
| 194 | @param debug: flag to log exception on DEBUG level instead of EXCEPTION one |
|---|
| 195 | @type debug: bool |
|---|
| 196 | """ |
|---|
| 197 | #self.__logger.exception(message) |
|---|
| 198 | |
|---|
| 199 | tracebackString = StringIO.StringIO() |
|---|
| 200 | traceback.print_exc(file=tracebackString) |
|---|
| 201 | message += "\n"+tracebackString.getvalue().strip() |
|---|
| 202 | tracebackString.close() |
|---|
| 203 | if debug: |
|---|
| 204 | self.debug(unicode(message)) |
|---|
| 205 | else: |
|---|
| 206 | self.log(logging.EXCEPTION, unicode(message)) |
|---|
| 207 | |
|---|
| 208 | def log(self, level, message, *args, **kwargs): |
|---|
| 209 | """ Logs a message with given level. |
|---|
| 210 | |
|---|
| 211 | @param level: log level to use |
|---|
| 212 | @type level: int |
|---|
| 213 | |
|---|
| 214 | @param message: message to log |
|---|
| 215 | @type message: string |
|---|
| 216 | """ |
|---|
| 217 | self.__logger.log(level, unicode(message), *args, **kwargs) |
|---|
| 218 | |
|---|
| 219 | def getTraceback(self): |
|---|
| 220 | """ Return the complete traceback. |
|---|
| 221 | |
|---|
| 222 | Should be called in an except statement. |
|---|
| 223 | """ |
|---|
| 224 | tracebackString = StringIO.StringIO() |
|---|
| 225 | traceback.print_exc(file=tracebackString) |
|---|
| 226 | message = tracebackString.getvalue().strip() |
|---|
| 227 | tracebackString.close() |
|---|
| 228 | return unicode(message) |
|---|
| 229 | |
|---|
| 230 | def shutdown(self): |
|---|
| 231 | """ Shutdown the logging service. |
|---|
| 232 | """ |
|---|
| 233 | logging.shutdown() |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | # Logger factory |
|---|
| 237 | def Logger(defaultStreamHandler=True, defaultFileHandler=True): |
|---|
| 238 | global logger |
|---|
| 239 | if logger is None: |
|---|
| 240 | logger = LoggerObject(defaultStreamHandler, defaultFileHandler) |
|---|
| 241 | |
|---|
| 242 | return logger |
|---|