-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The new logger interface provides a single function, `logln`, which can be implemented by the users of the API. The interface has a default implementation, `StdErrLogger`, which logs the data to `stderr` if the level is higher than `LogLevel::INFO`. `LogLevel::ALWAYS` is always logged without a prefix to match the existing behavior.
- Loading branch information
1 parent
fbd9a61
commit 48cbbad
Showing
3 changed files
with
117 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#pragma once | ||
|
||
#include <cstdio> | ||
#include <cstdarg> | ||
|
||
|
||
// Since this file is most likely to be used outside of the project (like, in Android), | ||
// using a namespace to avoid conflicts | ||
namespace wfb { | ||
enum class LogLevel { | ||
ALWAYS, | ||
ERROR, | ||
WARNING, | ||
INFO, | ||
DEBUG | ||
}; | ||
|
||
class Logger | ||
{ | ||
public: | ||
virtual ~Logger() = default; | ||
virtual void logln(LogLevel level, const char* format, ...) = 0; | ||
// logs unconditionally disregarding the log level | ||
// virtual void logln(const char* format, ...) = 0; | ||
}; | ||
|
||
class StdErrLogger : public Logger | ||
{ | ||
public: | ||
void logln(LogLevel level, const char* format, ...) override { | ||
const char* level_str = ""; | ||
switch (level) { | ||
case LogLevel::DEBUG: | ||
case LogLevel::INFO: | ||
return; | ||
case LogLevel::ALWAYS: | ||
level_str = ""; | ||
break; | ||
case LogLevel::WARNING: | ||
level_str = "WARNING: "; | ||
break; | ||
case LogLevel::ERROR: | ||
level_str = "ERROR: "; | ||
break; | ||
} | ||
|
||
char buffer[1024]; | ||
va_list args; | ||
va_start(args, format); | ||
int prefix_len = snprintf(buffer, sizeof(buffer), "%s", level_str); | ||
int message_len = vsnprintf(buffer + prefix_len, sizeof(buffer) - prefix_len - 2, format, args); | ||
va_end(args); | ||
|
||
buffer[prefix_len + message_len] = '\n'; | ||
buffer[prefix_len + message_len + 1] = '\0'; | ||
} | ||
}; | ||
} // namespace wfb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.