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          self._pending = []
     59
     60          self._set_internal_plugin()
     61
     62      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     63
     64 -    def show_console(self):
     65          """
     66          Show logger console
     67          """
     68
     69          # Queue is actually subclassed from list, but SimpleListWalker
     70          # checks arg type by type(), not by isinstance(), so cast explicitly
     71          _walker = lowui.SimpleListWalker(list(self._data))
     72          _walker.focus = len(_walker) - 1
     73
     74          _dim = tuple([x - 2 for x in self.xyz.screen.get_cols_rows()])
     75
     76          uilib.XYZListBox(self.xyz, self.xyz.top, _walker,
     77                           _(u"Logger console"), _dim).show()
     78
     79      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     80
     81 -    def log(self, msg, level=None):
     82          """
     83          Add new message to log
     84
     85          @param msg: Message
     86          @param level: Log level
     87          @type level: L{LogLevel} attribute
     88          """
     89
     90          if level is None:
     91              level = self.loglevel.UNKNOWN
     92
     93          # Urwid is not yet inited, postpone
     94          if self.xyz.skin is None:
     95              self._pending.append((level, msg))
     96              return
     97
     98          _sel_attr = self.xyz.skin.attr(uilib.XYZListBox.resolution,
     99                                         u"selected")
    100
    101          if self.tracked_levels & level:
    102              self._data.append(LogEntry(msg, self.loglevel.str_level(level),
    103                                _sel_attr))
    104
    105      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    106
    107 -    def process_pending(self):
    108          """
    109          Process pending messages
    110          """
    111
    112          if self._pending:
    113
    114              for l, m in self._pending:
    115                  self.log(m, l)
    116
    117              self._pending = []
    118
    119      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    120
    121      panic = lambda self, msg: self.log(msg, level=self.loglevel.PANIC)
    122      error = lambda self, msg: self.log(msg, level=self.loglevel.ERROR)
    123      warning = lambda self, msg: self.log(msg, level=self.loglevel.WARNING)
    124      info = lambda self, msg: self.log(msg, level=self.loglevel.INFO)
    125      debug = lambda self, msg: self.log(msg, level=self.loglevel.DEBUG)
    126
    127      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    128
    129 -    def clear(self):
    130          """
    131          Clear log queue
    132          """
    133
    134          self._data.clear()
    135
    136      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    137
    138 -    def _calc_levels(self, level_list):
    139          """
    140          Parse levels from config
    141          """
    142
    143          _level = self.loglevel.NONE
    144
    145          for _lvl in level_list:
    146              _level |= _lvl
    147
    148          return _level
    149
    150      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    151
    152 -    def _set_internal_plugin(self):
    153          """
    154          Set own virtual plugin
    155          """
    156
    157          _logger_plugin = VirtualPlugin(self.xyz, u"logger")
    158          _logger_plugin.AUTHOR = u"Max E. Kuznecov <syhpoon@syhpoon.name>"
    159          _logger_plugin.VERSION = u"0.1"
    160          _logger_plugin.BRIEF_DESCRIPTION = u"Logger plugin"
    161          _logger_plugin.FULL_DESCRIPTION = re.sub(r"\ {2,}",
    162                                                   r"", self.__doc__).strip()
    163          _logger_plugin.HOMEPAGE = u"xyzcmd.syhpoon.name"
    164
    165          _logger_plugin.export(self.show_console)
    166          _logger_plugin.export(self.log)
    167          _logger_plugin.export(self.clear)
    168
    169          self.xyz.pm.register(_logger_plugin)
    170