Package libxyz :: Package core :: Package logger :: Module logger
[hide private]
[frames] | no frames]

Source Code for Module libxyz.core.logger.logger

  1  #-*- coding: utf8 -* 
  2  # 
  3  # Max E. Kuznecov ~syhpoon <syhpoon@syhpoon.name> 2008 
  4  # 
  5  # This file is part of XYZCommander. 
  6  # XYZCommander is free software: you can redistribute it and/or modify 
  7  # it under the terms of the GNU Lesser Public License as published by 
  8  # the Free Software Foundation, either version 3 of the License, or 
  9  # (at your option) any later version. 
 10  # XYZCommander 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 
 13  # GNU Lesser Public License for more details. 
 14  # You should have received a copy of the GNU Lesser Public License 
 15  # along with XYZCommander. If not, see <http://www.gnu.org/licenses/>. 
 16   
 17  import re 
 18   
 19  import libxyz.ui as uilib 
 20   
 21  from libxyz.ui import lowui 
 22  from libxyz.core.plugins import VirtualPlugin 
 23  from libxyz.core import Queue 
 24  from libxyz.core.logger.loglevel import LogLevel 
 25  from libxyz.core.logger.logentry import LogEntry 
 26   
27 -class Logger(object):
28 """ 29 Logger console is used to collect system messages. 30 There are several message levels: 31 PANIC: Critical error. 32 ERROR: Non-critical error. 33 WARNING: Warning. 34 INFO: Informational message. 35 DEBUG: Debug messages. 36 ALL: All of the above. 37 """ 38
39 - def __init__(self, xyz, levels, lines=100):
40 """ 41 @param xyz: XYZ data 42 @param levels: A list of levels to track 43 @param lines: Max number of lines to be shown in logger console 44 """ 45 46 self.xyz = xyz 47 48 try: 49 self.lines = int(lines) 50 except ValueError: 51 pass 52 53 self.loglevel = LogLevel() 54 self.tracked_levels = self._calc_levels(levels) 55 56 self._lines = lines 57 self._data = Queue(self._lines) 58 59 self._set_internal_plugin()
60 61 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 62
63 - def show_console(self):
64 """ 65 Show logger console 66 """ 67 68 # Queue is actually subclassed from list, but SimpleListWalker 69 # checks arg type by type(), not by isinstance(), so cast explicitly 70 _walker = lowui.SimpleListWalker(list(self._data)) 71 _walker.focus = len(_walker) - 1 72 73 _dim = tuple([x - 2 for x in self.xyz.screen.get_cols_rows()]) 74 75 uilib.XYZListBox(self.xyz, self.xyz.top, _walker, 76 _(u"Logger console"), _dim).show()
77 78 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 79
80 - def log(self, msg, level=None):
81 """ 82 Add new message to log 83 84 @param msg: Message 85 @param level: Log level 86 @type level: L{LogLevel} attribute 87 """ 88 89 if level is None: 90 level = self.loglevel.UNKNOWN 91 92 _sel_attr = self.xyz.skin.attr(uilib.XYZListBox.resolution, 93 u"selected") 94 95 if self.tracked_levels & level: 96 self._data.append(LogEntry(msg, self.loglevel.str_level(level), 97 _sel_attr))
98 99 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 100 101 panic = lambda self, msg: self.log(msg, level=self.loglevel.PANIC) 102 error = lambda self, msg: self.log(msg, level=self.loglevel.ERROR) 103 warning = lambda self, msg: self.log(msg, level=self.loglevel.WARNING) 104 info = lambda self, msg: self.log(msg, level=self.loglevel.INFO) 105 debug = lambda self, msg: self.log(msg, level=self.loglevel.DEBUG) 106 107 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 108
109 - def clear(self):
110 """ 111 Clear log queue 112 """ 113 114 self._data.clear()
115 116 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 117
118 - def _calc_levels(self, level_list):
119 """ 120 Parse levels from config 121 """ 122 123 _level = self.loglevel.NONE 124 125 for _lvl in level_list: 126 _level |= _lvl 127 128 return _level
129 130 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 131
132 - def _set_internal_plugin(self):
133 """ 134 Set own virtual plugin 135 """ 136 137 _logger_plugin = VirtualPlugin(self.xyz, u"logger") 138 _logger_plugin.AUTHOR = u"Max E. Kuznecov <syhpoon@syhpoon.name>" 139 _logger_plugin.VERSION = u"0.1" 140 _logger_plugin.BRIEF_DESCRIPTION = u"Logger plugin" 141 _logger_plugin.FULL_DESCRIPTION = re.sub(r"\ {2,}", 142 r"", self.__doc__).strip() 143 _logger_plugin.HOMEPAGE = u"xyzcmd.syhpoon.name" 144 145 _logger_plugin.export(self.show_console) 146 _logger_plugin.export(self.log) 147 _logger_plugin.export(self.clear) 148 149 self.xyz.pm.register(_logger_plugin)
150