2007-11-14 08:30:19 +00:00
|
|
|
# -*- coding: utf-8; mode: python; -*-
|
|
|
|
#
|
2007-11-22 08:47:06 +00:00
|
|
|
# GStreamer Debug Viewer - View and analyze GStreamer debug log files
|
2007-11-14 08:30:19 +00:00
|
|
|
#
|
|
|
|
# Copyright (C) 2007 René Stadler <mail@renestadler.de>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU General Public License as published by the Free
|
|
|
|
# Software Foundation; either version 3 of the License, or (at your option)
|
|
|
|
# any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
# more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along with
|
|
|
|
# this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2007-11-22 08:47:06 +00:00
|
|
|
"""GStreamer Debug Viewer Data module."""
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2007-11-19 13:52:01 +00:00
|
|
|
import os
|
2007-11-14 08:30:19 +00:00
|
|
|
import logging
|
|
|
|
import re
|
2018-04-14 13:22:11 +00:00
|
|
|
import sys
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2015-10-20 13:21:01 +00:00
|
|
|
# Nanosecond resolution (like Gst.SECOND)
|
2007-11-14 08:30:19 +00:00
|
|
|
SECOND = 1000000000
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
|
|
|
|
def time_args(ts):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
secs = ts // SECOND
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
return "%i:%02i:%02i.%09i" % (secs // 60 ** 2,
|
2007-11-14 08:30:19 +00:00
|
|
|
secs // 60 % 60,
|
|
|
|
secs % 60,
|
|
|
|
ts % SECOND,)
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
|
|
|
|
def time_diff_args(time_diff):
|
2009-10-16 18:45:29 +00:00
|
|
|
|
|
|
|
if time_diff >= 0:
|
|
|
|
sign = "+"
|
|
|
|
else:
|
|
|
|
sign = "-"
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
secs = abs(time_diff) // SECOND
|
2009-10-16 18:45:29 +00:00
|
|
|
|
|
|
|
return "%s%02i:%02i.%09i" % (sign,
|
|
|
|
secs // 60,
|
|
|
|
secs % 60,
|
2016-09-28 18:38:55 +00:00
|
|
|
abs(time_diff) % SECOND,)
|
2009-10-16 18:45:29 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
|
|
|
|
def time_args_no_hours(ts):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
secs = ts // SECOND
|
|
|
|
|
|
|
|
return "%02i:%02i.%09i" % (secs // 60,
|
|
|
|
secs % 60,
|
|
|
|
ts % SECOND,)
|
|
|
|
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def parse_time(st):
|
2007-11-14 08:30:19 +00:00
|
|
|
"""Parse time strings that look like "0:00:00.0000000"."""
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
h, m, s = st.split(":")
|
|
|
|
secs, subsecs = s.split(".")
|
|
|
|
|
2018-04-14 13:22:11 +00:00
|
|
|
return int((int(h) * 60 ** 2 + int(m) * 60) * SECOND) + \
|
2018-04-14 15:04:22 +00:00
|
|
|
int(secs) * SECOND + int(subsecs)
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DebugLevel (int):
|
|
|
|
|
2018-05-13 12:02:11 +00:00
|
|
|
__names = ["NONE", "ERROR", "WARN", "FIXME",
|
|
|
|
"INFO", "DEBUG", "LOG", "TRACE", "MEMDUMP"]
|
2007-11-14 08:30:19 +00:00
|
|
|
__instances = {}
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def __new__(cls, level):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
try:
|
2016-09-28 18:38:55 +00:00
|
|
|
level_int = int(level)
|
2007-11-14 08:30:19 +00:00
|
|
|
except (ValueError, TypeError,):
|
|
|
|
try:
|
2016-09-28 18:38:55 +00:00
|
|
|
level_int = cls.__names.index(level.upper())
|
2007-11-14 08:30:19 +00:00
|
|
|
except ValueError:
|
2016-09-28 18:38:55 +00:00
|
|
|
raise ValueError("no debug level named %r" % (level,))
|
2007-11-14 08:30:19 +00:00
|
|
|
if level_int in cls.__instances:
|
|
|
|
return cls.__instances[level_int]
|
|
|
|
else:
|
2016-09-28 18:38:55 +00:00
|
|
|
new_instance = int.__new__(cls, level_int)
|
2007-11-14 08:30:19 +00:00
|
|
|
new_instance.name = cls.__names[level_int]
|
|
|
|
cls.__instances[level_int] = new_instance
|
|
|
|
return new_instance
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def __repr__(self):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
return "<%s %s (%i)>" % (type(self).__name__, self.__names[self], self,)
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def higher_level(self):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
if self == len(self.__names) - 1:
|
|
|
|
raise ValueError("already the highest debug level")
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
return DebugLevel(self + 1)
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def lower_level(self):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
if self == 0:
|
2016-09-28 18:38:55 +00:00
|
|
|
raise ValueError("already the lowest debug level")
|
|
|
|
|
|
|
|
return DebugLevel(self - 1)
|
|
|
|
|
2018-04-14 15:04:22 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
debug_level_none = DebugLevel("NONE")
|
|
|
|
debug_level_error = DebugLevel("ERROR")
|
|
|
|
debug_level_warning = DebugLevel("WARN")
|
|
|
|
debug_level_info = DebugLevel("INFO")
|
|
|
|
debug_level_debug = DebugLevel("DEBUG")
|
|
|
|
debug_level_log = DebugLevel("LOG")
|
|
|
|
debug_level_fixme = DebugLevel("FIXME")
|
|
|
|
debug_level_trace = DebugLevel("TRACE")
|
2018-05-13 12:02:11 +00:00
|
|
|
debug_level_memdump = DebugLevel("MEMDUMP")
|
2008-01-23 12:51:14 +00:00
|
|
|
debug_levels = [debug_level_none,
|
2010-07-06 08:42:08 +00:00
|
|
|
debug_level_trace,
|
|
|
|
debug_level_fixme,
|
2008-01-23 12:51:14 +00:00
|
|
|
debug_level_log,
|
|
|
|
debug_level_debug,
|
|
|
|
debug_level_info,
|
|
|
|
debug_level_warning,
|
2018-05-13 12:02:11 +00:00
|
|
|
debug_level_error,
|
|
|
|
debug_level_memdump]
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
# For stripping color codes:
|
2018-04-14 13:22:11 +00:00
|
|
|
_escape = re.compile(b"\x1b\\[[0-9;]*m")
|
2016-09-28 18:38:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
def strip_escape(s):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2007-11-19 09:35:27 +00:00
|
|
|
# FIXME: This can be optimized further!
|
|
|
|
|
2018-04-14 13:22:11 +00:00
|
|
|
while b"\x1b" in s:
|
|
|
|
s = _escape.sub(b"", s)
|
2007-11-19 09:35:27 +00:00
|
|
|
return s
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
|
|
|
|
def default_log_line_regex_():
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
# "DEBUG "
|
2018-05-13 12:02:11 +00:00
|
|
|
LEVEL = "([A-Z]+)\s*"
|
2007-11-14 08:30:19 +00:00
|
|
|
# "0x8165430 "
|
2016-09-28 18:38:55 +00:00
|
|
|
THREAD = r"(0x[0-9a-f]+)\s+" # r"\((0x[0-9a-f]+) - "
|
2007-11-14 08:30:19 +00:00
|
|
|
# "0:00:00.777913000 "
|
2008-06-29 18:14:07 +00:00
|
|
|
TIME = r"(\d+:\d\d:\d\d\.\d+)\s+"
|
2016-09-28 18:38:55 +00:00
|
|
|
CATEGORY = "([A-Za-z0-9_-]+)\s+" # "GST_REFCOUNTING ", "flacdec "
|
2007-11-14 08:30:19 +00:00
|
|
|
# " 3089 "
|
2012-09-21 17:11:40 +00:00
|
|
|
PID = r"(\d+)\s*"
|
2012-09-20 21:51:05 +00:00
|
|
|
FILENAME = r"([^:]*):"
|
2008-06-29 18:14:07 +00:00
|
|
|
LINE = r"(\d+):"
|
2018-04-14 13:22:11 +00:00
|
|
|
FUNCTION = "(~?[A-Za-z0-9_\s\*,\(\)]*):"
|
2007-11-14 08:30:19 +00:00
|
|
|
# FIXME: When non-g(st)object stuff is logged with *_OBJECT (like
|
|
|
|
# buffers!), the address is printed *without* <> brackets!
|
2012-09-21 17:11:40 +00:00
|
|
|
OBJECT = "(?:<([^>]+)>)?"
|
2008-06-29 18:14:07 +00:00
|
|
|
MESSAGE = "(.+)"
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2012-09-21 17:11:40 +00:00
|
|
|
ANSI = "(?:\x1b\\[[0-9;]*m\\s*)*\\s*"
|
|
|
|
|
2007-11-15 13:06:37 +00:00
|
|
|
# New log format:
|
2012-09-21 17:11:40 +00:00
|
|
|
expressions = [TIME, ANSI, PID, ANSI, THREAD, ANSI, LEVEL, ANSI,
|
|
|
|
CATEGORY, FILENAME, LINE, FUNCTION, ANSI,
|
|
|
|
OBJECT, ANSI, MESSAGE]
|
2007-11-15 13:06:37 +00:00
|
|
|
# Old log format:
|
2016-09-28 18:38:55 +00:00
|
|
|
# expressions = [LEVEL, THREAD, TIME, CATEGORY, PID, FILENAME, LINE,
|
|
|
|
# FUNCTION, OBJECT, MESSAGE]
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
return expressions
|
|
|
|
|
2009-03-14 21:02:45 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def default_log_line_regex():
|
|
|
|
|
2016-12-13 12:26:35 +00:00
|
|
|
return re.compile("".join(default_log_line_regex_()))
|
2016-09-28 18:38:55 +00:00
|
|
|
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
class Producer (object):
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def __init__(self):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
self.consumers = []
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def have_load_started(self):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
for consumer in self.consumers:
|
2016-09-28 18:38:55 +00:00
|
|
|
consumer.handle_load_started()
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def have_load_finished(self):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
for consumer in self.consumers:
|
2016-09-28 18:38:55 +00:00
|
|
|
consumer.handle_load_finished()
|
|
|
|
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2011-11-16 19:37:21 +00:00
|
|
|
class SortHelper (object):
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def __init__(self, fileobj, offsets):
|
2011-11-16 19:37:21 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
self._gen = self.__gen(fileobj, offsets)
|
2018-04-14 13:22:11 +00:00
|
|
|
next(self._gen)
|
2011-11-16 19:37:21 +00:00
|
|
|
|
|
|
|
# Override in the instance, for performance (this gets called in an
|
|
|
|
# inner loop):
|
|
|
|
self.find_insert_position = self._gen.send
|
|
|
|
|
|
|
|
@staticmethod
|
2016-09-28 18:38:55 +00:00
|
|
|
def find_insert_position(insert_time_string):
|
2011-11-16 19:37:21 +00:00
|
|
|
|
|
|
|
# Stub for documentary purposes.
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
@staticmethod
|
2016-09-28 18:38:55 +00:00
|
|
|
def __gen(fileobj, offsets):
|
2011-11-16 19:37:21 +00:00
|
|
|
|
|
|
|
from math import floor
|
|
|
|
tell = fileobj.tell
|
|
|
|
seek = fileobj.seek
|
|
|
|
read = fileobj.read
|
2016-09-28 18:38:55 +00:00
|
|
|
time_len = len(time_args(0))
|
2011-11-16 19:37:21 +00:00
|
|
|
|
|
|
|
# We remember the previous insertion point. This gives a nice speed up
|
|
|
|
# for larger bubbles which are already sorted. TODO: In practice, log
|
|
|
|
# lines only get out of order across threads. Need to check if it pays
|
|
|
|
# to parse the thread here and maintain multiple insertion points for
|
|
|
|
# heavily interleaved parts of the log.
|
|
|
|
pos = 0
|
|
|
|
pos_time_string = ""
|
|
|
|
|
|
|
|
insert_pos = None
|
|
|
|
while True:
|
|
|
|
insert_time_string = (yield insert_pos)
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
save_offset = tell()
|
2011-11-16 19:37:21 +00:00
|
|
|
|
|
|
|
if pos_time_string <= insert_time_string:
|
|
|
|
lo = pos
|
2016-09-28 18:38:55 +00:00
|
|
|
hi = len(offsets)
|
2011-11-16 19:37:21 +00:00
|
|
|
else:
|
|
|
|
lo = 0
|
|
|
|
hi = pos
|
|
|
|
|
|
|
|
# This is a bisection search, except we don't cut the range in the
|
|
|
|
# middle each time, but at the 90th percentile. This is because
|
|
|
|
# logs are "mostly sorted", so the insertion point is much more
|
|
|
|
# likely to be at the end anyways:
|
|
|
|
while lo < hi:
|
2016-09-28 18:38:55 +00:00
|
|
|
mid = int(floor(lo * 0.1 + hi * 0.9))
|
|
|
|
seek(offsets[mid])
|
|
|
|
mid_time_string = read(time_len)
|
2018-04-14 13:22:11 +00:00
|
|
|
if insert_time_string.encode('utf8') < mid_time_string:
|
2011-11-16 19:37:21 +00:00
|
|
|
hi = mid
|
|
|
|
else:
|
|
|
|
lo = mid + 1
|
|
|
|
pos = lo
|
|
|
|
# Caller will replace row at pos with the new one, so this is
|
|
|
|
# correct:
|
|
|
|
pos_time_string = insert_time_string
|
|
|
|
|
|
|
|
insert_pos = pos
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
seek(save_offset)
|
|
|
|
|
2011-11-16 19:37:21 +00:00
|
|
|
|
2007-11-14 08:30:19 +00:00
|
|
|
class LineCache (Producer):
|
2016-09-30 20:31:24 +00:00
|
|
|
"""
|
|
|
|
offsets: file position for each line
|
|
|
|
levels: the debug level for each line
|
|
|
|
"""
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2007-11-14 08:55:12 +00:00
|
|
|
_lines_per_iteration = 50000
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def __init__(self, fileobj, dispatcher):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
Producer.__init__(self)
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
self.logger = logging.getLogger("linecache")
|
2007-11-14 08:30:19 +00:00
|
|
|
self.dispatcher = dispatcher
|
|
|
|
|
2007-12-03 13:45:09 +00:00
|
|
|
self.__fileobj = fileobj
|
2016-09-28 18:38:55 +00:00
|
|
|
self.__fileobj.seek(0, 2)
|
|
|
|
self.__file_size = self.__fileobj.tell()
|
|
|
|
self.__fileobj.seek(0)
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2007-11-21 12:21:38 +00:00
|
|
|
self.offsets = []
|
2016-09-28 18:38:55 +00:00
|
|
|
self.levels = [] # FIXME
|
2007-11-21 09:40:13 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def start_loading(self):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
self.logger.debug("dispatching load process")
|
|
|
|
self.have_load_started()
|
|
|
|
self.dispatcher(self.__process())
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def get_progress(self):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
return float(self.__fileobj.tell()) / self.__file_size
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def __process(self):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
offsets = self.offsets
|
2007-11-16 09:03:22 +00:00
|
|
|
levels = self.levels
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
dict_levels = {"T": debug_level_trace, "F": debug_level_fixme,
|
|
|
|
"L": debug_level_log, "D": debug_level_debug,
|
|
|
|
"I": debug_level_info, "W": debug_level_warning,
|
2018-05-13 12:02:11 +00:00
|
|
|
"E": debug_level_error, " ": debug_level_none,
|
|
|
|
"M": debug_level_memdump, }
|
2012-09-21 17:11:40 +00:00
|
|
|
ANSI = "(?:\x1b\\[[0-9;]*m)?"
|
2018-04-14 15:04:22 +00:00
|
|
|
ANSI_PATTERN = r"\d:\d\d:\d\d\.\d+ " + ANSI + \
|
|
|
|
r" *\d+" + ANSI + \
|
|
|
|
r" +0x[0-9a-f]+ +" + ANSI + \
|
2018-05-13 12:02:11 +00:00
|
|
|
r"([TFLDIEWM ])"
|
2016-09-28 18:38:55 +00:00
|
|
|
BARE_PATTERN = ANSI_PATTERN.replace(ANSI, "")
|
|
|
|
rexp_bare = re.compile(BARE_PATTERN)
|
|
|
|
rexp_ansi = re.compile(ANSI_PATTERN)
|
2012-09-21 20:15:07 +00:00
|
|
|
rexp = rexp_bare
|
2007-11-16 09:03:22 +00:00
|
|
|
|
2008-06-30 16:48:34 +00:00
|
|
|
# Moving attribute lookups out of the loop:
|
2007-11-14 08:30:19 +00:00
|
|
|
readline = self.__fileobj.readline
|
|
|
|
tell = self.__fileobj.tell
|
2008-06-30 16:48:34 +00:00
|
|
|
rexp_match = rexp.match
|
|
|
|
levels_append = levels.append
|
|
|
|
offsets_append = offsets.append
|
|
|
|
dict_levels_get = dict_levels.get
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
self.__fileobj.seek(0)
|
2007-11-14 08:30:19 +00:00
|
|
|
limit = self._lines_per_iteration
|
2011-11-16 19:37:21 +00:00
|
|
|
last_line = ""
|
2007-11-14 08:30:19 +00:00
|
|
|
i = 0
|
2016-09-28 18:38:55 +00:00
|
|
|
sort_helper = SortHelper(self.__fileobj, offsets)
|
2011-11-16 19:37:21 +00:00
|
|
|
find_insert_position = sort_helper.find_insert_position
|
2007-11-14 08:30:19 +00:00
|
|
|
while True:
|
2012-09-21 19:38:58 +00:00
|
|
|
i += 1
|
|
|
|
if i >= limit:
|
|
|
|
i = 0
|
|
|
|
yield True
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
offset = tell()
|
2018-04-20 01:13:03 +00:00
|
|
|
line = readline().decode('utf-8', errors='replace')
|
2007-11-14 08:30:19 +00:00
|
|
|
if not line:
|
|
|
|
break
|
2016-09-28 18:38:55 +00:00
|
|
|
match = rexp_match(line)
|
2008-06-29 18:14:07 +00:00
|
|
|
if match is None:
|
2018-04-14 15:04:22 +00:00
|
|
|
if rexp is rexp_ansi or "\x1b" not in line:
|
2012-09-21 20:15:07 +00:00
|
|
|
continue
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
match = rexp_ansi.match(line)
|
2012-09-21 20:15:07 +00:00
|
|
|
if match is None:
|
|
|
|
continue
|
|
|
|
# Switch to slower ANSI parsing:
|
|
|
|
rexp = rexp_ansi
|
|
|
|
rexp_match = rexp.match
|
2011-11-16 19:37:21 +00:00
|
|
|
|
|
|
|
# Timestamp is in the very beginning of the row, and can be sorted
|
|
|
|
# by lexical comparison. That's why we don't bother parsing the
|
|
|
|
# time to integer. We also don't have to take a substring here,
|
|
|
|
# which would be a useless memcpy.
|
|
|
|
if line >= last_line:
|
2016-09-28 18:38:55 +00:00
|
|
|
levels_append(
|
|
|
|
dict_levels_get(match.group(1), debug_level_none))
|
|
|
|
offsets_append(offset)
|
2011-11-16 19:37:21 +00:00
|
|
|
last_line = line
|
|
|
|
else:
|
2016-09-28 18:38:55 +00:00
|
|
|
pos = find_insert_position(line)
|
|
|
|
levels.insert(
|
|
|
|
pos, dict_levels_get(match.group(1), debug_level_none))
|
|
|
|
offsets.insert(pos, offset)
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
self.have_load_finished()
|
2007-11-14 08:30:19 +00:00
|
|
|
yield False
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
|
2007-11-19 13:27:16 +00:00
|
|
|
class LogLine (list):
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
_line_regex = default_log_line_regex()
|
2007-11-19 13:52:01 +00:00
|
|
|
|
2007-11-19 13:27:16 +00:00
|
|
|
@classmethod
|
2016-09-28 18:38:55 +00:00
|
|
|
def parse_full(cls, line_string):
|
2018-04-20 01:13:03 +00:00
|
|
|
match = cls._line_regex.match(line_string.decode('utf8', errors='replace'))
|
2007-11-19 13:27:16 +00:00
|
|
|
if match is None:
|
2016-09-28 18:38:55 +00:00
|
|
|
# raise ValueError ("not a valid log line (%r)" % (line_string,))
|
2008-06-29 18:14:07 +00:00
|
|
|
groups = [0, 0, 0, 0, "", "", 0, "", "", 0]
|
2016-09-28 18:38:55 +00:00
|
|
|
return cls(groups)
|
2008-06-29 18:14:07 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
line = cls(match.groups())
|
2008-06-29 18:14:07 +00:00
|
|
|
# Timestamp.
|
2016-09-28 18:38:55 +00:00
|
|
|
line[0] = parse_time(line[0])
|
2008-06-29 18:14:07 +00:00
|
|
|
# PID.
|
2016-09-28 18:38:55 +00:00
|
|
|
line[1] = int(line[1])
|
2008-06-29 18:14:07 +00:00
|
|
|
# Thread.
|
2018-04-14 13:22:11 +00:00
|
|
|
line[2] = int(line[2], 16)
|
2008-06-29 18:14:07 +00:00
|
|
|
# Level (this is handled in LineCache).
|
|
|
|
line[3] = 0
|
|
|
|
# Line.
|
2016-09-28 18:38:55 +00:00
|
|
|
line[6] = int(line[6])
|
2008-06-29 18:14:07 +00:00
|
|
|
# Message start offset.
|
2016-09-28 18:38:55 +00:00
|
|
|
line[9] = match.start(9 + 1)
|
2008-06-29 18:14:07 +00:00
|
|
|
|
|
|
|
for col_id in (4, # COL_CATEGORY
|
|
|
|
5, # COL_FILENAME
|
|
|
|
7, # COL_FUNCTION,
|
2016-09-28 18:38:55 +00:00
|
|
|
8,): # COL_OBJECT
|
2018-04-14 13:22:11 +00:00
|
|
|
line[col_id] = sys.intern(line[col_id] or "")
|
2008-06-29 18:14:07 +00:00
|
|
|
|
|
|
|
return line
|
2007-11-19 13:27:16 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
|
2007-11-21 08:47:40 +00:00
|
|
|
class LogLines (object):
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def __init__(self, fileobj, line_cache):
|
2007-11-21 08:47:40 +00:00
|
|
|
|
|
|
|
self.__fileobj = fileobj
|
|
|
|
self.__line_cache = line_cache
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def __len__(self):
|
2007-11-21 08:47:40 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
return len(self.__line_cache.offsets)
|
2007-11-21 08:47:40 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def __getitem__(self, line_index):
|
2007-11-21 08:47:40 +00:00
|
|
|
|
|
|
|
offset = self.__line_cache.offsets[line_index]
|
2016-09-28 18:38:55 +00:00
|
|
|
self.__fileobj.seek(offset)
|
|
|
|
line_string = self.__fileobj.readline()
|
|
|
|
line = LogLine.parse_full(line_string)
|
2007-11-21 08:47:40 +00:00
|
|
|
msg = line_string[line[-1]:]
|
|
|
|
line[-1] = msg
|
|
|
|
return line
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def __iter__(self):
|
2007-11-21 08:47:40 +00:00
|
|
|
|
2018-04-14 15:04:22 +00:00
|
|
|
size = len(self)
|
2007-11-21 08:47:40 +00:00
|
|
|
i = 0
|
2018-04-14 15:04:22 +00:00
|
|
|
while i < size:
|
2007-11-21 08:47:40 +00:00
|
|
|
yield self[i]
|
|
|
|
i += 1
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
|
2007-11-14 08:30:19 +00:00
|
|
|
class LogFile (Producer):
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def __init__(self, filename, dispatcher):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2007-12-03 13:45:09 +00:00
|
|
|
import mmap
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
Producer.__init__(self)
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
self.logger = logging.getLogger("logfile")
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
self.path = os.path.normpath(os.path.abspath(filename))
|
2018-04-14 13:22:11 +00:00
|
|
|
self.__real_fileobj = open(filename, "rb")
|
2016-09-28 18:38:55 +00:00
|
|
|
self.fileobj = mmap.mmap(
|
|
|
|
self.__real_fileobj.fileno(), 0, access=mmap.ACCESS_READ)
|
|
|
|
self.line_cache = LineCache(self.fileobj, dispatcher)
|
|
|
|
self.line_cache.consumers.append(self)
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def start_loading(self):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
self.logger.debug("starting load")
|
|
|
|
self.line_cache.start_loading()
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def get_load_progress(self):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
return self.line_cache.get_progress()
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def handle_load_started(self):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
# Chain up to our consumers:
|
2016-09-28 18:38:55 +00:00
|
|
|
self.have_load_started()
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def handle_load_finished(self):
|
|
|
|
self.logger.debug("finish loading")
|
|
|
|
self.lines = LogLines(self.fileobj, self.line_cache)
|
2007-11-21 08:47:40 +00:00
|
|
|
|
2007-11-14 08:30:19 +00:00
|
|
|
# Chain up to our consumers:
|
2016-09-28 18:38:55 +00:00
|
|
|
self.have_load_finished()
|