ppf/new: Full implementation of add_* for MySQL.
[cs-p2p-next.git] / ppf / new / parser.py
1 """
2 Storage class for P2P logging information.
3
4 Built on previous work by Adriana Draghici, Marius Sandu-Popa, Razvan
5 Deaconescu and Mariana Marasoiu.
6
7 2011, Razvan Deaconescu, razvan.deaconescu@cs.pub.ro
8 """
9
10 import os
11 import os.path
12 import re
13 import datetime
14 import logging
15
16 import storage      # Use *Message classes.
17
18 #
19 # Logging code heavily inspired by Logging HOWTO documentation:
20 #     http://docs.python.org/dev/howto/logging.html#configuring-logging
21 #
22
23 # Create logger; default logging level is DEBUG.
24 logger = logging.getLogger(__name__)
25 logger.setLevel(logging.DEBUG)
26
27 # Create console handler and set level to ERROR.
28 ch = logging.StreamHandler()
29 ch.setLevel(logging.DEBUG)
30
31 # Create formatter.
32 formatter = logging.Formatter('%(filename)s:%(lineno)s - %(levelname)s: %(message)s')
33
34 # Add formatter to console handler.
35 ch.setFormatter(formatter)
36
37 # Add console handler to logger.
38 logger.addHandler(ch)
39
40 class SessionLogParser(object):
41     """
42     Top-level class for parsing log file(s) for a given BitTorrent session.
43     """
44
45     def __init__(self, path):
46         self.path = path
47
48     def get_next_message(self):
49         """
50         Find next message in log file/folder. May be status, peer status
51         or verbose message.
52         Return None when all logs have been parsed.
53         """
54         return None
55
56
57 class LibtorrentLogParser(object):
58     """
59     libtorrent-rasterbar log folder parser.
60     """
61
62     def __init__(self, path, priority=None):
63         """
64         If priority == "verbose" parse verbose log files first. Else, parse
65         status file first."
66         """
67         super(LibtorrrentLogParser, self).__init__(path)
68
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
72         self.to_parse = []
73         self.have_parsed = []
74         self.parsing = None
75
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.
80                 if True:
81                     to_parse.append(entry_path)
82
83         status_file_path = os.path.join(path, "status.log")
84         # TODO: Check if status file exists and is a file.
85
86         # List functions as a stack. First files go to the end.
87         if priority == "verbose":
88             to_parse.insert(0, status_file_path)
89         else:
90             to_parse.append(status_file_path)
91
92         open_next_file()
93
94     def open_next_file(self):
95         """
96         Open next log file from to_parse list.
97         Update have_parsed and parsing accordingly.
98         """
99         if self.parsing is None:     # first call
100             pass
101         else:
102             f.close()
103             have_parsed.append(parsing)
104
105         parsing = to_parse.pop()
106         f = open(parsing, 'r')
107
108         # TODO: Log this information somewhere for snapshotting purpose.
109         # In case an error occurs parsing would resume from that point.
110
111     def parse_status_log_line(self):
112         return None
113
114     def parse_verbose_log_line(self):
115         return None
116
117     def get_next_message(self):
118         """
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
121         been parsed.
122         """
123
124         while True:             # Find first message.
125             while True:             # Find first available file.
126                 line = f.readline()
127                 if line is not None:
128                     break
129
130                 # end of file reached
131                 open_next_file
132                 continue
133
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)
137
138             # Go around in case line is bogus (msg is None).
139             if msg is not None:
140                 break
141
142         return msg
143
144
145 class TriblerLogParser(object):
146     """
147     Tribler log file parser.
148     """
149
150     def __init__(self, path):
151         super(TriblerLogParser, self).__init__(path)
152
153     def get_next_message(self):
154         """
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
158         been parsed.
159         """
160         return None