PageRenderTime 195ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/libxyz/core/logger/logger.py

https://code.google.com/p/xyzcmd/
Python | 169 lines | 68 code | 39 blank | 62 comment | 9 complexity | 5ec5b81a5a78d9ec45885cd60d72c7b1 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0
  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. import re
  17. import libxyz.ui as uilib
  18. from libxyz.ui import lowui
  19. from libxyz.core.plugins import VirtualPlugin
  20. from libxyz.core import Queue
  21. from libxyz.core.logger.loglevel import LogLevel
  22. from libxyz.core.logger.logentry import LogEntry
  23. class Logger(object):
  24. """
  25. Logger console is used to collect system messages.
  26. There are several message levels:
  27. PANIC: Critical error.
  28. ERROR: Non-critical error.
  29. WARNING: Warning.
  30. INFO: Informational message.
  31. DEBUG: Debug messages.
  32. ALL: All of the above.
  33. """
  34. def __init__(self, xyz, levels, lines=100):
  35. """
  36. @param xyz: XYZ data
  37. @param levels: A list of levels to track
  38. @param lines: Max number of lines to be shown in logger console
  39. """
  40. self.xyz = xyz
  41. try:
  42. self.lines = int(lines)
  43. except ValueError:
  44. pass
  45. self.loglevel = LogLevel()
  46. self.tracked_levels = self._calc_levels(levels)
  47. self._lines = lines
  48. self._data = Queue(self._lines)
  49. self._pending = []
  50. self._set_internal_plugin()
  51. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  52. def show_console(self):
  53. """
  54. Show logger console
  55. """
  56. # Queue is actually subclassed from list, but SimpleListWalker
  57. # checks arg type by type(), not by isinstance(), so cast explicitly
  58. _walker = lowui.SimpleListWalker(list(self._data))
  59. _walker.focus = len(_walker) - 1
  60. _dim = tuple([x - 2 for x in self.xyz.screen.get_cols_rows()])
  61. uilib.XYZListBox(self.xyz, self.xyz.top, _walker,
  62. _(u"Logger console"), _dim).show()
  63. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  64. def log(self, msg, level=None):
  65. """
  66. Add new message to log
  67. @param msg: Message
  68. @param level: Log level
  69. @type level: L{LogLevel} attribute
  70. """
  71. if level is None:
  72. level = self.loglevel.UNKNOWN
  73. # Urwid is not yet inited, postpone
  74. if self.xyz.skin is None:
  75. self._pending.append((level, msg))
  76. return
  77. _sel_attr = self.xyz.skin.attr(uilib.XYZListBox.resolution,
  78. u"selected")
  79. if self.tracked_levels & level:
  80. self._data.append(LogEntry(msg, self.loglevel.str_level(level),
  81. _sel_attr))
  82. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  83. def process_pending(self):
  84. """
  85. Process pending messages
  86. """
  87. if self._pending:
  88. for l, m in self._pending:
  89. self.log(m, l)
  90. self._pending = []
  91. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  92. panic = lambda self, msg: self.log(msg, level=self.loglevel.PANIC)
  93. error = lambda self, msg: self.log(msg, level=self.loglevel.ERROR)
  94. warning = lambda self, msg: self.log(msg, level=self.loglevel.WARNING)
  95. info = lambda self, msg: self.log(msg, level=self.loglevel.INFO)
  96. debug = lambda self, msg: self.log(msg, level=self.loglevel.DEBUG)
  97. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  98. def clear(self):
  99. """
  100. Clear log queue
  101. """
  102. self._data.clear()
  103. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  104. def _calc_levels(self, level_list):
  105. """
  106. Parse levels from config
  107. """
  108. _level = self.loglevel.NONE
  109. for _lvl in level_list:
  110. _level |= _lvl
  111. return _level
  112. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  113. def _set_internal_plugin(self):
  114. """
  115. Set own virtual plugin
  116. """
  117. _logger_plugin = VirtualPlugin(self.xyz, u"logger")
  118. _logger_plugin.AUTHOR = u"Max E. Kuznecov <syhpoon@syhpoon.name>"
  119. _logger_plugin.VERSION = u"0.1"
  120. _logger_plugin.BRIEF_DESCRIPTION = u"Logger plugin"
  121. _logger_plugin.FULL_DESCRIPTION = re.sub(r"\ {2,}",
  122. r"", self.__doc__).strip()
  123. _logger_plugin.HOMEPAGE = u"xyzcmd.syhpoon.name"
  124. _logger_plugin.export(self.show_console)
  125. _logger_plugin.export(self.log)
  126. _logger_plugin.export(self.clear)
  127. self.xyz.pm.register(_logger_plugin)