From: Razvan Deaconescu Date: Fri, 26 Aug 2011 12:40:45 +0000 (+0300) Subject: ppf/new: Add logging support to util.py. Fixes due to tests. X-Git-Url: http://p2p-next.cs.pub.ro/gitweb/?a=commitdiff_plain;h=41c254fbfa514c35df6345903ce4e8cac7ee5d6c;p=cs-p2p-next.git ppf/new: Add logging support to util.py. Fixes due to tests. --- diff --git a/ppf/new/util.py b/ppf/new/util.py index 5068e02..dbf7e68 100644 --- a/ppf/new/util.py +++ b/ppf/new/util.py @@ -4,10 +4,35 @@ Utility classes. 2011, Razvan Deaconescu, razvan.deaconescu@cs.pub.ro """ +import logging + +# +# Logging code heavily inspired by Logging HOWTO documentation: +# http://docs.python.org/dev/howto/logging.html#configuring-logging +# + +# Create logger; default logging level is DEBUG. +logger = logging.getLogger(__name__) +logger.setLevel(logging.DEBUG) + +# Create console handler and set level to ERROR. +ch = logging.StreamHandler() +ch.setLevel(logging.DEBUG) + +# Create formatter. +formatter = logging.Formatter('%(filename)s:%(lineno)s - %(levelname)s: %(message)s') + +# Add formatter to console handler. +ch.setFormatter(formatter) + +# Add console handler to logger. +logger.addHandler(ch) + + class LiveLogStatus(object): """ Status information regarding live log processing. Stored in the - '.lstatus' file. + '.status' file. """ def __init__(self, position=0): @@ -20,14 +45,14 @@ class LiveLogFile(object): by a log file. A live status file is used for storing status information (such as current position in log file processing). The live status file name is the - log file name with ".lstatus" appended to it. + log file name with ".status" appended to it. """ def __init__(self, log_file_name): self.log_file_name = log_file_name - self.status_file_name = log_file_name + ".lstatus" + self.status_file_name = log_file_name + ".status" - self.log_file = open(self.log_file_name, "tt") + self.log_file = open(self.log_file_name, "rt") self.status_file = open(self.status_file_name, "ab+") self.status_file.seek(0) @@ -45,15 +70,18 @@ class LiveLogFile(object): line = self.status_file.readline() try: position = int(line) - except TypeError, e: + except ValueError, e: position = 0 + logger.debug("Invalid live. Use position = 0.") self.status = LiveLogStatus(position) self.status_file.seek(0) def store_status(self): self.status_file.seek(0) + self.status_file.truncate() self.status.position = self.log_file.tell() + logger.debug("status.position: %s" %(self.status.position)) self.status_file.write(str(self.status.position) + "\n") self.status_file.seek(0)