Skip to content
Snippets Groups Projects
Commit 642e2e46 authored by Cyril Moineau's avatar Cyril Moineau
Browse files

ConsoleLevel is now shared accross modules.

parent ee16f34c
No related branches found
No related tags found
2 merge requests!318[Upd] release verision 0.5.0,!295ConsoleLevel is now shared accross modules.
Pipeline #62698 passed
......@@ -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.
......
......@@ -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
......
......@@ -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.
......
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