Skip to content

Commit

Permalink
logger: Add NO_COLOR for console logger
Browse files Browse the repository at this point in the history
Change-Id: I02ccb3c2d05aaa14a7f42dd0c6e2854f9905ccf2
  • Loading branch information
Olivier Dion committed Dec 22, 2021
1 parent 21c8feb commit ea6a019
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,17 @@ class ConsoleLog : public jami::Logger::Handler

virtual void consume(jami::Logger::Msg& msg) override
{
static bool no_color = (getenv("NO_COLOR") ||
getenv("NO_COLORS") ||
getenv("NO_COLOUR") ||
getenv("NO_COLOURS"));

#if defined(_WIN32) && !defined(RING_UWP)
WORD saved_attributes;
HANDLE hConsole;
#endif

if (not no_color) {
#ifndef _WIN32
const char* color_header = CYAN;
const char* color_prefix = "";
Expand All @@ -305,9 +316,9 @@ class ConsoleLog : public jami::Logger::Handler
WORD color_header = CYAN;
#endif
#if defined(_WIN32) && !defined(RING_UWP)
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
WORD saved_attributes;

#endif

switch (msg.level_) {
Expand Down Expand Up @@ -335,18 +346,23 @@ class ConsoleLog : public jami::Logger::Handler
SetConsoleTextAttribute(hConsole, saved_attributes);
SetConsoleTextAttribute(hConsole, color_prefix);
#endif
} else {
fputs(msg.header_.c_str(), stderr);
}

fputs(msg.payload_.get(), stderr);

if (msg.linefeed_) {
putc(ENDL, stderr);
}

if (not no_color) {
#ifndef _WIN32
fputs(END_COLOR, stderr);
#elif !defined(RING_UWP)
SetConsoleTextAttribute(hConsole, saved_attributes);
#endif
}
}
};

Expand Down

0 comments on commit ea6a019

Please sign in to comment.