From 3b558eebee54fa4bc9d1a7bb428d4bd33e1b817e Mon Sep 17 00:00:00 2001 From: Daniel Lim Wee Soong Date: Thu, 22 Mar 2018 18:21:29 +0800 Subject: [PATCH 1/3] Logging: Create logging macros based on fmtlib Add a new set of logging macros based on fmtlib Similar but not exactly the same as https://github.com/citra-emu/citra/pull/3533 Citra currently uses a different version of fmt, which does not support FMT_VARIADIC so make_args is used instead. On the other hand, yuzu uses fmt 4.1.0 which doesn't have make_args yet so FMT_VARIADIC is used. --- src/common/CMakeLists.txt | 2 +- src/common/logging/backend.cpp | 36 ++++++++++++++--------- src/common/logging/backend.h | 7 +++-- src/common/logging/filter.cpp | 8 +++--- src/common/logging/filter.h | 2 +- src/common/logging/log.h | 33 ++++++++++++++++++++- src/common/logging/text_formatter.cpp | 41 +++++++-------------------- src/common/logging/text_formatter.h | 14 +-------- src/common/string_util.cpp | 23 +++++++++++++++ src/common/string_util.h | 13 +++++++++ 10 files changed, 112 insertions(+), 67 deletions(-) diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index d132ab969..4480c25f9 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -90,7 +90,7 @@ endif() create_target_directory_groups(common) -target_link_libraries(common PUBLIC Boost::boost microprofile) +target_link_libraries(common PUBLIC Boost::boost fmt microprofile) if (ARCHITECTURE_x86_64) target_link_libraries(common PRIVATE xbyak) endif() diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index 7f3ae1a4e..22afa1d66 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp @@ -11,6 +11,7 @@ #include "common/logging/filter.h" #include "common/logging/log.h" #include "common/logging/text_formatter.h" +#include "common/string_util.h" namespace Log { @@ -102,25 +103,20 @@ const char* GetLevelName(Level log_level) { } Entry CreateEntry(Class log_class, Level log_level, const char* filename, unsigned int line_nr, - const char* function, const char* format, va_list args) { + const char* function, std::string message) { using std::chrono::duration_cast; using std::chrono::steady_clock; static steady_clock::time_point time_origin = steady_clock::now(); - std::array formatting_buffer; - Entry entry; entry.timestamp = duration_cast(steady_clock::now() - time_origin); entry.log_class = log_class; entry.log_level = log_level; - - snprintf(formatting_buffer.data(), formatting_buffer.size(), "%s:%s:%u", filename, function, - line_nr); - entry.location = std::string(formatting_buffer.data()); - - vsnprintf(formatting_buffer.data(), formatting_buffer.size(), format, args); - entry.message = std::string(formatting_buffer.data()); + entry.filename = Common::TrimSourcePath(filename); + entry.line_num = line_nr; + entry.function = function; + entry.message = std::move(message); return entry; } @@ -131,15 +127,27 @@ void SetFilter(Filter* new_filter) { filter = new_filter; } -void LogMessage(Class log_class, Level log_level, const char* filename, unsigned int line_nr, +void LogMessage(Class log_class, Level log_level, const char* filename, unsigned int line_num, const char* function, const char* format, ...) { - if (filter != nullptr && !filter->CheckMessage(log_class, log_level)) + if (filter && !filter->CheckMessage(log_class, log_level)) return; - + std::array formatting_buffer; va_list args; va_start(args, format); - Entry entry = CreateEntry(log_class, log_level, filename, line_nr, function, format, args); + vsnprintf(formatting_buffer.data(), formatting_buffer.size(), format, args); va_end(args); + Entry entry = CreateEntry(log_class, log_level, filename, line_num, function, + std::string(formatting_buffer.data())); + + PrintColoredMessage(entry); +} + +void FmtLogMessage(Class log_class, Level log_level, const char* filename, unsigned int line_num, + const char* function, const char* format, const fmt::ArgList& args) { + if (filter && !filter->CheckMessage(log_class, log_level)) + return; + Entry entry = + CreateEntry(log_class, log_level, filename, line_num, function, fmt::format(format, args)); PrintColoredMessage(entry); } diff --git a/src/common/logging/backend.h b/src/common/logging/backend.h index 70744e3e5..7e81efb23 100644 --- a/src/common/logging/backend.h +++ b/src/common/logging/backend.h @@ -22,13 +22,16 @@ struct Entry { std::chrono::microseconds timestamp; Class log_class; Level log_level; - std::string location; + std::string filename; + unsigned int line_num; + std::string function; std::string message; Entry() = default; Entry(Entry&& o) = default; Entry& operator=(Entry&& o) = default; + Entry& operator=(const Entry& o) = default; }; /** @@ -44,7 +47,7 @@ const char* GetLevelName(Level log_level); /// Creates a log entry by formatting the given source location, and message. Entry CreateEntry(Class log_class, Level log_level, const char* filename, unsigned int line_nr, - const char* function, const char* format, va_list args); + const char* function, std::string message); void SetFilter(Filter* filter); } // namespace Log diff --git a/src/common/logging/filter.cpp b/src/common/logging/filter.cpp index 733247b51..428723dce 100644 --- a/src/common/logging/filter.cpp +++ b/src/common/logging/filter.cpp @@ -65,14 +65,14 @@ bool Filter::ParseFilterRule(const std::string::const_iterator begin, const std::string::const_iterator end) { auto level_separator = std::find(begin, end, ':'); if (level_separator == end) { - LOG_ERROR(Log, "Invalid log filter. Must specify a log level after `:`: %s", - std::string(begin, end).c_str()); + NGLOG_ERROR(Log, "Invalid log filter. Must specify a log level after `:`: %s", + std::string(begin, end).c_str()); return false; } const Level level = GetLevelByName(level_separator + 1, end); if (level == Level::Count) { - LOG_ERROR(Log, "Unknown log level in filter: %s", std::string(begin, end).c_str()); + NGLOG_ERROR(Log, "Unknown log level in filter: %s", std::string(begin, end).c_str()); return false; } @@ -83,7 +83,7 @@ bool Filter::ParseFilterRule(const std::string::const_iterator begin, const Class log_class = GetClassByName(begin, level_separator); if (log_class == Class::Count) { - LOG_ERROR(Log, "Unknown log class in filter: %s", std::string(begin, end).c_str()); + NGLOG_ERROR(Log, "Unknown log class in filter: %s", std::string(begin, end).c_str()); return false; } diff --git a/src/common/logging/filter.h b/src/common/logging/filter.h index 16fa72642..ccca289bd 100644 --- a/src/common/logging/filter.h +++ b/src/common/logging/filter.h @@ -19,7 +19,7 @@ namespace Log { class Filter { public: /// Initializes the filter with all classes having `default_level` as the minimum level. - Filter(Level default_level); + Filter(Level default_level = Level::Info); /// Resets the filter so that all classes have `level` as the minimum displayed level. void ResetAll(Level level); diff --git a/src/common/logging/log.h b/src/common/logging/log.h index 3cf13fcb0..31fa932fb 100644 --- a/src/common/logging/log.h +++ b/src/common/logging/log.h @@ -4,6 +4,8 @@ #pragma once +#include +#include #include "common/common_types.h" namespace Log { @@ -87,7 +89,7 @@ enum class Class : ClassType { }; /// Logs a message to the global logger. -void LogMessage(Class log_class, Level log_level, const char* filename, unsigned int line_nr, +void LogMessage(Class log_class, Level log_level, const char* filename, unsigned int line_num, const char* function, #ifdef _MSC_VER _Printf_format_string_ @@ -99,6 +101,10 @@ void LogMessage(Class log_class, Level log_level, const char* filename, unsigned #endif ; +void FmtLogMessage(Class log_class, Level log_level, const char* filename, unsigned int line_num, + const char* function, const char* format, const fmt::ArgList& args); +FMT_VARIADIC(void, FmtLogMessage, Class, Level, const char*, unsigned int, const char*, const char*) + } // namespace Log #define LOG_GENERIC(log_class, log_level, ...) \ @@ -121,3 +127,28 @@ void LogMessage(Class log_class, Level log_level, const char* filename, unsigned LOG_GENERIC(::Log::Class::log_class, ::Log::Level::Error, __VA_ARGS__) #define LOG_CRITICAL(log_class, ...) \ LOG_GENERIC(::Log::Class::log_class, ::Log::Level::Critical, __VA_ARGS__) + +// Define the fmt lib macros +#ifdef _DEBUG +#define NGLOG_TRACE(log_class, ...) \ + ::Log::FmtLogMessage(::Log::Class::log_class, ::Log::Level::Trace, __FILE__, __LINE__, \ + __func__, __VA_ARGS__) +#else +#define NGLOG_TRACE(log_class, fmt, ...) (void(0)) +#endif + +#define NGLOG_DEBUG(log_class, ...) \ + ::Log::FmtLogMessage(::Log::Class::log_class, ::Log::Level::Debug, __FILE__, __LINE__, \ + __func__, __VA_ARGS__) +#define NGLOG_INFO(log_class, ...) \ + ::Log::FmtLogMessage(::Log::Class::log_class, ::Log::Level::Info, __FILE__, __LINE__, \ + __func__, __VA_ARGS__) +#define NGLOG_WARNING(log_class, ...) \ + ::Log::FmtLogMessage(::Log::Class::log_class, ::Log::Level::Warning, __FILE__, __LINE__, \ + __func__, __VA_ARGS__) +#define NGLOG_ERROR(log_class, ...) \ + ::Log::FmtLogMessage(::Log::Class::log_class, ::Log::Level::Error, __FILE__, __LINE__, \ + __func__, __VA_ARGS__) +#define NGLOG_CRITICAL(log_class, ...) \ + ::Log::FmtLogMessage(::Log::Class::log_class, ::Log::Level::Critical, __FILE__, __LINE__, \ + __func__, __VA_ARGS__) diff --git a/src/common/logging/text_formatter.cpp b/src/common/logging/text_formatter.cpp index e7e46c76b..8583916a8 100644 --- a/src/common/logging/text_formatter.cpp +++ b/src/common/logging/text_formatter.cpp @@ -18,50 +18,29 @@ 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) { +std::string FormatLogMessage(const Entry& entry) { unsigned int time_seconds = static_cast(entry.timestamp.count() / 1000000); unsigned int time_fractional = static_cast(entry.timestamp.count() % 1000000); const char* class_name = GetLogClassName(entry.log_class); const char* level_name = GetLevelName(entry.log_level); - snprintf(out_text, text_len, "[%4u.%06u] %s <%s> %s: %s", time_seconds, time_fractional, - class_name, level_name, TrimSourcePath(entry.location.c_str()), entry.message.c_str()); + return fmt::format("[{:4d}.{:06d}] {} <{}> {}:{}:{}: {}", time_seconds, time_fractional, + class_name, level_name, entry.filename, entry.function, entry.line_num, + entry.message); } 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); + auto str = FormatLogMessage(entry) + '\n'; + fputs(str.c_str(), stderr); } void PrintColoredMessage(const Entry& entry) { #ifdef _WIN32 - static HANDLE console_handle = GetStdHandle(STD_ERROR_HANDLE); + HANDLE console_handle = GetStdHandle(STD_ERROR_HANDLE); + if (console_handle == INVALID_HANDLE_VALUE) { + return; + } CONSOLE_SCREEN_BUFFER_INFO original_info = {0}; GetConsoleScreenBufferInfo(console_handle, &original_info); diff --git a/src/common/logging/text_formatter.h b/src/common/logging/text_formatter.h index 66e86d9ec..c587faefb 100644 --- a/src/common/logging/text_formatter.h +++ b/src/common/logging/text_formatter.h @@ -10,20 +10,8 @@ namespace Log { struct Entry; -/** - * Attempts to trim an arbitrary prefix from `path`, leaving only the part starting at `root`. It's - * intended to be used to strip a system-specific build directory from the `__FILE__` macro, - * leaving only the path relative to the sources root. - * - * @param path The input file path as a null-terminated string - * @param root The name of the root source directory as a null-terminated string. Path up to and - * including the last occurrence of this name will be stripped - * @return A pointer to the same string passed as `path`, but starting at the trimmed portion - */ -const char* TrimSourcePath(const char* path, const char* root = "src"); - /// Formats a log entry into the provided text buffer. -void FormatLogMessage(const Entry& entry, char* out_text, size_t text_len); +std::string FormatLogMessage(const Entry& entry); /// Formats and prints a log entry to stderr. void PrintMessage(const Entry& entry); /// Prints the same message as `PrintMessage`, but colored acoording to the severity level. diff --git a/src/common/string_util.cpp b/src/common/string_util.cpp index e9a2a6b00..124a8937f 100644 --- a/src/common/string_util.cpp +++ b/src/common/string_util.cpp @@ -462,4 +462,27 @@ std::string StringFromFixedZeroTerminatedBuffer(const char* buffer, size_t max_l return std::string(buffer, len); } + +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; +} + } // namespace Common diff --git a/src/common/string_util.h b/src/common/string_util.h index ceb8df48e..ec0c31a24 100644 --- a/src/common/string_util.h +++ b/src/common/string_util.h @@ -134,4 +134,17 @@ bool ComparePartialString(InIt begin, InIt end, const char* other) { * NUL-terminated then the string ends at max_len characters. */ std::string StringFromFixedZeroTerminatedBuffer(const char* buffer, size_t max_len); + +/** + * Attempts to trim an arbitrary prefix from `path`, leaving only the part starting at `root`. It's + * intended to be used to strip a system-specific build directory from the `__FILE__` macro, + * leaving only the path relative to the sources root. + * + * @param path The input file path as a null-terminated string + * @param root The name of the root source directory as a null-terminated string. Path up to and + * including the last occurrence of this name will be stripped + * @return A pointer to the same string passed as `path`, but starting at the trimmed portion + */ +const char* TrimSourcePath(const char* path, const char* root = "src"); + } // namespace Common From 47f96fe13ae54d17c83de4e565612279530c957e Mon Sep 17 00:00:00 2001 From: Daniel Lim Wee Soong Date: Thu, 22 Mar 2018 18:26:43 +0800 Subject: [PATCH 2/3] Change "yuzu starting..." to be logged with the new macro Just as a proof that it works --- src/yuzu/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index e5252abdc..9b2047e04 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp @@ -387,7 +387,7 @@ bool GMainWindow::LoadROM(const QString& filename) { } void GMainWindow::BootGame(const QString& filename) { - LOG_INFO(Frontend, "yuzu starting..."); + NGLOG_INFO(Frontend, "yuzu starting..."); StoreRecentFile(filename); // Put the filename on top of the list if (!LoadROM(filename)) From 8529d84f31f94502d97a43a723275049c2cb79d7 Mon Sep 17 00:00:00 2001 From: Daniel Lim Wee Soong Date: Thu, 22 Mar 2018 21:53:51 +0800 Subject: [PATCH 3/3] Remove dependency chrono Earlier chrono was included but after some code changed it was no longer needed Forgot to remove it so I'm removing it now --- src/common/logging/log.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/common/logging/log.h b/src/common/logging/log.h index 31fa932fb..7f6d2ade8 100644 --- a/src/common/logging/log.h +++ b/src/common/logging/log.h @@ -4,7 +4,6 @@ #pragma once -#include #include #include "common/common_types.h"