1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
65 """
66 Show logger console
67 """
68
69
70
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
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
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
130 """
131 Clear log queue
132 """
133
134 self._data.clear()
135
136
137
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
170