From 616d87444313db865c60fbeee36ebe5250ef301e Mon Sep 17 00:00:00 2001 From: Yuri Kunde Schlesner Date: Tue, 28 Oct 2014 05:36:00 -0200 Subject: New logging system --- src/common/logging/text_formatter.cpp | 47 +++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/common/logging/text_formatter.cpp (limited to 'src/common/logging/text_formatter.cpp') diff --git a/src/common/logging/text_formatter.cpp b/src/common/logging/text_formatter.cpp new file mode 100644 index 000000000..01c355bb6 --- /dev/null +++ b/src/common/logging/text_formatter.cpp @@ -0,0 +1,47 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#include +#include + +#include "common/logging/backend.h" +#include "common/logging/log.h" +#include "common/logging/text_formatter.h" + +namespace Log { + +void FormatLogMessage(const Entry& entry, char* out_text, size_t text_len) { + unsigned int time_seconds = static_cast(entry.timestamp.count() / 1000000); + unsigned int time_fractional = static_cast(entry.timestamp.count() % 1000000); + + const char* class_name = Logger::GetLogClassName(entry.log_class); + const char* level_name = Logger::GetLevelName(entry.log_level); + + snprintf(out_text, text_len, "[%4u.%06u] %s <%s> %s: %s", + time_seconds, time_fractional, class_name, level_name, + entry.location.c_str(), entry.message.c_str()); +} + +void PrintMessage(const Entry& entry) { + std::array format_buffer; + FormatLogMessage(entry, format_buffer.data(), format_buffer.size()); + fputs(format_buffer.data(), stderr); + fputc('\n', stderr); +} + +void TextLoggingLoop(std::shared_ptr logger) { + std::array entry_buffer; + + while (true) { + size_t num_entries = logger->GetEntries(entry_buffer.data(), entry_buffer.size()); + if (num_entries == Logger::QUEUE_CLOSED) { + break; + } + for (size_t i = 0; i < num_entries; ++i) { + PrintMessage(entry_buffer[i]); + } + } +} + +} -- cgit v1.2.3 From 6b0fb62c47875bf19edcb8e964d7595662caf5b3 Mon Sep 17 00:00:00 2001 From: Yuri Kunde Schlesner Date: Tue, 4 Nov 2014 01:42:34 -0200 Subject: Re-add coloring to the console logging output. --- src/common/logging/text_formatter.cpp | 50 +++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'src/common/logging/text_formatter.cpp') diff --git a/src/common/logging/text_formatter.cpp b/src/common/logging/text_formatter.cpp index 01c355bb6..b603ead13 100644 --- a/src/common/logging/text_formatter.cpp +++ b/src/common/logging/text_formatter.cpp @@ -5,6 +5,11 @@ #include #include +#ifdef _WIN32 +# define WIN32_LEAN_AND_MEAN +# include +#endif + #include "common/logging/backend.h" #include "common/logging/log.h" #include "common/logging/text_formatter.h" @@ -23,7 +28,52 @@ void FormatLogMessage(const Entry& entry, char* out_text, size_t text_len) { entry.location.c_str(), entry.message.c_str()); } +static void ChangeConsoleColor(Level level) { +#ifdef _WIN32 + static HANDLE console_handle = GetStdHandle(STD_ERROR_HANDLE); + + WORD color = 0; + switch (level) { + case Level::Trace: // Grey + color = FOREGROUND_INTENSITY; break; + case Level::Debug: // Cyan + color = FOREGROUND_GREEN | FOREGROUND_BLUE; break; + case Level::Info: // Bright gray + color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; break; + case Level::Warning: // Bright yellow + color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY; break; + case Level::Error: // Bright red + color = FOREGROUND_RED | FOREGROUND_INTENSITY; break; + case Level::Critical: // Bright magenta + color = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY; break; + } + + SetConsoleTextAttribute(console_handle, color); +#else +#define ESC "\x1b" + const char* color = ""; + switch (level) { + case Level::Trace: // Grey + color = ESC "[1;30m"; break; + case Level::Debug: // Cyan + color = ESC "[0;36m"; break; + case Level::Info: // Bright gray + color = ESC "[0;37m"; break; + case Level::Warning: // Bright yellow + color = ESC "[1;33m"; break; + case Level::Error: // Bright red + color = ESC "[1;31m"; break; + case Level::Critical: // Bright magenta + color = ESC "[1;35m"; break; + } +#undef ESC + + fputs(color, stderr); +#endif +} + void PrintMessage(const Entry& entry) { + ChangeConsoleColor(entry.log_level); std::array format_buffer; FormatLogMessage(entry, format_buffer.data(), format_buffer.size()); fputs(format_buffer.data(), stderr); -- cgit v1.2.3 From 6390c66e950b0536c438bf3be1ea78fd0540d6c9 Mon Sep 17 00:00:00 2001 From: Yuri Kunde Schlesner Date: Tue, 4 Nov 2014 03:03:19 -0200 Subject: Implement text path trimming for shorter paths. --- src/common/logging/text_formatter.cpp | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'src/common/logging/text_formatter.cpp') diff --git a/src/common/logging/text_formatter.cpp b/src/common/logging/text_formatter.cpp index b603ead13..88deb150e 100644 --- a/src/common/logging/text_formatter.cpp +++ b/src/common/logging/text_formatter.cpp @@ -14,8 +14,33 @@ #include "common/logging/log.h" #include "common/logging/text_formatter.h" +#include "common/string_util.h" + namespace Log { +// TODO(bunnei): This should be moved to a generic path manipulation library +const char* TrimSourcePath(const char* path, const char* root) { + const char* p = path; + + while (*p != '\0') { + const char* next_slash = p; + while (*next_slash != '\0' && *next_slash != '/' && *next_slash != '\\') { + ++next_slash; + } + + bool is_src = Common::ComparePartialString(p, next_slash, root); + p = next_slash; + + if (*p != '\0') { + ++p; + } + if (is_src) { + path = p; + } + } + return path; +} + void FormatLogMessage(const Entry& entry, char* out_text, size_t text_len) { unsigned int time_seconds = static_cast(entry.timestamp.count() / 1000000); unsigned int time_fractional = static_cast(entry.timestamp.count() % 1000000); @@ -25,7 +50,7 @@ void FormatLogMessage(const Entry& entry, char* out_text, size_t text_len) { snprintf(out_text, text_len, "[%4u.%06u] %s <%s> %s: %s", time_seconds, time_fractional, class_name, level_name, - entry.location.c_str(), entry.message.c_str()); + TrimSourcePath(entry.location.c_str()), entry.message.c_str()); } static void ChangeConsoleColor(Level level) { -- cgit v1.2.3 From 0e0a007a2503d468391004c8ea2faae305232345 Mon Sep 17 00:00:00 2001 From: Yuri Kunde Schlesner Date: Sat, 6 Dec 2014 20:00:08 -0200 Subject: Add configurable per-class log filtering --- src/common/logging/text_formatter.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/common/logging/text_formatter.cpp') diff --git a/src/common/logging/text_formatter.cpp b/src/common/logging/text_formatter.cpp index 88deb150e..3fe435346 100644 --- a/src/common/logging/text_formatter.cpp +++ b/src/common/logging/text_formatter.cpp @@ -11,6 +11,7 @@ #endif #include "common/logging/backend.h" +#include "common/logging/filter.h" #include "common/logging/log.h" #include "common/logging/text_formatter.h" @@ -105,7 +106,7 @@ void PrintMessage(const Entry& entry) { fputc('\n', stderr); } -void TextLoggingLoop(std::shared_ptr logger) { +void TextLoggingLoop(std::shared_ptr logger, const Filter* filter) { std::array entry_buffer; while (true) { @@ -114,7 +115,10 @@ void TextLoggingLoop(std::shared_ptr logger) { break; } for (size_t i = 0; i < num_entries; ++i) { - PrintMessage(entry_buffer[i]); + const Entry& entry = entry_buffer[i]; + if (filter->CheckMessage(entry.log_class, entry.log_level)) { + PrintMessage(entry); + } } } } -- cgit v1.2.3