Skip to content

Commit 4ad3442

Browse files
committed
feat: added ansi color stdout logging
Signed-off-by: Michael Pollind <mpollind@gmail.com>
1 parent 27545f2 commit 4ad3442

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef _NBL_SYSTEM_C_COLORFUL_STDOUT_LOGGER_ANSI_INCLUDED_
2+
#define _NBL_SYSTEM_C_COLORFUL_STDOUT_LOGGER_ANSI_INCLUDED_
3+
4+
#include "nbl/system/IThreadsafeLogger.h"
5+
6+
#include <string_view>
7+
8+
namespace nbl::system {
9+
10+
#include "nbl/system/DefaultFuncPtrLoader.h"
11+
12+
// logging using ANSI escape codes
13+
class NBL_API CColoredStdoutLoggerANSI : public IThreadsafeLogger {
14+
15+
public:
16+
CColoredStdoutLoggerANSI(
17+
core::bitflag<E_LOG_LEVEL> logLevelMask = ILogger::defaultLogMask())
18+
: IThreadsafeLogger(logLevelMask) {}
19+
20+
private:
21+
virtual void threadsafeLog_impl(const std::string_view &fmt,
22+
E_LOG_LEVEL logLevel, va_list args) override;
23+
};
24+
25+
} // namespace nbl::system
26+
27+
#endif
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "nbl/system/CColoredStdoutLoggerANSI.h"
2+
3+
using namespace nbl::system;
4+
5+
void CColoredStdoutLoggerANSI::threadsafeLog_impl(const std::string_view &fmt, E_LOG_LEVEL logLevel, va_list args) {
6+
auto str = constructLogString(fmt, logLevel, args);
7+
switch (logLevel) {
8+
case ELL_DEBUG: {
9+
printf("\x1b[37m%s", str.data()); // White
10+
break;
11+
}
12+
case ELL_INFO: {
13+
printf("\x1b[37m%s", str.data()); // White
14+
break;
15+
}
16+
case ELL_WARNING: {
17+
printf("\x1b[33m%s", str.data()); // yellow
18+
break;
19+
}
20+
case ELL_ERROR: {
21+
printf("\x1b[31m%s", str.data()); // red
22+
break;
23+
}
24+
case ELL_PERFORMANCE: {
25+
printf("\x1b[34m%s", str.data()); // blue
26+
break;
27+
}
28+
case ELL_NONE: {
29+
assert(false);
30+
break;
31+
}
32+
}
33+
fflush(stdout);
34+
}
35+

0 commit comments

Comments
 (0)