Package elisa :: Package core :: Module log
[hide private]
[frames] | no frames]

Source Code for Module elisa.core.log

 1  # -*- coding: utf-8 -*- 
 2  # Elisa - Home multimedia server 
 3  # Copyright (C) 2006-2008 Fluendo Embedded S.L. (www.fluendo.com). 
 4  # All rights reserved. 
 5  # 
 6  # This file is available under one of two license agreements. 
 7  # 
 8  # This file is licensed under the GPL version 3. 
 9  # See "LICENSE.GPL" in the root of this distribution including a special 
10  # exception to use Elisa with Fluendo's plugins. 
11  # 
12  # The GPL part of Elisa is also available under a commercial licensing 
13  # agreement from Fluendo. 
14  # See "LICENSE.Elisa" in the root directory of this distribution package 
15  # for details on that license. 
16   
17  from elisa.extern.log import log as externlog 
18  from elisa.extern.log.log import * 
19  from elisa.core.utils.misc import un_camelify 
20   
21  import os 
22  import threading 
23   
24 -def customStderrHandler(level, object, category, file, line, message):
25 """ 26 A log handler that writes to stderr. 27 28 @type level: string 29 @type object: string (or None) 30 @type category: string 31 @type message: string 32 """ 33 34 if not isinstance(message, basestring): 35 message = str(message) 36 37 if isinstance(message, unicode): 38 message = message.encode('utf-8') 39 40 message = "".join(message.splitlines()) 41 42 where = "(%s:%d)" % (file, line) 43 44 formatted_level = getFormattedLevelName(level) 45 formatted_time = time.strftime("%b %d %H:%M:%S") 46 thread_name = threading.currentThread().getName() 47 formatted = '%s %-15s %-27s %-15s ' % (formatted_level, thread_name, 48 category, 49 formatted_time) 50 51 safeprintf(sys.stderr, formatted) 52 safeprintf(sys.stderr, ' %s %s\n', message, where) 53 54 sys.stderr.flush()
55
56 -def setDebug(debug_string):
57 warn_all = '*:2' 58 levels = debug_string.split(',') 59 if warn_all not in levels: 60 levels.insert(0, warn_all) 61 62 externlog.setDebug(','.join(levels))
63
64 -def init(log_to_file = False):
65 66 externlog.init('ELISA_DEBUG', True) 67 68 externlog.setPackageScrubList('elisa', 'twisted') 69 70 if log_to_file == True: 71 externlog.outputToFiles('elisa.log', 'elisa.log') 72 73 setDebug(os.getenv('ELISA_DEBUG','')) 74 75 # register our log handler 76 if externlog.stderrHandler in externlog._log_handlers_limited: 77 externlog.removeLimitedLogHandler(externlog.stderrHandler) 78 externlog.addLimitedLogHandler(customStderrHandler)
79 80 # Make Loggable a new-style object
81 -class Loggable(externlog.Loggable, object):
82
83 - def __init__(self):
84 85 # I don't really like CamelCase 86 if hasattr(self, 'log_category'): 87 self.logCategory = self.log_category 88 else: 89 # set log category to class name, lowercased 90 self.logCategory = un_camelify(self.__class__.__name__) 91 92 super(Loggable, self).__init__()
93