Skip to content
Snippets Groups Projects
Commit b0f6bbf8 authored by DEBREUVE Eric's avatar DEBREUVE Eric
Browse files

added memory usage logging

parent 2d702e7f
No related branches found
No related tags found
No related merge requests found
......@@ -35,5 +35,6 @@ from logger_36.main import (
ElapsedTime,
LogSystemDetails,
SaveLOGasHTML,
SetShowMemoryUsage,
)
from logger_36.version import __version__
......@@ -42,6 +42,22 @@ from rich.console import Console as console_t
from rich.style import Style as style_t
from rich.text import Text as text_t
try:
from psutil import Process as process_t
_PROCESS = process_t()
_GIGA_SCALING = 1.0 / (1024**3)
def UsedMemoryInGb() -> float:
""""""
return round(_GIGA_SCALING * _PROCESS.memory_info().rss, 1)
except ModuleNotFoundError:
_PROCESS = None
def UsedMemoryInGb() -> float:
return 0.0
# This module is certainly imported early. Therefore, the current time should be close enough to the real start time.
_START_TIME = time.time()
......@@ -90,16 +106,16 @@ class color_handler_t(lggg.Handler):
formatter: lggg.Formatter = None
console: console_t = None
show_memory_usage: bool = False
def __init__(self, /, *, level=lggg.NOTSET) -> None:
def __init__(self) -> None:
""""""
super().__init__(level=level)
super().__init__(level=lggg.NOTSET)
self.formatter = lggg.Formatter(fmt=_MESSAGE_FORMAT, datefmt=_DATE_FORMAT)
self.console = console_t(
record=True, force_terminal=True, width=1000, tab_size=5
)
self.setFormatter(self.formatter)
def emit(self, record: lggg.LogRecord, /) -> None:
......@@ -107,14 +123,18 @@ class color_handler_t(lggg.Handler):
cls = self.__class__
formatted = self.formatter.format(record)
if self.show_memory_usage:
memory_usage = f" +mem={UsedMemoryInGb()}"
else:
memory_usage = ""
if "\n" in formatted:
lines = formatted.splitlines()
highlighted = text_t(lines[0])
highlighted.append(f" +{ElapsedTime()}\n", style="green")
highlighted.append(f" +{ElapsedTime()}{memory_usage}\n", style="green")
highlighted.append(" " + "\n ".join(lines[1:]))
else:
highlighted = text_t(formatted)
highlighted.append(f" +{ElapsedTime()}", style="green")
highlighted.append(f" +{ElapsedTime()}{memory_usage}", style="green")
highlighted.stylize("dodger_blue2", end=19)
highlighted.highlight_words(
......@@ -135,6 +155,17 @@ LOGGER.setLevel(lggg.INFO) # Minimum desired level
LOGGER.addHandler(color_handler_t())
def SetShowMemoryUsage(show_memory_usage: bool, /) -> None:
""""""
if show_memory_usage and (_PROCESS is None):
LOGGER.warning('Cannot show memory usage: Package "psutil" not installed')
return
for handler in LOGGER.handlers:
if hasattr(handler, "show_memory_usage"):
handler.show_memory_usage = show_memory_usage
def AddFileHandler(file: Union[str, path_t], /) -> None:
""""""
if file.exists():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment