From 709083acf1a143514bc64bed837527789c8bb984 Mon Sep 17 00:00:00 2001 From: DEBREUVE Eric <eric.debreuve@cnrs.fr> Date: Thu, 4 Jul 2024 11:26:42 +0200 Subject: [PATCH] refactoring --- package/logger_36/__init__.py | 7 -- .../catalog/{logging => logger}/chronos.py | 0 .../catalog/{logging => logger}/exception.py | 0 .../catalog/{logging => logger}/gpu.py | 0 .../catalog/{logging => logger}/memory.py | 0 .../catalog/{logging => logger}/system.py | 0 package/logger_36/config/logger.py | 103 ++++++++++++++++++ package/logger_36/format.py | 59 ++++++++++ package/logger_36/{main.py => handler.py} | 43 -------- package/logger_36/logger.py | 56 ++++++++++ package/logger_36/logger_gpu.py | 53 +++++++++ package/logger_36/measure.py | 55 ++++++++++ package/logger_36/storage.py | 53 +++++++++ package/logger_36/task/measure/memory.py | 2 +- package/logger_36/type/handler.py | 4 +- package/logger_36/version.py | 2 +- test/main.py | 8 +- 17 files changed, 387 insertions(+), 58 deletions(-) rename package/logger_36/catalog/{logging => logger}/chronos.py (100%) rename package/logger_36/catalog/{logging => logger}/exception.py (100%) rename package/logger_36/catalog/{logging => logger}/gpu.py (100%) rename package/logger_36/catalog/{logging => logger}/memory.py (100%) rename package/logger_36/catalog/{logging => logger}/system.py (100%) create mode 100644 package/logger_36/config/logger.py create mode 100644 package/logger_36/format.py rename package/logger_36/{main.py => handler.py} (83%) create mode 100644 package/logger_36/logger.py create mode 100644 package/logger_36/logger_gpu.py create mode 100644 package/logger_36/measure.py create mode 100644 package/logger_36/storage.py diff --git a/package/logger_36/__init__.py b/package/logger_36/__init__.py index 0cbaeaf..6caf26b 100644 --- a/package/logger_36/__init__.py +++ b/package/logger_36/__init__.py @@ -5,13 +5,6 @@ SEE COPYRIGHT NOTICE BELOW """ from logger_36.instance.logger import LOGGER -from logger_36.main import ( - AddConsoleHandler, - AddFileHandler, - AddGenericHandler, - AddRichConsoleHandler, -) -from logger_36.task.format.message import FormattedMessage from logger_36.type.logger import logger_t from logger_36.version import __version__ diff --git a/package/logger_36/catalog/logging/chronos.py b/package/logger_36/catalog/logger/chronos.py similarity index 100% rename from package/logger_36/catalog/logging/chronos.py rename to package/logger_36/catalog/logger/chronos.py diff --git a/package/logger_36/catalog/logging/exception.py b/package/logger_36/catalog/logger/exception.py similarity index 100% rename from package/logger_36/catalog/logging/exception.py rename to package/logger_36/catalog/logger/exception.py diff --git a/package/logger_36/catalog/logging/gpu.py b/package/logger_36/catalog/logger/gpu.py similarity index 100% rename from package/logger_36/catalog/logging/gpu.py rename to package/logger_36/catalog/logger/gpu.py diff --git a/package/logger_36/catalog/logging/memory.py b/package/logger_36/catalog/logger/memory.py similarity index 100% rename from package/logger_36/catalog/logging/memory.py rename to package/logger_36/catalog/logger/memory.py diff --git a/package/logger_36/catalog/logging/system.py b/package/logger_36/catalog/logger/system.py similarity index 100% rename from package/logger_36/catalog/logging/system.py rename to package/logger_36/catalog/logger/system.py diff --git a/package/logger_36/config/logger.py b/package/logger_36/config/logger.py new file mode 100644 index 0000000..e0f7558 --- /dev/null +++ b/package/logger_36/config/logger.py @@ -0,0 +1,103 @@ +""" +Copyright CNRS/Inria/UniCA +Contributor(s): Eric Debreuve (eric.debreuve@cnrs.fr) since 2023 +SEE COPYRIGHT NOTICE BELOW +""" + +import logging as lggg + +from logger_36.catalog.handler.console import console_handler_t +from logger_36.catalog.handler.console_rich import console_rich_handler_t +from logger_36.catalog.handler.file import file_handler_t +from logger_36.catalog.handler.generic import generic_handler_t +from logger_36.constant.handler import HANDLER_CODES, handler_codes_h +from logger_36.instance.logger import LOGGER +from logger_36.task.format.message import FormattedMessage + + +def SetLOGLevel( + level: int, + /, + *, + logger: lggg.Logger | None = None, + which: handler_codes_h | str = "a", +) -> None: + """ + which: g=generic, c=console, f=file, a=all, str=name. + """ + if logger is None: + logger = LOGGER + + which_is_name = which not in HANDLER_CODES + found = False + for handler in logger.handlers: + if ( + (which == "a") + or ((which == "g") and isinstance(handler, generic_handler_t)) + or ( + (which == "c") + and isinstance(handler, (console_handler_t, console_rich_handler_t)) + ) + or ((which == "f") and isinstance(handler, file_handler_t)) + or (which == handler.name) + ): + handler.setLevel(level) + if which_is_name: + return + found = True + + if not found: + raise ValueError( + FormattedMessage( + "Handler not found", + actual=which, + expected=f"{str(HANDLER_CODES)[1:-1]}, or a handler name", + ) + ) + + +""" +COPYRIGHT NOTICE + +This software is governed by the CeCILL license under French law and +abiding by the rules of distribution of free software. You can use, +modify and/ or redistribute the software under the terms of the CeCILL +license as circulated by CEA, CNRS and INRIA at the following URL +"http://www.cecill.info". + +As a counterpart to the access to the source code and rights to copy, +modify and redistribute granted by the license, users are provided only +with a limited warranty and the software's author, the holder of the +economic rights, and the successive licensors have only limited +liability. + +In this respect, the user's attention is drawn to the risks associated +with loading, using, modifying and/or developing or reproducing the +software by the user in light of its specific status of free software, +that may mean that it is complicated to manipulate, and that also +therefore means that it is reserved for developers and experienced +professionals having in-depth computer knowledge. Users are therefore +encouraged to load and test the software's suitability as regards their +requirements in conditions enabling the security of their systems and/or +data to be ensured and, more generally, to use and operate it in the +same conditions as regards security. + +The fact that you are presently reading this means that you have had +knowledge of the CeCILL license and that you accept its terms. + +SEE LICENCE NOTICE: file README-LICENCE-utf8.txt at project source root. + +This software is being developed by Eric Debreuve, a CNRS employee and +member of team Morpheme. +Team Morpheme is a joint team between Inria, CNRS, and UniCA. +It is hosted by the Centre Inria d'Université Côte d'Azur, Laboratory +I3S, and Laboratory iBV. + +CNRS: https://www.cnrs.fr/index.php/en +Inria: https://www.inria.fr/en/ +UniCA: https://univ-cotedazur.eu/ +Centre Inria d'Université Côte d'Azur: https://www.inria.fr/en/centre/sophia/ +I3S: https://www.i3s.unice.fr/en/ +iBV: http://ibv.unice.fr/ +Team Morpheme: https://team.inria.fr/morpheme/ +""" diff --git a/package/logger_36/format.py b/package/logger_36/format.py new file mode 100644 index 0000000..c6ba7ee --- /dev/null +++ b/package/logger_36/format.py @@ -0,0 +1,59 @@ +""" +Copyright CNRS/Inria/UniCA +Contributor(s): Eric Debreuve (eric.debreuve@cnrs.fr) since 2023 +SEE COPYRIGHT NOTICE BELOW +""" + +from logger_36.task.format.memory import FormattedUsage as FormattedMemoryUsage +from logger_36.task.format.memory import ( + FormattedUsageWithAutoUnit as FormattedMemoryUsageWithAutoUnit, +) +from logger_36.task.format.memory import UsageBar as MemoryUsageBar +from logger_36.task.format.message import FormattedMessage +from logger_36.task.format.rule import Rule, RuleAsText + +""" +COPYRIGHT NOTICE + +This software is governed by the CeCILL license under French law and +abiding by the rules of distribution of free software. You can use, +modify and/ or redistribute the software under the terms of the CeCILL +license as circulated by CEA, CNRS and INRIA at the following URL +"http://www.cecill.info". + +As a counterpart to the access to the source code and rights to copy, +modify and redistribute granted by the license, users are provided only +with a limited warranty and the software's author, the holder of the +economic rights, and the successive licensors have only limited +liability. + +In this respect, the user's attention is drawn to the risks associated +with loading, using, modifying and/or developing or reproducing the +software by the user in light of its specific status of free software, +that may mean that it is complicated to manipulate, and that also +therefore means that it is reserved for developers and experienced +professionals having in-depth computer knowledge. Users are therefore +encouraged to load and test the software's suitability as regards their +requirements in conditions enabling the security of their systems and/or +data to be ensured and, more generally, to use and operate it in the +same conditions as regards security. + +The fact that you are presently reading this means that you have had +knowledge of the CeCILL license and that you accept its terms. + +SEE LICENCE NOTICE: file README-LICENCE-utf8.txt at project source root. + +This software is being developed by Eric Debreuve, a CNRS employee and +member of team Morpheme. +Team Morpheme is a joint team between Inria, CNRS, and UniCA. +It is hosted by the Centre Inria d'Université Côte d'Azur, Laboratory +I3S, and Laboratory iBV. + +CNRS: https://www.cnrs.fr/index.php/en +Inria: https://www.inria.fr/en/ +UniCA: https://univ-cotedazur.eu/ +Centre Inria d'Université Côte d'Azur: https://www.inria.fr/en/centre/sophia/ +I3S: https://www.i3s.unice.fr/en/ +iBV: http://ibv.unice.fr/ +Team Morpheme: https://team.inria.fr/morpheme/ +""" diff --git a/package/logger_36/main.py b/package/logger_36/handler.py similarity index 83% rename from package/logger_36/main.py rename to package/logger_36/handler.py index 91317f3..ad1af80 100644 --- a/package/logger_36/main.py +++ b/package/logger_36/handler.py @@ -12,9 +12,7 @@ from logger_36.catalog.handler.console import console_handler_t from logger_36.catalog.handler.file import file_handler_t from logger_36.catalog.handler.generic import generic_handler_t, interface_h from logger_36.constant.error import MISSING_RICH_ERROR -from logger_36.constant.handler import HANDLER_CODES, handler_codes_h from logger_36.instance.logger import LOGGER -from logger_36.task.format.message import FormattedMessage try: from logger_36.catalog.handler.console_rich import console_rich_handler_t @@ -164,47 +162,6 @@ def AddFileHandler( logger.AddHandler(handler, should_hold_messages) -def SetLOGLevel( - level: int, - /, - *, - logger: lggg.Logger | None = None, - which: handler_codes_h | str = "a", -) -> None: - """ - which: g=generic, c=console, f=file, a=all, str=name. - """ - if logger is None: - logger = LOGGER - - which_is_name = which not in HANDLER_CODES - found = False - for handler in logger.handlers: - if ( - (which == "a") - or ((which == "g") and isinstance(handler, generic_handler_t)) - or ( - (which == "c") - and isinstance(handler, (console_handler_t, console_rich_handler_t)) - ) - or ((which == "f") and isinstance(handler, file_handler_t)) - or (which == handler.name) - ): - handler.setLevel(level) - if which_is_name: - return - found = True - - if not found: - raise ValueError( - FormattedMessage( - "Handler not found", - actual=which, - expected=f"{str(HANDLER_CODES)[1:-1]}, or a handler name", - ) - ) - - """ COPYRIGHT NOTICE diff --git a/package/logger_36/logger.py b/package/logger_36/logger.py new file mode 100644 index 0000000..5f69ead --- /dev/null +++ b/package/logger_36/logger.py @@ -0,0 +1,56 @@ +""" +Copyright CNRS/Inria/UniCA +Contributor(s): Eric Debreuve (eric.debreuve@cnrs.fr) since 2023 +SEE COPYRIGHT NOTICE BELOW +""" + +from logger_36.catalog.logger.chronos import LogElapsedTime +from logger_36.catalog.logger.exception import LogException +from logger_36.catalog.logger.memory import LogMaximumMemoryUsage, LogMemoryUsages +from logger_36.catalog.logger.system import LogSystemDetails + +""" +COPYRIGHT NOTICE + +This software is governed by the CeCILL license under French law and +abiding by the rules of distribution of free software. You can use, +modify and/ or redistribute the software under the terms of the CeCILL +license as circulated by CEA, CNRS and INRIA at the following URL +"http://www.cecill.info". + +As a counterpart to the access to the source code and rights to copy, +modify and redistribute granted by the license, users are provided only +with a limited warranty and the software's author, the holder of the +economic rights, and the successive licensors have only limited +liability. + +In this respect, the user's attention is drawn to the risks associated +with loading, using, modifying and/or developing or reproducing the +software by the user in light of its specific status of free software, +that may mean that it is complicated to manipulate, and that also +therefore means that it is reserved for developers and experienced +professionals having in-depth computer knowledge. Users are therefore +encouraged to load and test the software's suitability as regards their +requirements in conditions enabling the security of their systems and/or +data to be ensured and, more generally, to use and operate it in the +same conditions as regards security. + +The fact that you are presently reading this means that you have had +knowledge of the CeCILL license and that you accept its terms. + +SEE LICENCE NOTICE: file README-LICENCE-utf8.txt at project source root. + +This software is being developed by Eric Debreuve, a CNRS employee and +member of team Morpheme. +Team Morpheme is a joint team between Inria, CNRS, and UniCA. +It is hosted by the Centre Inria d'Université Côte d'Azur, Laboratory +I3S, and Laboratory iBV. + +CNRS: https://www.cnrs.fr/index.php/en +Inria: https://www.inria.fr/en/ +UniCA: https://univ-cotedazur.eu/ +Centre Inria d'Université Côte d'Azur: https://www.inria.fr/en/centre/sophia/ +I3S: https://www.i3s.unice.fr/en/ +iBV: http://ibv.unice.fr/ +Team Morpheme: https://team.inria.fr/morpheme/ +""" diff --git a/package/logger_36/logger_gpu.py b/package/logger_36/logger_gpu.py new file mode 100644 index 0000000..af0df2d --- /dev/null +++ b/package/logger_36/logger_gpu.py @@ -0,0 +1,53 @@ +""" +Copyright CNRS/Inria/UniCA +Contributor(s): Eric Debreuve (eric.debreuve@cnrs.fr) since 2023 +SEE COPYRIGHT NOTICE BELOW +""" + +from logger_36.catalog.logger.gpu import LogGPURelatedDetails + +""" +COPYRIGHT NOTICE + +This software is governed by the CeCILL license under French law and +abiding by the rules of distribution of free software. You can use, +modify and/ or redistribute the software under the terms of the CeCILL +license as circulated by CEA, CNRS and INRIA at the following URL +"http://www.cecill.info". + +As a counterpart to the access to the source code and rights to copy, +modify and redistribute granted by the license, users are provided only +with a limited warranty and the software's author, the holder of the +economic rights, and the successive licensors have only limited +liability. + +In this respect, the user's attention is drawn to the risks associated +with loading, using, modifying and/or developing or reproducing the +software by the user in light of its specific status of free software, +that may mean that it is complicated to manipulate, and that also +therefore means that it is reserved for developers and experienced +professionals having in-depth computer knowledge. Users are therefore +encouraged to load and test the software's suitability as regards their +requirements in conditions enabling the security of their systems and/or +data to be ensured and, more generally, to use and operate it in the +same conditions as regards security. + +The fact that you are presently reading this means that you have had +knowledge of the CeCILL license and that you accept its terms. + +SEE LICENCE NOTICE: file README-LICENCE-utf8.txt at project source root. + +This software is being developed by Eric Debreuve, a CNRS employee and +member of team Morpheme. +Team Morpheme is a joint team between Inria, CNRS, and UniCA. +It is hosted by the Centre Inria d'Université Côte d'Azur, Laboratory +I3S, and Laboratory iBV. + +CNRS: https://www.cnrs.fr/index.php/en +Inria: https://www.inria.fr/en/ +UniCA: https://univ-cotedazur.eu/ +Centre Inria d'Université Côte d'Azur: https://www.inria.fr/en/centre/sophia/ +I3S: https://www.i3s.unice.fr/en/ +iBV: http://ibv.unice.fr/ +Team Morpheme: https://team.inria.fr/morpheme/ +""" diff --git a/package/logger_36/measure.py b/package/logger_36/measure.py new file mode 100644 index 0000000..f60d775 --- /dev/null +++ b/package/logger_36/measure.py @@ -0,0 +1,55 @@ +""" +Copyright CNRS/Inria/UniCA +Contributor(s): Eric Debreuve (eric.debreuve@cnrs.fr) since 2023 +SEE COPYRIGHT NOTICE BELOW +""" + +from logger_36.task.measure.chronos import ElapsedTime, TimeStamp +from logger_36.task.measure.memory import CanCheckUsage as CanCheckMemoryUsage +from logger_36.task.measure.memory import CurrentUsage as CurrentMemoryUsage + +""" +COPYRIGHT NOTICE + +This software is governed by the CeCILL license under French law and +abiding by the rules of distribution of free software. You can use, +modify and/ or redistribute the software under the terms of the CeCILL +license as circulated by CEA, CNRS and INRIA at the following URL +"http://www.cecill.info". + +As a counterpart to the access to the source code and rights to copy, +modify and redistribute granted by the license, users are provided only +with a limited warranty and the software's author, the holder of the +economic rights, and the successive licensors have only limited +liability. + +In this respect, the user's attention is drawn to the risks associated +with loading, using, modifying and/or developing or reproducing the +software by the user in light of its specific status of free software, +that may mean that it is complicated to manipulate, and that also +therefore means that it is reserved for developers and experienced +professionals having in-depth computer knowledge. Users are therefore +encouraged to load and test the software's suitability as regards their +requirements in conditions enabling the security of their systems and/or +data to be ensured and, more generally, to use and operate it in the +same conditions as regards security. + +The fact that you are presently reading this means that you have had +knowledge of the CeCILL license and that you accept its terms. + +SEE LICENCE NOTICE: file README-LICENCE-utf8.txt at project source root. + +This software is being developed by Eric Debreuve, a CNRS employee and +member of team Morpheme. +Team Morpheme is a joint team between Inria, CNRS, and UniCA. +It is hosted by the Centre Inria d'Université Côte d'Azur, Laboratory +I3S, and Laboratory iBV. + +CNRS: https://www.cnrs.fr/index.php/en +Inria: https://www.inria.fr/en/ +UniCA: https://univ-cotedazur.eu/ +Centre Inria d'Université Côte d'Azur: https://www.inria.fr/en/centre/sophia/ +I3S: https://www.i3s.unice.fr/en/ +iBV: http://ibv.unice.fr/ +Team Morpheme: https://team.inria.fr/morpheme/ +""" diff --git a/package/logger_36/storage.py b/package/logger_36/storage.py new file mode 100644 index 0000000..1602a3e --- /dev/null +++ b/package/logger_36/storage.py @@ -0,0 +1,53 @@ +""" +Copyright CNRS/Inria/UniCA +Contributor(s): Eric Debreuve (eric.debreuve@cnrs.fr) since 2023 +SEE COPYRIGHT NOTICE BELOW +""" + +from logger_36.task.storage import SaveLOGasHTML, html_reader_t + +""" +COPYRIGHT NOTICE + +This software is governed by the CeCILL license under French law and +abiding by the rules of distribution of free software. You can use, +modify and/ or redistribute the software under the terms of the CeCILL +license as circulated by CEA, CNRS and INRIA at the following URL +"http://www.cecill.info". + +As a counterpart to the access to the source code and rights to copy, +modify and redistribute granted by the license, users are provided only +with a limited warranty and the software's author, the holder of the +economic rights, and the successive licensors have only limited +liability. + +In this respect, the user's attention is drawn to the risks associated +with loading, using, modifying and/or developing or reproducing the +software by the user in light of its specific status of free software, +that may mean that it is complicated to manipulate, and that also +therefore means that it is reserved for developers and experienced +professionals having in-depth computer knowledge. Users are therefore +encouraged to load and test the software's suitability as regards their +requirements in conditions enabling the security of their systems and/or +data to be ensured and, more generally, to use and operate it in the +same conditions as regards security. + +The fact that you are presently reading this means that you have had +knowledge of the CeCILL license and that you accept its terms. + +SEE LICENCE NOTICE: file README-LICENCE-utf8.txt at project source root. + +This software is being developed by Eric Debreuve, a CNRS employee and +member of team Morpheme. +Team Morpheme is a joint team between Inria, CNRS, and UniCA. +It is hosted by the Centre Inria d'Université Côte d'Azur, Laboratory +I3S, and Laboratory iBV. + +CNRS: https://www.cnrs.fr/index.php/en +Inria: https://www.inria.fr/en/ +UniCA: https://univ-cotedazur.eu/ +Centre Inria d'Université Côte d'Azur: https://www.inria.fr/en/centre/sophia/ +I3S: https://www.i3s.unice.fr/en/ +iBV: http://ibv.unice.fr/ +Team Morpheme: https://team.inria.fr/morpheme/ +""" diff --git a/package/logger_36/task/measure/memory.py b/package/logger_36/task/measure/memory.py index a0ccd78..4b4300c 100644 --- a/package/logger_36/task/measure/memory.py +++ b/package/logger_36/task/measure/memory.py @@ -12,7 +12,7 @@ except ModuleNotFoundError: _PROCESS = None -def CanCheckMemory() -> bool: +def CanCheckUsage() -> bool: """""" return _PROCESS is not None diff --git a/package/logger_36/type/handler.py b/package/logger_36/type/handler.py index e3658b8..f8eda80 100644 --- a/package/logger_36/type/handler.py +++ b/package/logger_36/type/handler.py @@ -18,7 +18,7 @@ from logger_36.constant.message import NEXT_LINE_PROLOGUE from logger_36.constant.record import HIDE_WHERE_ATTR, SHOW_WHERE_ATTR from logger_36.task.format.message import FormattedMessage, MessageFormat from logger_36.task.measure.chronos import TimeStamp -from logger_36.task.measure.memory import CanCheckMemory +from logger_36.task.measure.memory import CanCheckUsage as CanCheckMemoryUsage _MEMORY_MEASURE_ERROR = MEMORY_MEASURE_ERROR @@ -53,7 +53,7 @@ class handler_extension_t: if self.name is None: self.name = TimeStamp() - if self.show_memory_usage and not CanCheckMemory(): + if self.show_memory_usage and not CanCheckMemoryUsage(): self.show_memory_usage = False if _MEMORY_MEASURE_ERROR is not None: print(_MEMORY_MEASURE_ERROR, file=sstm.stderr) diff --git a/package/logger_36/version.py b/package/logger_36/version.py index 0e8a7a2..a5657e2 100644 --- a/package/logger_36/version.py +++ b/package/logger_36/version.py @@ -4,7 +4,7 @@ Contributor(s): Eric Debreuve (eric.debreuve@cnrs.fr) since 2023 SEE COPYRIGHT NOTICE BELOW """ -__version__ = "2024.17" +__version__ = "2024.18" """ COPYRIGHT NOTICE diff --git a/test/main.py b/test/main.py index cf436ae..e62c94a 100644 --- a/test/main.py +++ b/test/main.py @@ -13,10 +13,10 @@ from io import StringIO as fake_file_t from pathlib import Path as path_t from tempfile import TemporaryDirectory -from logger_36 import LOGGER, AddFileHandler, AddGenericHandler, AddRichConsoleHandler -from logger_36.catalog.logging.memory import LogMaximumMemoryUsage, LogMemoryUsages -from logger_36.catalog.logging.system import LogSystemDetails -from logger_36.task.storage import SaveLOGasHTML, html_reader_t +from logger_36 import LOGGER +from logger_36.handler import AddFileHandler, AddGenericHandler, AddRichConsoleHandler +from logger_36.logger import LogMaximumMemoryUsage, LogMemoryUsages, LogSystemDetails +from logger_36.storage import SaveLOGasHTML, html_reader_t VERY_LONG_LINE = "V" + 30 * "e" + "ry l" + 30 * "o" + "ng line" -- GitLab