optimad / bitpit

Open source library for scientific HPC
http://optimad.github.io/bitpit/
GNU Lesser General Public License v3.0
117 stars 33 forks source link

common: improve logger #257

Closed andrea-iob closed 2 years ago

andrea-iob commented 2 years ago

The improvements are:

Here some examples:

log.cout().disableFile(); // Logging on file will be disabled
log::cout() << "Information message" << std::endl;
log::cout(log::CRITICAL) << "Critical message" << std::endl;
log::info() << "Information message" << std::endl;
log::debug() << "Debug message" << std::endl;

log.cout("logger1") << disableConsole(log::INFO); // Only messages with severity greater than INFO will be printed on the console
log.cout("logger1", log::ERROR) << "Error message" << std::endl;
log.warning("logger1") << "Warning message" << std::endl;

log.cout("logger2") << disable(); // Disable logging (verbosity is overridden in order to suppress all output)
log.error("logger2") << "Error message" << std::endl; // This message will not be logged
log.cout("logger2") << disable(log::NOTSET); // Re-enable the logging (log::NOTSET will remove the verbosity override)
log.error("logger2") << "Error message" << std::endl;
andrea-iob commented 2 years ago

I moved insertion/extraction operators from the operators module to the common module, because those operations need to be defined before the logger. If the operators are defined after the logger, they will not be found by the compiler when selecting the right specialization to be used for the insertion operator in Logger::println (gcc will still accept the code, but this is a gcc bug, https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51577).

andrea-iob commented 2 years ago

All fixed, thanks. I've also rebased the branch on top of the current master.

marcocisternino commented 2 years ago

Could it be useful for users to rely on an "enable" method for the logger? I understand the semantic, but double negation to enable (or re-enable) the logger (at its level( it seems not intuitive to me. Maybe a kind of "enableDefault", even if I do not like "Default".

roccoarpa commented 2 years ago

Hello guys, modifications are fine for me, but honestly I've not completely understood the request from Marco, and i don't want to rush my final approval. I will let you to iterate and elaborate more the concept, first.

andrea-iob commented 2 years ago

Could it be useful for users to rely on an "enable" method for the logger? I understand the semantic, but double negation to enable (or re-enable) the logger (at its level( it seems not intuitive to me. Maybe a kind of "enableDefault", even if I do not like "Default".

The function disable(log:Level level) will provide an overriding verbosity level which takes precedence over the logger’s own level. Its effect is to disable all logging calls of severity level and below. When no arguments are specified, the overriding verbosity will be set to CRITICAL, hence all logger output will be disabled.

Its main purpose is to temporary throttle logging output down. When the logger verbosity can be set again to its defualt value, the function debug is called with the argument log::NOTSET and the verbosity override is removed.

(I've updated the wording of the example in the pull request description to better reflect what the function disable does.)

marcocisternino commented 2 years ago

Fine. Doc in sources was clear and your final comment too. Thanks for re-wording. I approve.