1 | # lognestmonster Copyright (c) 2019 Joshua 'joshuas3' Stockin |
2 | # <https://github.com/JoshuaS3/lognestmonster/>. |
3 |
|
4 |
|
5 | # This file is part of lognestmonster. |
6 |
|
7 | # lognestmonster is free software: you can redistribute it and/or modify |
8 | # it under the terms of the GNU General Public License as published by |
9 | # the Free Software Foundation, either version 3 of the License, or |
10 | # (at your option) any later version. |
11 |
|
12 | # lognestmonster is distributed in the hope that it will be useful, |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | # GNU General Public License for more details. |
16 |
|
17 | # You should have received a copy of the GNU General Public License |
18 | # along with lognestmonster. If not, see <https://www.gnu.org/licenses/>. |
19 |
|
20 | import struct |
21 | import os |
22 | import resource |
23 | import time |
24 | import sys |
25 |
|
26 | STATEMENT_START = 0 |
27 | STATEMENT_END = 1 |
28 | EVENT_START = 2 |
29 | EVENT_END = 3 |
30 |
|
31 | VERBOSITY_LEVELS = { |
32 | 0: "INIT", |
33 | 1: "DEBUG", |
34 | 2: "VERBOSE", |
35 | 3: "VERYVERBOSE", |
36 | 4: "WARNING", |
37 | 5: "ERROR" |
38 | } |
39 |
|
40 | def ulonglong(bytestr): |
41 | return struct.unpack("@Q", bytestr)[0] |
42 | def uchar(charv): |
43 | return struct.unpack("@B", charv)[0] |
44 | def ushort(shortv): |
45 | return struct.unpack("@H", shortv)[0] |
46 |
|
47 |
|
48 | class EventProto: |
49 | parent = None |
50 | pushed = [] |
51 | def __init__(self): |
52 | self.parent = None |
53 | self.pushed = [] |
54 |
|
55 | class Reader: |
56 | fd = None |
57 |
|
58 | version = 0 |
59 | timestamp = 0 |
60 |
|
61 | top_level = [] |
62 |
|
63 | event_count = 0 |
64 | statement_count = 0 |
65 |
|
66 | file_size = 0 |
67 | position = 0 |
68 | bad_bytes = 0 |
69 |
|
70 | filter_time_start = -1 |
71 | filter_time_end = -1 |
72 | filter_verbosity = -1 |
73 | filter_tag = -1 |
74 |
|
75 | def __init__(self, fd): |
76 | self.fd = fd |
77 |
|
78 | self.version = 0 |
79 | self.timestamp = 0 |
80 |
|
81 | self.top_level = [] |
82 |
|
83 | self.event_count = 0 |
84 | self.statement_count = 0 |
85 |
|
86 | self.file_size = 0 |
87 | self.position = 0 |
88 | self.bad_bytes = 0 |
89 |
|
90 | self.filter_time_start = -1 |
91 | self.filter_time_end = -1 |
92 | self.filter_verbosity = -1 |
93 | self.filter_tag = -1 |
94 |
|
95 | def size(self): |
96 | self.fd.seek(0, os.SEEK_END) # go to end of file and get position |
97 | newsize = self.fd.tell() |
98 | self.fd.seek(self.position) # return to previous position |
99 |
|
100 | is_diff = self.file_size is not newsize |
101 | self.file_size = newsize |
102 | return is_diff |
103 |
|
104 | def pos(self): |
105 | self.position = self.fd.tell() |
106 | return self.position |
107 |
|
108 | def seek(self, position): |
109 | self.position = position |
110 | self.fd.seek(self.position) |
111 |
|
112 | def read(self, byte_count): |
113 | data = self.fd.read(byte_count) |
114 | if len(data) == byte_count: |
115 | return data |
116 | else: |
117 | return False |
118 |
|
119 | def scan(self): # scan for events and statements from self.position to the end of file |
120 | print() |
121 | print("beginning file scan") |
122 | print("file size: {0}".format(self.file_size)) |
123 | print() |
124 |
|
125 | s = time.time() |
126 |
|
127 | if self.position == 0: # if it's the start of the file, grab version and timestamp |
128 | self.version = uchar(self.read(1)) |
129 | self.timestamp = ulonglong(self.read(8)) |
130 |
|
131 | current_statement = None |
132 | current_event = None |
133 |
|
134 | if self.position < self.file_size: # if the seeker is before EOF |
135 | while self.position < self.file_size: # while the seeker is before EOF |
136 | in_byte = uchar(self.read(1)) # read 1 byte |
137 |
|
138 | if in_byte == STATEMENT_START: # the byte indicates a statement's start, begin interpreting |
139 | self.statement_count += 1 |
140 |
|
141 | new_statement = self.position |
142 |
|
143 | timestamp = ulonglong(self.read(8)) |
144 | verbosity = uchar(self.read(1)) |
145 | |
146 | tag_size = uchar(self.read(1)) |
147 | tag = self.read(tag_size).decode("utf-8") |
148 | |
149 | append = True |
150 | if self.filter_time_start is not -1: |
151 | if timestamp < self.filter_time_start: append = False |
152 | if self.filter_time_end is not -1: |
153 | if timestamp > self.filter_time_end: append = False |
154 | if self.filter_verbosity is not -1: |
155 | if verbosity is not self.filter_verbosity: append = False |
156 | if self.filter_tag is not -1: |
157 | if tag is not self.filter_tag: append = False |
158 |
|
159 | message_size = ushort(self.read(2)) |
160 | self.seek(self.pos() + message_size) # ignore the message |
161 | |
162 | while uchar(self.read(1)) is not STATEMENT_END and self.pos() < self.file_size: |
163 | self.bad_bytes += 1 |
164 |
|
165 | if append: |
166 | if current_event is not None: |
167 | current_event.pushed.append(new_statement) |
168 | else: |
169 | self.top_level.append(new_statement) |
170 |
|
171 | elif in_byte == EVENT_START: # the byte indicates an event's start, create an event |
172 | self.event_count += 1 |
173 | new_event = EventProto() |
174 | if current_event is not None: # if an event exists, push the new event to it |
175 | new_event.parent = current_event |
176 | current_event.pushed.append(new_event) |
177 | current_event = new_event |
178 |
|
179 |
|
180 | elif in_byte == EVENT_END: # the byte indicates an event's end, close event if exists |
181 | if current_event is not None: # if an event exists |
182 | if current_event.parent is not None: |
183 | current_event = current_event.parent # if the event has a parent, set the parent to current |
184 | else: |
185 | self.top_level.append(current_event) # event has no parent, it's a top-level log item |
186 | current_event = None |
187 |
|
188 | else: |
189 | self.bad_bytes += 1 |
190 |
|
191 | self.pos() # update seeker position for next byte (if not EOF) |
192 |
|
193 | print() |
194 | print("finished reading, {0} bad bytes".format(self.bad_bytes)) |
195 | print() |
196 | print("version {0}".format(self.version)) |
197 | print("timestamp {0}".format(self.timestamp)) |
198 | print("event count {0}".format(self.event_count)) |
199 | print("statement count {0}".format(self.statement_count)) |
200 | print("time: {0}".format(time.time() - s)) |
201 |
|
202 |
|