2018-07-04 15:39:44 +01:00
|
|
|
# Copyright 2018 ARM Limited
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
|
2017-11-23 17:39:54 +00:00
|
|
|
import logging
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from wa.utils.types import enum
|
|
|
|
|
|
|
|
|
|
|
|
LogcatLogLevel = enum(['verbose', 'debug', 'info', 'warn', 'error', 'assert'], start=2)
|
|
|
|
|
|
|
|
log_level_map = ''.join(n[0].upper() for n in LogcatLogLevel.names)
|
|
|
|
|
|
|
|
logger = logging.getLogger('logcat')
|
|
|
|
|
|
|
|
|
|
|
|
class LogcatEvent(object):
|
|
|
|
|
|
|
|
__slots__ = ['timestamp', 'pid', 'tid', 'level', 'tag', 'message']
|
|
|
|
|
|
|
|
def __init__(self, timestamp, pid, tid, level, tag, message):
|
|
|
|
self.timestamp = timestamp
|
|
|
|
self.pid = pid
|
2018-07-02 15:28:06 +01:00
|
|
|
self.tid = tid
|
2017-11-23 17:39:54 +00:00
|
|
|
self.level = level
|
|
|
|
self.tag = tag
|
|
|
|
self.message = message
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '{} {} {} {} {}: {}'.format(
|
|
|
|
self.timestamp, self.pid, self.tid,
|
|
|
|
self.level.name.upper(), self.tag,
|
|
|
|
self.message,
|
|
|
|
)
|
|
|
|
|
|
|
|
__str__ = __repr__
|
|
|
|
|
|
|
|
|
|
|
|
class LogcatParser(object):
|
|
|
|
|
|
|
|
def parse(self, filepath):
|
2020-04-02 17:33:48 +01:00
|
|
|
with open(filepath, errors='replace') as fh:
|
2017-11-23 17:39:54 +00:00
|
|
|
for line in fh:
|
|
|
|
event = self.parse_line(line)
|
|
|
|
if event:
|
|
|
|
yield event
|
|
|
|
|
2018-07-04 17:44:55 +01:00
|
|
|
def parse_line(self, line): # pylint: disable=no-self-use
|
2017-11-23 17:39:54 +00:00
|
|
|
line = line.strip()
|
2017-11-24 16:25:45 +00:00
|
|
|
if not line or line.startswith('-') or ': ' not in line:
|
2017-11-23 17:39:54 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
metadata, message = line.split(': ', 1)
|
|
|
|
|
|
|
|
parts = metadata.split(None, 5)
|
|
|
|
try:
|
|
|
|
ts = ' '.join([parts.pop(0), parts.pop(0)])
|
|
|
|
timestamp = datetime.strptime(ts, '%m-%d %H:%M:%S.%f').replace(year=datetime.now().year)
|
|
|
|
pid = int(parts.pop(0))
|
|
|
|
tid = int(parts.pop(0))
|
|
|
|
level = LogcatLogLevel.levels[log_level_map.index(parts.pop(0))]
|
|
|
|
tag = (parts.pop(0) if parts else '').strip()
|
2018-07-09 15:28:22 +01:00
|
|
|
except Exception as e: # pylint: disable=broad-except
|
2017-11-23 17:39:54 +00:00
|
|
|
message = 'Invalid metadata for line:\n\t{}\n\tgot: "{}"'
|
|
|
|
logger.warning(message.format(line, e))
|
|
|
|
return None
|
|
|
|
|
|
|
|
return LogcatEvent(timestamp, pid, tid, level, tag, message)
|