-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update ColorCoutSink for WIN32 (#132)
* Update ColorCoutSink for WIN32 * Separate sinks between Linux and Windows
- Loading branch information
Showing
5 changed files
with
102 additions
and
53 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
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
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,35 @@ | ||
#pragma once | ||
|
||
struct ColorCoutSink { | ||
// Linux xterm color | ||
// http://stackoverflow.com/questions/2616906/how-do-i-output-coloured-text-to-a-linux-terminal | ||
enum FG_Color {YELLOW = 33, RED = 31, GREEN = 32, WHITE = 37}; | ||
|
||
|
||
FG_Color GetColor(const LEVELS level) const { | ||
if (level.value == WARNING.value) { return YELLOW; } | ||
if (level.value == G3LOG_DEBUG.value) { return GREEN; } | ||
if (g3::internal::wasFatal(level)) { return RED; } | ||
|
||
return WHITE; | ||
} | ||
|
||
void reset() { | ||
std::ostringstream oss; | ||
oss << "\033[" << WHITE << "m" << " " << "\033[m" << std::endl; | ||
std::cout << oss.str(); | ||
} | ||
|
||
void ReceiveLogMessage(g3::LogMessageMover logEntry) { | ||
auto level = logEntry.get()._level; | ||
auto color = GetColor(level); | ||
|
||
reset(); | ||
|
||
std::ostringstream oss; | ||
oss << "\033[" << color << "m" | ||
<< logEntry.get().toString() << "\033[m" << std::endl; | ||
std::cout << oss.str(); | ||
reset(); | ||
} | ||
}; |
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,45 @@ | ||
#pragma once | ||
|
||
#ifndef WIN32_LEAN_AND_MEAN | ||
#define WIN32_LEAN_AND_MEAN 1 | ||
#endif | ||
|
||
#include <Windows.h> | ||
|
||
struct ColorCoutSink { | ||
ColorCoutSink() : hHandle_(GetStdHandle(STD_OUTPUT_HANDLE)) {} | ||
|
||
// Windows colors | ||
// https://docs.microsoft.com/en-us/windows/console/console-screen-buffers#character-attributes | ||
enum FG_Color { YELLOW = FOREGROUND_RED | FOREGROUND_GREEN, RED = FOREGROUND_RED, GREEN = FOREGROUND_GREEN, WHITE = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED }; | ||
|
||
|
||
FG_Color GetColor(const LEVELS level) const { | ||
if (level.value == WARNING.value) { return YELLOW; } | ||
if (level.value == G3LOG_DEBUG.value) { return GREEN; } | ||
if (g3::internal::wasFatal(level)) { return RED; } | ||
|
||
return WHITE; | ||
} | ||
|
||
void reset() { | ||
SetConsoleTextAttribute(hHandle_, WHITE); | ||
} | ||
|
||
void ReceiveLogMessage(g3::LogMessageMover logEntry) { | ||
auto level = logEntry.get()._level; | ||
auto color = GetColor(level); | ||
|
||
reset(); | ||
|
||
auto str = logEntry.get().toString(); | ||
SetConsoleTextAttribute(hHandle_, (WORD)color); | ||
|
||
std::cout << str; | ||
|
||
reset(); | ||
} | ||
|
||
private: | ||
HANDLE hHandle_; | ||
}; |