From 3b558eebee54fa4bc9d1a7bb428d4bd33e1b817e Mon Sep 17 00:00:00 2001 From: Daniel Lim Wee Soong Date: Thu, 22 Mar 2018 18:21:29 +0800 Subject: 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/logging/backend.cpp | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) (limited to 'src/common/logging/backend.cpp') 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); } -- cgit v1.2.3