7 |
7 |
8 from log_line import LogTypes |
8 from log_line import LogTypes |
9 |
9 |
10 class LogFormatter (object) : |
10 class LogFormatter (object) : |
11 """ |
11 """ |
12 Provides a method to format series of LogLines into various output formats, with varying themes |
12 Provides a method to format series of LogLines into various output formats, with varying themes. |
13 """ |
13 """ |
14 |
14 |
15 # the formatter's code name |
15 # the formatter's code name |
16 name = None |
16 name = None |
17 |
17 |
21 """ |
21 """ |
22 |
22 |
23 self.tz = tz |
23 self.tz = tz |
24 self.timestamp_fmt = timestamp_fmt |
24 self.timestamp_fmt = timestamp_fmt |
25 |
25 |
26 def _format_line_text (self, line, template_dict) : |
26 def _format_line_text (self, line, template_dict, full_timestamp=False) : |
27 """ |
27 """ |
28 Format the given line as text, using the given { type: string template } dict |
28 Format the given line as text, using the given { type: string template } dict |
29 """ |
29 """ |
30 |
30 |
31 # look up the template |
31 # look up the template |
32 template = template_dict[line.type] |
32 template = template_dict[line.type] |
|
33 |
|
34 # convert timestamp into display timezone |
|
35 dtz = line.timestamp.astimezone(self.tz) |
|
36 |
|
37 # full timestamps? |
|
38 if full_timestamp : |
|
39 # XXX: ugly |
|
40 timestamp_fmt = '%Y-%m-%d ' + self.timestamp_fmt |
|
41 |
|
42 else : |
|
43 timestamp_fmt = self.timestamp_fmt |
33 |
44 |
34 # build timestamp |
45 # build timestamp |
35 timestamp = line.timestamp.astimezone(self.tz).strftime(self.timestamp_fmt) |
46 timestamp = dtz.strftime(timestamp_fmt) |
36 |
47 |
37 # format with dict |
48 # format with dict |
38 return template % dict( |
49 return template % dict( |
39 timestamp = timestamp, |
50 timestamp = timestamp, |
40 source = line.source, |
51 source = line.source, |
41 data = line.data, |
52 data = line.data, |
42 ) |
53 ) |
43 |
54 |
44 def format_txt (self, lines) : |
55 def format_txt (self, lines, full_timestamps=False) : |
45 """ |
56 """ |
46 Format as plaintext |
57 Format given lines as plaintext. |
|
58 |
|
59 If full_timestamps is given, the output will contain full timestamps with both date and time. |
|
60 |
|
61 No trailing newlines. |
47 """ |
62 """ |
48 |
63 |
49 abstract |
64 abstract |
50 |
65 |
51 def format_html (self, lines) : |
66 def format_html (self, lines, full_timestamps=False) : |
52 """ |
67 """ |
53 Format as HTML |
68 Format as HTML. |
|
69 |
|
70 See format_txt for information about arguments |
54 """ |
71 """ |
55 |
72 |
56 abstract |
73 abstract |
57 |
74 |
58 class IrssiTextFormatter (LogFormatter) : |
75 class IrssiTextFormatter (LogFormatter) : |
63 # format definitions by type |
80 # format definitions by type |
64 __FMT = { |
81 __FMT = { |
65 LogTypes.RAW : "%(timestamp)s %(data)s", |
82 LogTypes.RAW : "%(timestamp)s %(data)s", |
66 } |
83 } |
67 |
84 |
68 def format_txt (self, lines) : |
85 def format_txt (self, lines, full_timestamps=False) : |
69 # ...handle each line |
86 # ...handle each line |
70 for line in lines : |
87 for line in lines : |
71 # using __TYPES |
88 # using __TYPES |
72 yield self._format_line_text(line, self.__FMT) |
89 yield self._format_line_text(line, self.__FMT, full_timestamps) |
73 |
90 |
74 class IrssiFormatter (IrssiTextFormatter) : |
91 class IrssiFormatter (IrssiTextFormatter) : |
75 """ |
92 """ |
76 Implements plain black-and-white irssi-style formatting |
93 Implements plain black-and-white irssi-style formatting |
77 """ |
94 """ |
78 |
95 |
79 name = 'irssi' |
96 name = 'irssi' |
80 title = "Irssi (plain)" |
97 title = "Irssi (plain)" |
81 |
98 |
82 def format_html (self, lines) : |
99 def format_html (self, lines, full_timestamps=False) : |
83 """ |
100 """ |
84 Just uses format_txt, but wraps in <pre></pre> |
101 Just uses format_txt, but wraps in <pre></pre> |
85 """ |
102 """ |
86 |
103 |
87 # open pre |
104 # open pre |
88 yield "<pre>" |
105 yield "<pre>" |
89 |
106 |
90 # format using IrssiTextFormatter |
107 # format using IrssiTextFormatter |
91 for line in self.format_txt(lines) : |
108 for line in self.format_txt(lines, full_timestamps) : |
92 # escape HTML |
109 # escape HTML |
93 yield cgi.escape(line) |
110 yield cgi.escape(line) |
94 |
111 |
95 # close pre |
112 # close pre |
96 yield "</pre>" |
113 yield "</pre>" |