-
Notifications
You must be signed in to change notification settings - Fork 20
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
Adding setLogLevel feature #264
base: main
Are you sure you want to change the base?
Changes from all 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 | ||||
---|---|---|---|---|---|---|
|
@@ -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
+118
to
+120
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. 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. 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) { | ||||||
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.