-
Notifications
You must be signed in to change notification settings - Fork 21
Adding setLogLevel feature #264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -23,6 +23,8 @@ enum class Level { | |||
| fatal | ||||
| }; | ||||
|
|
||||
| extern Level currentLogLevel; | ||||
|
|
||||
| class Formatter { | ||||
| public: | ||||
| virtual void formatMessage(std::ostringstream& ostream, | ||||
|
|
@@ -68,14 +70,14 @@ extern std::unique_ptr<Formatter> formatter; | |||
| class Logger { | ||||
| public: | ||||
| Logger(Level level, const char *format, va_list args); | ||||
| Logger(Logger&& other) noexcept : ostream(std::move(other.ostream)) {} | ||||
| Logger(Logger&& other) noexcept : level(other.level), ostream(std::move(other.ostream)) {} | ||||
| Logger(const Logger&) = delete; | ||||
| Logger& operator=(const Logger&) = delete; | ||||
| ~Logger(); | ||||
|
|
||||
| template<typename T> | ||||
| Logger& operator()(const char *key, const T& value) { | ||||
| if (formatter) | ||||
| if (formatter && level >= currentLogLevel) | ||||
| formatter->formatKeyValueBefore(ostream, key); | ||||
|
|
||||
| using DecayedT = std::decay_t<T>; | ||||
|
|
@@ -88,12 +90,13 @@ class Logger { | |||
| else | ||||
| ostream << value; | ||||
|
|
||||
| if (formatter) | ||||
| if (formatter && level >= currentLogLevel) | ||||
| formatter->formatKeyValueAfter(ostream, key); | ||||
| return *this; | ||||
| } | ||||
|
|
||||
| private: | ||||
| Level level; | ||||
| std::ostringstream ostream; | ||||
| }; | ||||
|
|
||||
|
|
@@ -130,6 +133,31 @@ class Logger { | |||
| * {"time":"2024-11-15T00:27:30.300Z","level":"error","msg":"High load","percent":99.8,"num_clients":9801} | ||||
| * {"time":"2024-11-15T00:27:30.300Z","level":"debug","msg":"Counter incremented","cnt":355} | ||||
| * {"time":"2024-11-15T00:27:30.300Z","level":"fatal","msg":"Emergency exit","err_code":312645} | ||||
| * Example C: Setting the log level dynamically | ||||
| * ============================================ | ||||
| * mesh::log::setLogLevel(mesh::log::Level::warn); // Set minimum log level to WARN | ||||
| * | ||||
| * log::info("This message will not be displayed")("id", "123456"); | ||||
| * log::warn("Low memory warning")("available_mb", 512); | ||||
| * log::error("Critical error occurred")("error_code", 5001); | ||||
| * log::debug("Debugging details")("step", "init"); | ||||
| * | ||||
| * Output: | ||||
| * Nov 15 00:26:07.672 [WARN] Low memory warning available_mb=512 | ||||
| * Nov 15 00:26:07.672 [ERRO] Critical error occurred error_code=5001 | ||||
| * Nov 15 00:26:07.672 [DEBU] Debugging details step=init | ||||
|
||||
| * Nov 15 00:26:07.672 [DEBU] Debugging details step=init |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -11,6 +11,8 @@ | |||||
|
|
||||||
| namespace mesh::log { | ||||||
|
|
||||||
| Level currentLogLevel = Level::info; | ||||||
|
|
||||||
| void StandardFormatter::formatMessage(std::ostringstream& ostream, Level level, | ||||||
| const char *format, va_list args) | ||||||
| { | ||||||
|
|
@@ -113,21 +115,24 @@ void setFormatter(std::unique_ptr<Formatter> new_formatter) | |||||
| formatter = std::move(new_formatter); | ||||||
| } | ||||||
|
|
||||||
| void setLogLevel(Level level) { | ||||||
| currentLogLevel = level; | ||||||
| } | ||||||
|
Comment on lines
+132
to
+134
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is not thread-safe. Since there is no meaning in changing the log level at runtime, I suggest to leave this function as it is but say in the comment block that this is to be called only once the logger is created in the app.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually there was a meaning for me - for different tests I wanted to have control over what is visible where, I'll make it thread safe.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you make it thread safe, you'll make |
||||||
|
|
||||||
| Logger::Logger(Level level, const char *format, va_list args) | ||||||
| { | ||||||
| if (formatter) { | ||||||
| if (level >= currentLogLevel && formatter) { | ||||||
| formatter->formatBefore(ostream); | ||||||
| formatter->formatMessage(ostream, level, format, args); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| Logger::~Logger() | ||||||
| { | ||||||
| if (formatter) | ||||||
| formatter->formatAfter(ostream); | ||||||
|
|
||||||
| if (!ostream.str().empty()) | ||||||
| Logger::~Logger() { | ||||||
| if (!ostream.str().empty() && level >= currentLogLevel) { | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| if (formatter) | ||||||
| formatter->formatAfter(ostream); | ||||||
| std::cout << ostream.str() << std::endl; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| Logger info(const char* format, ...) | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You still print the log message here no matter which log level is set, right? You only avoid formatter methods to be called if the level is disabled.