Skip to content

Commit

Permalink
feat:easylog windows console add color
Browse files Browse the repository at this point in the history
  • Loading branch information
helintongh committed Jul 16, 2023
1 parent 3ab1e4c commit 2aaa895
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions include/ylt/easylog/appender.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,54 @@ class appender {
}
}

#ifdef _WIN32
enum class color_type : int {
none = -1,
black = 0,
blue,
green,
cyan,
red,
magenta,
yellow,
white,
black_bright,
blue_bright,
green_bright,
cyan_bright,
red_bright,
magenta_bright,
yellow_bright,
white_bright
};

void windows_set_color(color_type _fg, color_type _bg) {
auto handle = GetStdHandle(STD_OUTPUT_HANDLE);
if (handle != nullptr) {
CONSOLE_SCREEN_BUFFER_INFO info{};
auto status = GetConsoleScreenBufferInfo(handle, &info);
if (status) {
WORD color = info.wAttributes;
if (_fg != color_type::none) {
color = (color & 0xFFF0) | int(_fg);
}
if (_bg != color_type::none) {
color = (color & 0xFF0F) | int(_bg) << 4;
}
SetConsoleTextAttribute(handle, color);
}
}
}
#endif

void add_color(Severity severity) {
#if defined(_WIN32)
if (severity == Severity::WARN)
windows_set_color(color_type::black, color_type::yellow);
if (severity == Severity::ERROR)
windows_set_color(color_type::black, color_type::red);
if (severity == Severity::CRITICAL)
windows_set_color(color_type::white_bright, color_type::red);
#else
if (severity == Severity::WARN)
std::cout << "\x1B[93m";
Expand All @@ -203,6 +249,8 @@ class appender {

void clean_color(Severity severity) {
#if defined(_WIN32)
if (severity >= Severity::WARN)
windows_set_color(color_type::white, color_type::black);
#else
if (severity >= Severity::WARN)
std::cout << "\x1B[0m\x1B[0K";
Expand Down

0 comments on commit 2aaa895

Please sign in to comment.