log_formatter.py
changeset 65 8b50694f841e
parent 64 cdb6403c2498
child 69 1f182913b1f2
--- a/log_formatter.py	Mon Feb 09 11:05:53 2009 +0200
+++ b/log_formatter.py	Mon Feb 09 11:46:17 2009 +0200
@@ -9,7 +9,7 @@
 
 class LogFormatter (object) :
     """
-        Provides a method to format series of LogLines into various output formats, with varying themes
+        Provides a method to format series of LogLines into various output formats, with varying themes.
     """
 
     # the formatter's code name
@@ -23,16 +23,27 @@
         self.tz = tz
         self.timestamp_fmt = timestamp_fmt
     
-    def _format_line_text (self, line, template_dict) :
+    def _format_line_text (self, line, template_dict, full_timestamp=False) :
         """
             Format the given line as text, using the given { type: string template } dict
         """
-        
+            
         # look up the template
         template = template_dict[line.type]
+        
+        # convert timestamp into display timezone
+        dtz = line.timestamp.astimezone(self.tz)
+        
+        # full timestamps?
+        if full_timestamp :
+            # XXX: ugly
+            timestamp_fmt = '%Y-%m-%d ' + self.timestamp_fmt
+
+        else :
+            timestamp_fmt = self.timestamp_fmt
 
         # build timestamp
-        timestamp = line.timestamp.astimezone(self.tz).strftime(self.timestamp_fmt)
+        timestamp = dtz.strftime(timestamp_fmt)
         
         # format with dict
         return template % dict(
@@ -41,16 +52,22 @@
             data            = line.data,
         )
     
-    def format_txt (self, lines) :
+    def format_txt (self, lines, full_timestamps=False) :
         """
-            Format as plaintext
+            Format given lines as plaintext.
+
+            If full_timestamps is given, the output will contain full timestamps with both date and time.
+
+            No trailing newlines.
         """
 
         abstract
 
-    def format_html (self, lines) :
+    def format_html (self, lines, full_timestamps=False) :
         """
-            Format as HTML
+            Format as HTML.
+            
+            See format_txt for information about arguments
         """
 
         abstract
@@ -65,11 +82,11 @@
         LogTypes.RAW        : "%(timestamp)s %(data)s",
     }
 
-    def format_txt (self, lines) :
+    def format_txt (self, lines, full_timestamps=False) :
         # ...handle each line
         for line in lines :
             # using __TYPES
-            yield self._format_line_text(line, self.__FMT)
+            yield self._format_line_text(line, self.__FMT, full_timestamps)
 
 class IrssiFormatter (IrssiTextFormatter) :
     """
@@ -79,7 +96,7 @@
     name = 'irssi'
     title = "Irssi (plain)"
 
-    def format_html (self, lines) :
+    def format_html (self, lines, full_timestamps=False) :
         """
             Just uses format_txt, but wraps in <pre></pre>
         """
@@ -88,7 +105,7 @@
         yield "<pre>"
         
         # format using IrssiTextFormatter
-        for line in self.format_txt(lines) :
+        for line in self.format_txt(lines, full_timestamps) :
             # escape HTML
             yield cgi.escape(line)