-
Notifications
You must be signed in to change notification settings - Fork 246
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.
- Loading branch information
1 parent
fbd9a61
commit e3f1ad8
Showing
3 changed files
with
111 additions
and
35 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,61 @@ | ||
#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; | ||
}; | ||
|
||
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: | ||
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); | ||
if (prefix_len < 0) | ||
return; | ||
|
||
int message_len = vsnprintf(buffer + prefix_len, sizeof(buffer) - prefix_len - 2, format, args); | ||
if (message_len < 0) | ||
return; | ||
|
||
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
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