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

code factorization/simplification + fixed mem usage units + max mem usage

parent d1717408
No related branches found
No related tags found
No related merge requests found
...@@ -34,6 +34,7 @@ from logger_36.main import ( ...@@ -34,6 +34,7 @@ from logger_36.main import (
AddFileHandler, AddFileHandler,
ElapsedTime, ElapsedTime,
LogSystemDetails, LogSystemDetails,
MaximumMemoryUsage,
SaveLOGasHTML, SaveLOGasHTML,
SetShowMemoryUsage, SetShowMemoryUsage,
) )
......
...@@ -46,11 +46,11 @@ try: ...@@ -46,11 +46,11 @@ try:
from psutil import Process as process_t from psutil import Process as process_t
_PROCESS = process_t() _PROCESS = process_t()
_MEGA_SCALING = 1.0 / (1024.0**2) _MEGA_UNIT = 1024.0**2
_GIGA_SCALING = _MEGA_SCALING / 1024.0 _GIGA_UNIT = _MEGA_UNIT * 1024.0
except ModuleNotFoundError: except ModuleNotFoundError:
_PROCESS = None _PROCESS = None
_MEGA_SCALING = _GIGA_SCALING = 0.0 _MEGA_UNIT = _GIGA_UNIT = 0.0
# This module is certainly imported early. Therefore, the current time should be close enough to the real start time. # This module is certainly imported early. Therefore, the current time should be close enough to the real start time.
...@@ -64,7 +64,8 @@ _WHERE_SEPARATOR = "@" ...@@ -64,7 +64,8 @@ _WHERE_SEPARATOR = "@"
_MESSAGE_FORMAT = ( _MESSAGE_FORMAT = (
f"%(asctime)s[%(levelname)s]\t- " f"%(asctime)s[%(levelname)s]\t- "
f"%(message)s {_WHERE_SEPARATOR} " f"%(message)s {_WHERE_SEPARATOR} "
f"%(module)s:%(funcName)s:%(lineno)d" f"%(module)s:%(funcName)s:%(lineno)d "
f"+%(elapsed_time)s%(memory_usage)s"
) )
_NEXT_LINE_PROLOGUE = "\n" + (27 + _TAB_SIZE) * " " _NEXT_LINE_PROLOGUE = "\n" + (27 + _TAB_SIZE) * " "
_DATE_FORMAT = "%Y-%m-%d@%H:%M:%S" _DATE_FORMAT = "%Y-%m-%d@%H:%M:%S"
...@@ -95,18 +96,18 @@ _MAX_DETAIL_NAME_LENGTH = max(map(len, _SYSTEM_DETAILS.keys())) ...@@ -95,18 +96,18 @@ _MAX_DETAIL_NAME_LENGTH = max(map(len, _SYSTEM_DETAILS.keys()))
class _handler_extension: class _handler_extension:
__slots__ = ("formatter", "show_memory_usage") __slots__ = ("formatter", "show_memory_usage", "max_memory_usage")
formatter: lggg.Formatter formatter: lggg.Formatter
show_memory_usage: bool show_memory_usage: bool
max_memory_usage: int
def __init__(self) -> None: def __init__(self) -> None:
"""""" """"""
self.formatter = lggg.Formatter(fmt=_MESSAGE_FORMAT, datefmt=_DATE_FORMAT) self.formatter = lggg.Formatter(fmt=_MESSAGE_FORMAT, datefmt=_DATE_FORMAT)
self.show_memory_usage = False self.show_memory_usage = False
self.max_memory_usage = 0
def FormattedMessage( def FormattedMessage(self, record: lggg.LogRecord, /) -> tuple[str, str | None]:
self, record: lggg.LogRecord, /
) -> tuple[str, list[str] | None]:
""" """
Note: "message" is not yet an attribute of record (it will be set by format()); Use "msg" instead. Note: "message" is not yet an attribute of record (it will be set by format()); Use "msg" instead.
""" """
...@@ -116,8 +117,16 @@ class _handler_extension: ...@@ -116,8 +117,16 @@ class _handler_extension:
original_message = record.msg original_message = record.msg
lines = original_message.splitlines() lines = original_message.splitlines()
record.msg = lines[0] record.msg = lines[0]
next_lines = _NEXT_LINE_PROLOGUE.join(lines[1:])
next_lines = f"{_NEXT_LINE_PROLOGUE}{next_lines}"
else:
original_message = next_lines = None
record.elapsed_time = ElapsedTime()
if self.show_memory_usage:
record.memory_usage = self.MemoryUsage()
else: else:
original_message = lines = None record.memory_usage = ""
formatted = self.formatter.format(record) formatted = self.formatter.format(record)
...@@ -125,16 +134,19 @@ class _handler_extension: ...@@ -125,16 +134,19 @@ class _handler_extension:
if original_message is not None: if original_message is not None:
record.msg = original_message record.msg = original_message
return formatted, lines return formatted, next_lines
def MemoryUsage(self) -> str: def MemoryUsage(self) -> str:
"""""" """"""
if self.show_memory_usage: if self.show_memory_usage:
usage = _PROCESS.memory_info().rss usage = _PROCESS.memory_info().rss
if usage > _GIGA_SCALING: if usage > self.max_memory_usage:
return f" :{round(_GIGA_SCALING * usage, 1)}GB" self.max_memory_usage = usage
if usage > _GIGA_UNIT:
return f" :{round(usage / _GIGA_UNIT, 1)}GB"
else: else:
return f" :{round(_MEGA_SCALING * usage, 1)}MB" return f" :{round(usage / _MEGA_UNIT, 1)}MB"
return "" return ""
...@@ -167,25 +179,24 @@ class console_handler_t(lggg.Handler, _handler_extension): ...@@ -167,25 +179,24 @@ class console_handler_t(lggg.Handler, _handler_extension):
"""""" """"""
cls = self.__class__ cls = self.__class__
formatted, lines = self.FormattedMessage(record) formatted, next_lines = self.FormattedMessage(record)
highlighted = text_t(formatted) highlighted = text_t(formatted)
highlighted.append(f" +{ElapsedTime()}{self.MemoryUsage()}", style="green")
if lines is not None:
highlighted.append(
_NEXT_LINE_PROLOGUE + _NEXT_LINE_PROLOGUE.join(lines[1:])
)
highlighted.stylize("dodger_blue2", end=19) highlighted.stylize("dodger_blue2", end=19)
highlighted.highlight_words( highlighted.highlight_words(
(f"[{record.levelname}]",), style=cls._LEVEL_COLOR[record.levelno] (f"[{record.levelname}]",), style=cls._LEVEL_COLOR[record.levelno]
) )
highlighted.highlight_regex( highlighted.highlight_words(
f"{_WHERE_SEPARATOR} {record.module}:{record.funcName}:{record.lineno}", (f"{_WHERE_SEPARATOR} {record.module}:{record.funcName}:{record.lineno}",),
style=cls._GRAY_STYLE, style=cls._GRAY_STYLE,
) )
highlighted.highlight_regex(cls._ACTUAL, style="red") highlighted.highlight_regex(f"\+{record.elapsed_time}.*$", style="green")
highlighted.highlight_regex(cls._EXPECTED, style="green")
if next_lines is not None:
highlighted.append(next_lines)
highlighted.highlight_words((cls._ACTUAL,), style="red")
highlighted.highlight_words((cls._EXPECTED,), style="green")
self.console.print(highlighted) self.console.print(highlighted)
...@@ -200,14 +211,11 @@ class file_handler_t(lggg.FileHandler, _handler_extension): ...@@ -200,14 +211,11 @@ class file_handler_t(lggg.FileHandler, _handler_extension):
def emit(self, record: lggg.LogRecord, /) -> None: def emit(self, record: lggg.LogRecord, /) -> None:
"""""" """"""
formatted, lines = self.FormattedMessage(record) formatted, next_lines = self.FormattedMessage(record)
formatted = formatted.replace("\t", (9 - record.levelname.__len__()) * " ")
if lines is None: message = formatted.replace("\t", (9 - record.levelname.__len__()) * " ")
message = f"{formatted} +{ElapsedTime()}{self.MemoryUsage()}" if next_lines is not None:
else: message = f"{message}{next_lines}"
next_lines = _NEXT_LINE_PROLOGUE.join(lines[1:])
message = f"{formatted} +{ElapsedTime()}{self.MemoryUsage()}{_NEXT_LINE_PROLOGUE}{next_lines}"
print(message, file=self.stream) print(message, file=self.stream)
self.stream.flush() self.stream.flush()
...@@ -315,3 +323,8 @@ def ElapsedTime() -> str: ...@@ -315,3 +323,8 @@ def ElapsedTime() -> str:
output = output.split(maxsplit=1)[-1] output = output.split(maxsplit=1)[-1]
return output return output
def MaximumMemoryUsage() -> int:
""""""
return max(_hdr.max_memory_usage for _hdr in LOGGER.handlers)
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
from pathlib import Path as path_t from pathlib import Path as path_t
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
from logger_36 import LOGGER, AddFileHandler from logger_36 import LOGGER, AddFileHandler, MaximumMemoryUsage, SetShowMemoryUsage
with TemporaryDirectory() as tmp_folder: with TemporaryDirectory() as tmp_folder:
...@@ -40,11 +40,14 @@ with TemporaryDirectory() as tmp_folder: ...@@ -40,11 +40,14 @@ with TemporaryDirectory() as tmp_folder:
tmp_file = tmp_folder / "log.txt" tmp_file = tmp_folder / "log.txt"
AddFileHandler(tmp_file) AddFileHandler(tmp_file)
SetShowMemoryUsage(True)
for level in ("debug", "info", "warning", "error", "critical"): for level in ("debug", "info", "warning", "error", "critical"):
LogMessage = getattr(LOGGER, level) LogMessage = getattr(LOGGER, level)
LogMessage(f"{level.capitalize()} message") LogMessage(f"{level.capitalize()} message")
LogMessage(f"Multi-line\n{level.capitalize()}\nmessage") LogMessage(f"Multi-line\n{level.capitalize()}\nmessage")
LOGGER.info(f"Max memory usage: {MaximumMemoryUsage()}")
content = open(tmp_file, "r").read() content = open(tmp_file, "r").read()
print(f"\n{content}") print(f"\n{content}")
...@@ -29,4 +29,4 @@ ...@@ -29,4 +29,4 @@
# The fact that you are presently reading this means that you have had # The fact that you are presently reading this means that you have had
# knowledge of the CeCILL license and that you accept its terms. # knowledge of the CeCILL license and that you accept its terms.
__version__ = "2023.6" __version__ = "2023.7"
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