[svn] Introduce a real logging system. Right now all this really gets us is the trunk

Tue, 19 Dec 2006 19:13:29 -0500

author
brett
date
Tue, 19 Dec 2006 19:13:29 -0500
branch
trunk
changeset 12
5d202467c589
parent 11
a2ca41a85243
child 13
0a3ef1b9f6d4

[svn] Introduce a real logging system. Right now all this really gets us is the
return of warnings when BombHandler has to use an unusual extraction
directory, but it can grow from here.

scripts/x file | annotate | diff | comparison | revisions
--- a/scripts/x	Tue Dec 19 18:01:00 2006 -0500
+++ b/scripts/x	Tue Dec 19 19:13:29 2006 -0500
@@ -18,6 +18,7 @@
 # 51 Franklin Street, 5th Floor, Boston, MA, 02111.
 
 import errno
+import logging
 import mimetypes
 import optparse
 import os
@@ -248,6 +249,7 @@
 
 class MatchHandler(object):
     def __init__(self, extractor, contents):
+        self.logger = logging.getLogger('x-log')
         self.extractor = extractor
         self.contents = contents
         self.directory = extractor.basename()
@@ -282,8 +284,9 @@
                     continue
                 raise ValueError("could not make extraction directory %s: %s" %
                                  (error.filename, error.strerror))
-##             if suffix != '':
-##                 self.show_error("extracted to %s" % (directory,))
+            if suffix != '':
+                self.logger.warning("%s: extracted to %s",
+                                    extractor.filename, self.directory)
             break
         else:
             raise ValueError("all good names for an extraction directory taken")
@@ -316,6 +319,7 @@
 class ExtractorApplication(object):
     def __init__(self, arguments):
         self.parse_options(arguments)
+        self.setup_logger()
         self.successes = []
         self.failures = []
 
@@ -333,8 +337,14 @@
             parser.error("you did not list any archives")
         self.archives = {os.path.realpath(os.curdir): filenames}
 
-    def show_error(self, message):
-        print >>sys.stderr, "%s: %s" % (self.current_filename, message)
+    def setup_logger(self):
+        self.logger = logging.getLogger('x-log')
+        handler = logging.StreamHandler()
+        # Switch here.
+        handler.setLevel(logging.WARNING)
+        formatter = logging.Formatter("x: %(levelname)s: %(message)s")
+        handler.setFormatter(formatter)
+        self.logger.addHandler(handler)
 
     def get_extractor(self):
         mimetype, encoding = mimetypes.guess_type(self.current_filename)
@@ -363,7 +373,7 @@
     def report(self, function, *args):
         error = function(*args)
         if error:
-            self.show_error(error)
+            self.logger.error("%s: %s", self.current_filename, error)
             return False
         return True
 

mercurial