Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick fixes on parsers and logging #129

Merged
merged 4 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/sysdiagnose/parsers/accessibility_tcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import glob
import os
from sysdiagnose.utils.base import BaseParserInterface
from sysdiagnose.utils.base import BaseParserInterface, logger
from sysdiagnose.utils.apollo import Apollo


Expand All @@ -31,7 +31,7 @@ def execute(self) -> list | dict:
# only one file to parse
try:
result = []
apollo = Apollo(os_version='yolo') # FIXME get right OS version, but also update the Apollo modules to be aware of the latest OS versions
apollo = Apollo(logger=logger, os_version='yolo') # FIXME get right OS version, but also update the Apollo modules to be aware of the latest OS versions
for logfile in self.get_log_files():
result.extend(apollo.parse_db(db_fname=logfile, db_type='TCC.db'))
return result
Expand Down
4 changes: 2 additions & 2 deletions src/sysdiagnose/parsers/powerlogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import glob
import os
from sysdiagnose.utils.base import BaseParserInterface
from sysdiagnose.utils.base import BaseParserInterface, logger
from sysdiagnose.utils.apollo import Apollo


Expand Down Expand Up @@ -34,7 +34,7 @@ def get_log_files(self) -> list:

def execute(self) -> list:
result = []
apollo = Apollo(os_version='yolo') # FIXME get right OS version, but also update the Apollo modules to be aware of the latest OS versions
apollo = Apollo(logger=logger, os_version='yolo') # FIXME get right OS version, but also update the Apollo modules to be aware of the latest OS versions
for logfile in self.get_log_files():
result.extend(apollo.parse_db(db_fname=logfile, db_type='CurrentPowerlog.PLSQL'))

Expand Down
15 changes: 11 additions & 4 deletions src/sysdiagnose/parsers/spindumpnosymbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ def parse_file(path: str) -> list:

# stripping
for line in f_in:
if line.strip() == "" or line.strip() == "Heavy format: stacks are sorted by count" or line.strip() == "Use -i and -timeline to re-report with chronological sorting":
if line.strip() == "No samples":
status = 'empty'
# Since the rest is just 'binary format', we ignore the rest of the file.
break
elif line.strip() == "" or line.strip() == "Heavy format: stacks are sorted by count" or line.strip() == "Use -i and -timeline to re-report with chronological sorting":
continue
elif line.strip() == "------------------------------------------------------------":
status = 'processes_raw'
Expand All @@ -62,9 +66,12 @@ def parse_file(path: str) -> list:

# call parsing function per section
events = []
basic = SpindumpNoSymbolsParser.parse_basic(headers)
events.append(basic)
events.extend(SpindumpNoSymbolsParser.parse_processes(processes_raw, start_timestamp=basic['timestamp']))
if status != 'empty':
basic = SpindumpNoSymbolsParser.parse_basic(headers)
events.append(basic)
events.extend(SpindumpNoSymbolsParser.parse_processes(processes_raw, start_timestamp=basic['timestamp']))
# Logging
logger.debug(f"{len(events)} events retrieved", extra={'num_events': len(events)})

return events

Expand Down
19 changes: 13 additions & 6 deletions src/sysdiagnose/utils/apollo.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,21 @@
import re
from datetime import datetime, timezone
import glob
import logging

default_mod_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'apollo_modules')


class Apollo():
def __init__(self, mod_dir: str = default_mod_dir, os_version: str = 'yolo'):
def __init__(self, logger: logging.Logger, mod_dir: str = default_mod_dir, os_version: str = 'yolo'):
"""
Initialize the Apollo class for parsing databases

Args:
mod_dir (str): The directory where the module definitions are stored
os_version (str): The version of the OS for which to parse the modules. 'yolo' means all versions.
"""
self.logger = logger
self.os_version = os_version
self.mod_dir = mod_dir

Expand Down Expand Up @@ -125,7 +127,7 @@ def parse_db(self, db_fname: str, db_type: str = None) -> list:
try:
module_queries = self.modules[db_type]
except KeyError:
print(f"No modules with queries for {db_type}.")
self.logger.exception(f"No modules with queries for {db_type}.")
return results

# establish db connection
Expand All @@ -140,11 +142,14 @@ def parse_db(self, db_fname: str, db_type: str = None) -> list:
cur.execute(module_query['sql'])
rows = cur.fetchall()
except Exception:
print(f"WARNING: Cannot fetch query contents for query with name: {module_query['name']}.")
self.logger.warning(
f"WARNING: Cannot fetch query contents for query with name: {module_query['name']}.",
extra={"apollo_module": module_query['name']}, exc_info=True)
continue

if not rows:
print(f"No Records Found for {module_query['name']}.")
self.logger.info(f"No Records Found for {module_query['name']}.",
extra={"apollo_module": module_query['name']})
continue

headers = []
Expand All @@ -164,7 +169,9 @@ def parse_db(self, db_fname: str, db_type: str = None) -> list:
results.append(item)
except TypeError:
# problem with timestamp parsing
print(f"WARNING: Problem with timestamp parsing for table {db_fname}, row {list(row)}")
self.logger.warning(f"WARNING: Problem with timestamp parsing for table {db_fname}, row {list(row)}",
extra={"apollo_module": module_query['name'], "table": db_fname, "row": list(row)},
exc_info=True)

print("Executing module on: " + db_fname)
self.logger.info("Executing module on: " + db_fname, extra={"apollo_module": module_query['name'], "table": db_fname})
return results
8 changes: 4 additions & 4 deletions src/sysdiagnose/utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from datetime import datetime

logger = logging.getLogger('sysdiagnose')
# By default, we want to have the possibility to log almost everything.
logger.setLevel(logging.INFO)
# By default, we want to have the possibility to log everything.
logger.setLevel(logging.DEBUG)


class SysdiagnoseJsonFormatter(jsonlogger.JsonFormatter):
Expand All @@ -31,13 +31,13 @@ def get_console_handler(level: str) -> logging.StreamHandler:
return ch


def get_json_handler(filename: str, level: int = logging.INFO) -> logging.FileHandler:
def get_json_handler(filename: str, level: int = logging.DEBUG) -> logging.FileHandler:
'''
Creates a logging JSON format file handler.

Args:
filename: Filename where to log.
level: Logging level. By default to INFO. https://docs.python.org/3/library/logging.html#logging-levels
level: Logging level. By default to DEBUG. https://docs.python.org/3/library/logging.html#logging-levels
'''
fmt_json = SysdiagnoseJsonFormatter(
fmt='%(created)f %(asctime)s %(levelname)s %(module)s %(message)s',
Expand Down
Loading