2 Storage class for P2P logging information.
4 Built on previous work by Adriana Draghici, Marius Sandu-Popa, Razvan
5 Deaconescu and Mariana Marasoiu.
7 2011, Razvan Deaconescu, razvan.deaconescu@cs.pub.ro
16 import storage # Use *Message classes.
19 # Logging code heavily inspired by Logging HOWTO documentation:
20 # http://docs.python.org/dev/howto/logging.html#configuring-logging
23 # Create logger; default logging level is DEBUG.
24 logger = logging.getLogger(__name__)
25 logger.setLevel(logging.DEBUG)
27 # Create console handler and set level to ERROR.
28 ch = logging.StreamHandler()
29 ch.setLevel(logging.DEBUG)
32 formatter = logging.Formatter('%(filename)s:%(lineno)s - %(levelname)s: %(message)s')
34 # Add formatter to console handler.
35 ch.setFormatter(formatter)
37 # Add console handler to logger.
40 class SessionLogParser(object):
42 Top-level class for parsing log file(s) for a given BitTorrent session.
45 def __init__(self, path):
48 def get_next_message(self):
50 Find next message in log file/folder. May be status, peer status
52 Return None when all logs have been parsed.
57 class LibtorrentLogParser(object):
59 libtorrent-rasterbar log folder parser.
62 def __init__(self, path, priority=None):
64 If priority == "verbose" parse verbose log files first. Else, parse
67 super(LibtorrrentLogParser, self).__init__(path)
69 # to_parse: list of files to be parsed
70 # have_parsed: list of files that have been parsed
71 # parsing: file currently being parsed
76 for entry in os.listdir(path):
77 entry_path = os.path.join(path, entry)
78 if os.path.isfile(entry_path):
79 # TODO: If entry is file and name is IP_PORT.log add it to list.
81 to_parse.append(entry_path)
83 status_file_path = os.path.join(path, "status.log")
84 # TODO: Check if status file exists and is a file.
86 # List functions as a stack. First files go to the end.
87 if priority == "verbose":
88 to_parse.insert(0, status_file_path)
90 to_parse.append(status_file_path)
94 def open_next_file(self):
96 Open next log file from to_parse list.
97 Update have_parsed and parsing accordingly.
99 if self.parsing is None: # first call
103 have_parsed.append(parsing)
105 parsing = to_parse.pop()
106 f = open(parsing, 'r')
108 # TODO: Log this information somewhere for snapshotting purpose.
109 # In case an error occurs parsing would resume from that point.
111 def parse_status_log_line(self):
114 def parse_verbose_log_line(self):
117 def get_next_message(self):
119 Go through all files in libtorrent log folder and parse them.
120 Return the next message available or None when all log files have
124 while True: # Find first message.
125 while True: # Find first available file.
130 # end of file reached
134 # TODO: Use a flag to distinguish between status and verbose logs.
135 msg = parse_status_log_line(line)
136 #msg = parse_verbose_log_line(line)
138 # Go around in case line is bogus (msg is None).
145 class TriblerLogParser(object):
147 Tribler log file parser.
150 def __init__(self, path):
151 super(TriblerLogParser, self).__init__(path)
153 def get_next_message(self):
155 Go through Tribler log file. All log messages (verbose, status)
156 are stored in the same file.
157 Return the next message available or None when all messages have