Skip to content

Commit 39cadcc

Browse files
Merge pull request #455 from pollend/feature/add-support-ansi-stdout-logging
feat: added ansi color stdout logging @pollend I hope you don't ming the changes I did
2 parents 27545f2 + fda2738 commit 39cadcc

File tree

1 file changed

+49
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)