From 642e2e467bf4a103d36c88e9aa082621e6f74373 Mon Sep 17 00:00:00 2001 From: cmoineau <cyril.moineau@cea.fr> Date: Fri, 10 Jan 2025 07:45:28 +0000 Subject: [PATCH] ConsoleLevel is now shared accross modules. --- include/aidge/utils/Log.hpp | 37 ++++++++++++++++++++++++++++- python_binding/utils/pybind_Log.cpp | 22 +++++++++++++---- src/utils/Log.cpp | 2 +- 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/include/aidge/utils/Log.hpp b/include/aidge/utils/Log.hpp index bc99ab7c0..ce0e0791e 100644 --- a/include/aidge/utils/Log.hpp +++ b/include/aidge/utils/Log.hpp @@ -21,7 +21,14 @@ #include "aidge/data/half_fmt.hpp" #include "aidge/utils/Attributes.hpp" +#ifdef PYBIND +#include <pybind11/pybind11.h> // get_shared_data, set_shared_data, PyIsInitialized +#endif + namespace Aidge { +#ifdef PYBIND +namespace py = pybind11; +#endif /** * Helper to define a context anywhere, hiding the scoped variable name * which has no relevance. @@ -126,8 +133,36 @@ class Log { */ static void setConsoleLevel(Level level) { mConsoleLevel = level; - } + // Note: In python each module has a compiled version of Log. + // This means each static instance is separated and does not communicate. + // To allow the communication between the different modules, we use PyCapsule. + // https://docs.python.org/3/extending/extending.html#providing-a-c-api-for-an-extension-module + #ifdef PYBIND + #define _CRT_SECURE_NO_WARNINGS + if (Py_IsInitialized()){ + // Note: Setting mConsoleLevel and not level is important + // to avoid garbage collection of the pointer. + py::set_shared_data("consoleLevel", &mConsoleLevel); + } + #endif // PYBIND + } + /** + * @brief Get the curent Console Level. + * + * @return const Level + */ + static Level getConsoleLevel(){ + #ifdef PYBIND + #define _CRT_SECURE_NO_WARNINGS + if (Py_IsInitialized()){ + auto shared_data = reinterpret_cast<Level *>(py::get_shared_data("consoleLevel")); + if (shared_data) + mConsoleLevel = *shared_data; + } + #endif // PYBIND + return mConsoleLevel; + } /** * Set or disable colors on console. * Initial value should be assumed true. diff --git a/python_binding/utils/pybind_Log.cpp b/python_binding/utils/pybind_Log.cpp index aa42c6605..bb81c10b2 100644 --- a/python_binding/utils/pybind_Log.cpp +++ b/python_binding/utils/pybind_Log.cpp @@ -78,17 +78,31 @@ void init_Log(py::module& m){ .def_static("set_console_level", &Log::setConsoleLevel, py::arg("level"), R"mydelimiter( Set the minimum log level displayed in the console. - Available `Level`s in ascending order : + Available `Level`s in ascending order: - Level.Debug - Level.Info - Level.Notice - Level.Warn - Level.Error - - Level.Fatal + - Level.Fatal :param level: Log level. :type level: Level )mydelimiter") + .def_static("get_console_level", &Log::getConsoleLevel, + R"mydelimiter( + Get the current log level. Log level is set to ``Notice`` by default. + Log level is shared across modules. + Available `Level`s in ascending order: + - Level.Debug + - Level.Info + - Level.Notice + - Level.Warn + - Level.Error + - Level.Fatal + :return level: Log level. + :rtype level: Level + )mydelimiter") .def_static("set_console_color", &Log::setConsoleColor, py::arg("enabled"), R"mydelimiter( Enables or disable color output on comsole. @@ -100,13 +114,13 @@ void init_Log(py::module& m){ .def_static("set_file_level", &Log::setFileLevel, py::arg("level"), R"mydelimiter( Set the minimum log level saved in the log file. - Available `Level`s in ascending order : + Available `Level`s in ascending order : - Level.Debug - Level.Info - Level.Notice - Level.Warn - Level.Error - - Level.Fatal + - Level.Fatal :param level: Log level. :type level: Level diff --git a/src/utils/Log.cpp b/src/utils/Log.cpp index 136fcc16f..383fdae17 100644 --- a/src/utils/Log.cpp +++ b/src/utils/Log.cpp @@ -60,7 +60,7 @@ std::unique_ptr<FILE, Aidge::Log::fcloseDeleter> Aidge::Log::mFile {nullptr, Aid std::vector<std::string> Aidge::Log::mContext; void Aidge::Log::log(Level level, const std::string& msg) { - if (level >= mConsoleLevel) { + if (level >= getConsoleLevel()) { // Apply log level style only for console. // Styles that were already applied to msg with fmt are kept also in // the log file. -- GitLab