Skip to content

Commit

Permalink
Configuration: move logging to separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Weiss committed Mar 1, 2024
1 parent b8eefa0 commit 5742033
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 30 deletions.
33 changes: 3 additions & 30 deletions ocgraph/configuration/configuration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env python
# SPDX-License-Identifier: GTDGmbH
"""Module for configuration of the ocgraph package."""
import logging

from .logger import OcctreLogger, preset_logging

from .architecture.architecture import Architecture
from .architecture.x86 import X86Architecture
Expand Down Expand Up @@ -51,26 +52,6 @@
},
}

preset_logging: dict[str, dict] = {
"development": {
"file_log": "debug.log",
"file_level": logging.DEBUG,
"console_log": True,
"console_level": logging.DEBUG,
},
"module": {
"file_log": None,
"file_level": logging.ERROR,
"console_log": False,
"console_level": logging.ERROR,
},
"default": {
"file_log": "asm2cfg.log",
"file_level": logging.INFO,
"console_log": True,
"console_level": logging.INFO,
},
}
# fmt: on


Expand All @@ -93,15 +74,7 @@ def __init__(
self.__dict__ = _preset

# configure logging
log_config = preset_logging.get(logging_preset)
if log_config["file_log"]:
file_stream: logging.FileHandler = logging.FileHandler(log_config["file_log"])
file_stream.setLevel(log_config["file_level"])
self.logger.addHandler(file_stream)
if log_config["console_log"]:
console_stream: logging.StreamHandler = logging.StreamHandler()
console_stream.setLevel(log_config["console_level"])
self.logger.addHandler(console_stream)
self.logger = OCGraphLogger("OcGraph", logging_preset, "asm2cfg.log")

@staticmethod
def architectures():
Expand Down
47 changes: 47 additions & 0 deletions ocgraph/configuration/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python
# SPDX-License-Identifier: GTDGmbH
# Copyright 2024 by GTD GmbH.
"""Class configuring the OCGraph logging."""
import logging

# fmt: off
logging_preset: dict[str, dict] = {
"development": {
"file_log": True,
"file_level": logging.DEBUG,
"console_log": True,
"console_level": logging.DEBUG
},

"module": {
"file_log": False,
"file_level": logging.ERROR,
"console_log": False,
"console_level": logging.ERROR
},

"default": {
"file_log": True,
"file_level": logging.INFO,
"console_log": True,
"console_level": logging.INFO
},
}
# fmt: on


class OCGraphLogger(logging.Logger):
"""Logging mechanism for module"""

def __init__(self, name: str, preset="default", file=""):
super().__init__(name)
log_config = logging_preset.get(preset)
if log_config["file_log"]:
logging_file = file + ".log"
file_stream: logging.FileHandler = logging.FileHandler(logging_file)
file_stream.setLevel(log_config["file_level"])
self.addHandler(file_stream)
if log_config["console_log"]:
console_stream: logging.StreamHandler = logging.StreamHandler()
console_stream.setLevel(log_config["console_level"])
self.addHandler(console_stream)

0 comments on commit 5742033

Please sign in to comment.