From 8fed8e439d0dfbd0d8485ae47c55d4ce1d4c0e8a Mon Sep 17 00:00:00 2001 From: James Rowe Date: Tue, 30 May 2017 21:50:33 -0600 Subject: [PATCH] Logging: Changed to a class based logger like the original logger --- src/citra_qt/game_list.cpp | 8 ++-- src/citra_qt/main.cpp | 10 ++--- src/common/logging/backend_spdlog.cpp | 44 ++++++++++----------- src/common/logging/backend_spdlog.h | 12 +----- src/common/logging/formatter.cpp | 12 +++--- src/common/logging/log.h | 56 ++++++++++----------------- 6 files changed, 57 insertions(+), 85 deletions(-) diff --git a/src/citra_qt/game_list.cpp b/src/citra_qt/game_list.cpp index bf1b11940..35ac7b47e 100644 --- a/src/citra_qt/game_list.cpp +++ b/src/citra_qt/game_list.cpp @@ -16,8 +16,6 @@ #include "game_list_p.h" #include "ui_settings.h" -REGISTER_LOGGER("Game List"); - GameList::SearchField::KeyReleaseEater::KeyReleaseEater(GameList* gamelist) { this->gamelist = gamelist; edit_filter_text_old = ""; @@ -200,7 +198,6 @@ GameList::GameList(GMainWindow* parent) : QWidget{parent} { watcher = new QFileSystemWatcher(this); connect(watcher, &QFileSystemWatcher::directoryChanged, this, &GameList::RefreshGameDirectory); - SPDLOG_WARNING("Test! {1} {0}", "world!", "Hello"); this->main_window = parent; layout = new QVBoxLayout; tree_view = new QTreeView; @@ -319,7 +316,8 @@ void GameList::PopupContextMenu(const QPoint& menu_location) { void GameList::PopulateAsync(const QString& dir_path, bool deep_scan) { if (!FileUtil::Exists(dir_path.toStdString()) || !FileUtil::IsDirectory(dir_path.toStdString())) { - SPDLOG_ERROR("Could not find game list folder at {}", dir_path.toLocal8Bit().data()); + SPDLOG_ERROR(Frontend, "Could not find game list folder at {}", + dir_path.toLocal8Bit().data()); search_field->setFilterResult(0, 0); return; } @@ -369,7 +367,7 @@ static bool HasSupportedFileExtension(const std::string& file_name) { void GameList::RefreshGameDirectory() { if (!UISettings::values.gamedir.isEmpty() && current_worker != nullptr) { - SPDLOG_INFO("Change detected in the games directory. Reloading game list."); + SPDLOG_INFO(Frontend, "Change detected in the games directory. Reloading game list."); search_field->clear(); PopulateAsync(UISettings::values.gamedir, UISettings::values.gamedir_deepscan); } diff --git a/src/citra_qt/main.cpp b/src/citra_qt/main.cpp index 80a04d836..ec94a0f59 100644 --- a/src/citra_qt/main.cpp +++ b/src/citra_qt/main.cpp @@ -48,8 +48,6 @@ Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin); #endif -REGISTER_LOGGER("Main"); - GMainWindow::GMainWindow() : config(new Config()), emu_thread(nullptr) { Pica::g_debug_context = Pica::DebugContext::Construct(); setAcceptDrops(true); @@ -323,13 +321,13 @@ bool GMainWindow::LoadROM(const QString& filename) { if (result != Core::System::ResultStatus::Success) { switch (result) { case Core::System::ResultStatus::ErrorGetLoader: - SPDLOG_CRITICAL("Failed to obtain loader for {}!", filename.toStdString()); + SPDLOG_CRITICAL(Frontend, "Failed to obtain loader for {}!", filename.toStdString()); QMessageBox::critical(this, tr("Error while loading ROM!"), tr("The ROM format is not supported.")); break; case Core::System::ResultStatus::ErrorSystemMode: - SPDLOG_CRITICAL("Failed to load ROM!"); + SPDLOG_CRITICAL(Frontend, "Failed to load ROM!"); QMessageBox::critical(this, tr("Error while loading ROM!"), tr("Could not determine the system mode.")); break; @@ -378,7 +376,7 @@ bool GMainWindow::LoadROM(const QString& filename) { } void GMainWindow::BootGame(const QString& filename) { - SPDLOG_INFO("Citra starting..."); + SPDLOG_INFO(Frontend, "Citra starting..."); StoreRecentFile(filename); // Put the filename on top of the list if (!LoadROM(filename)) @@ -504,7 +502,7 @@ void GMainWindow::OnGameListOpenSaveFolder(u64 program_id) { return; } - SPDLOG_INFO("Opening save data path for program_id={0:#x}", program_id); + SPDLOG_INFO(Frontend, "Opening save data path for program_id={0:#x}", program_id); QDesktopServices::openUrl(QUrl::fromLocalFile(qpath)); } diff --git a/src/common/logging/backend_spdlog.cpp b/src/common/logging/backend_spdlog.cpp index 102903f2e..f3a7ef5f7 100644 --- a/src/common/logging/backend_spdlog.cpp +++ b/src/common/logging/backend_spdlog.cpp @@ -1,10 +1,13 @@ #include #include "common/assert.h" +#include "common/logging/backend.h" #include "common/logging/backend_spdlog.h" #include "common/logging/formatter.h" #include "common/string_util.h" +namespace Log { + static spdlog::level::level_enum GetLevel(Log::Level log_level) { switch (log_level) { case Log::Level::Trace: @@ -21,14 +24,10 @@ static spdlog::level::level_enum GetLevel(Log::Level log_level) { return spdlog::level::critical; default: UNREACHABLE(); - break; } - return spdlog::level::off; } -namespace Log { - -SpdLogBackend& SpdLogBackend::instance() { +SpdLogBackend& SpdLogBackend::Instance() { static SpdLogBackend instance; return instance; } @@ -37,6 +36,7 @@ SpdLogBackend::SpdLogBackend() { // setup the custom citra formatter spdlog::set_formatter(std::make_shared()); + std::vector sinks; // Define the sinks to be passed to the loggers // true means truncate file auto file_sink = std::make_shared("citra_log.txt", true); @@ -46,29 +46,27 @@ SpdLogBackend::SpdLogBackend() { auto stderr_sink = spdlog::sinks::stderr_sink_mt::instance(); auto color_sink = std::make_shared(stderr_sink); #endif - sinks.push_back(file_sink); - sinks.push_back(color_sink); + sinks.push_back(std::move(file_sink)); + sinks.push_back(std::move(color_sink)); + + // register all of loggers with spdlog + for (u8 log_class = 0; log_class != static_cast(Log::Class::Count); ++log_class) { + spdlog::create(GetLogClassName(static_cast(log_class)), begin(sinks), + end(sinks)); + } } SpdLogBackend::~SpdLogBackend() { spdlog::drop_all(); } -const std::shared_ptr& SpdLogBackend::GetLogger(u32 logger) const { - return loggers[logger]; -} - -u32 SpdLogBackend::RegisterLogger(const char* class_name) { - loggers.push_back(spdlog::create(class_name, sinks.begin(), sinks.end())); - return loggers.size() - 1; -} - -void SpdLogImpl(u32 logger, Level log_level, const char* format, fmt::ArgList& args) { - auto log = SpdLogBackend::instance().GetLogger(logger); - log->log(GetLevel(log_level), format, args); -} - -u32 RegisterLogger(const char* class_name) { - return SpdLogBackend::instance().RegisterLogger(class_name); +void SpdLogImpl(Class log_class, Level log_level, const char* file, int line_num, + const char* function, const char* format, fmt::ArgList args) { + SpdLogBackend::Instance(); + fmt::MemoryWriter formatting_buffer; + formatting_buffer << Common::TrimSourcePath(file) << ':' << function << ':' << line_num << ": " + << format; + auto& logger = spdlog::get(GetLogClassName(log_class)); + logger->log(GetLevel(log_level), formatting_buffer.c_str(), args); } }; // namespace Log diff --git a/src/common/logging/backend_spdlog.h b/src/common/logging/backend_spdlog.h index db1a3229a..f53f974f8 100644 --- a/src/common/logging/backend_spdlog.h +++ b/src/common/logging/backend_spdlog.h @@ -7,21 +7,13 @@ namespace Log { class SpdLogBackend { public: - static SpdLogBackend& instance(); + static SpdLogBackend& Instance(); SpdLogBackend(SpdLogBackend const&) = delete; - SpdLogBackend& operator=(SpdLogBackend const&) = delete; - - u32 RegisterLogger(const char* class_name); - - const std::shared_ptr& GetLogger(u32 logger) const; + const SpdLogBackend& operator=(SpdLogBackend const&) = delete; private: SpdLogBackend(); - ~SpdLogBackend(); - - std::vector> loggers; - std::vector sinks; }; } // namespace Log \ No newline at end of file diff --git a/src/common/logging/formatter.cpp b/src/common/logging/formatter.cpp index 56f6c6998..b3164de7e 100644 --- a/src/common/logging/formatter.cpp +++ b/src/common/logging/formatter.cpp @@ -3,6 +3,8 @@ #include "common/assert.h" #include "common/logging/formatter.h" +namespace Log { + static const char* GetLevelName(spdlog::level_t log_level) { switch (log_level) { case spdlog::level::trace: @@ -23,24 +25,22 @@ static const char* GetLevelName(spdlog::level_t log_level) { } } -namespace Log { - void Formatter::format(spdlog::details::log_msg& msg) { using std::chrono::steady_clock; using std::chrono::duration_cast; - static steady_clock::time_point time_origin = steady_clock::now(); + static const steady_clock::time_point time_origin = steady_clock::now(); auto timestamp = duration_cast(steady_clock::now() - time_origin); - unsigned int time_seconds = static_cast(timestamp.count() / 1000000); - unsigned int time_fractional = static_cast(timestamp.count() % 1000000); + const auto time_seconds = timestamp.count() / 1000000; + const auto time_fractional = timestamp.count() % 1000000; msg.formatted << '[' << fmt::pad(time_seconds, 4, ' ') << '.' << fmt::pad(time_fractional, 6, '0') << "] "; msg.formatted << *msg.logger_name << " <" << GetLevelName(msg.level) << "> "; msg.formatted << fmt::StringRef(msg.raw.data(), msg.raw.size()); - msg.formatted.write(spdlog::details::os::eol, spdlog::details::os::eol_size); + msg.formatted << '\n'; } } // namespace Log \ No newline at end of file diff --git a/src/common/logging/log.h b/src/common/logging/log.h index 32154cde3..383f895cb 100644 --- a/src/common/logging/log.h +++ b/src/common/logging/log.h @@ -107,21 +107,10 @@ void LogMessage(Class log_class, Level log_level, const char* filename, unsigned #endif ; -void SpdLogImpl(u32 logger, Level log_level, const char* format, fmt::ArgList& args); +void SpdLogImpl(Class log_class, Level log_level, const char* file, int line_num, + const char* function, const char* format, fmt::ArgList args); -template -void SpdLogMessage(u32 logger, Level log_level, const char* filename, unsigned int line_nr, - const char* function, const Arg1& format, const Args&... args) { - typedef fmt::internal::ArgArray ArgArray; - typename ArgArray::Type array{ArgArray::template make>(args)...}; - fmt::MemoryWriter formatting_buffer; - formatting_buffer << Common::TrimSourcePath(filename) << ':' << function << ':' << line_nr - << ": " << format; - SpdLogImpl(logger, log_level, formatting_buffer.c_str(), - fmt::ArgList(fmt::internal::make_type(args...), array)); -} - -u32 RegisterLogger(const char* class_name); +FMT_VARIADIC(void, SpdLogImpl, Class, Level, const char*, int, const char*, const char*) } // namespace Log @@ -147,29 +136,26 @@ u32 RegisterLogger(const char* class_name); LOG_GENERIC(::Log::Class::log_class, ::Log::Level::Critical, __VA_ARGS__) // Define the spdlog level macros - -#define REGISTER_LOGGER(class_name) static u32 _logger = ::Log::RegisterLogger(class_name) - #ifdef _DEBUG -#define SPDLOG_TRACE(fmt, ...) \ - ::Log::SpdLogMessage(_logger, ::Log::Level::Trace, __FILE__, __LINE__, __func__, fmt, \ +#define SPDLOG_TRACE(log_class, fmt, ...) \ + ::Log::SpdLogMessage(log_class, ::Log::Level::Trace, __FILE__, __LINE__, __func__, fmt, \ ##__VA_ARGS__) #else -#define SPDLOG_TRACE(fmt, ...) (void(0)) +#define SPDLOG_TRACE(log_class, fmt, ...) (void(0)) #endif -#define SPDLOG_DEBUG(fmt, ...) \ - ::Log::SpdLogMessage(_logger, ::Log::Level::Debug, __FILE__, __LINE__, __func__, fmt, \ - ##__VA_ARGS__) -#define SPDLOG_INFO(fmt, ...) \ - ::Log::SpdLogMessage(_logger, ::Log::Level::Info, __FILE__, __LINE__, __func__, fmt, \ - ##__VA_ARGS__) -#define SPDLOG_WARNING(fmt, ...) \ - ::Log::SpdLogMessage(_logger, ::Log::Level::Warning, __FILE__, __LINE__, __func__, fmt, \ - ##__VA_ARGS__) -#define SPDLOG_ERROR(fmt, ...) \ - ::Log::SpdLogMessage(_logger, ::Log::Level::Error, __FILE__, __LINE__, __func__, fmt, \ - ##__VA_ARGS__) -#define SPDLOG_CRITICAL(fmt, ...) \ - ::Log::SpdLogMessage(_logger, ::Log::Level::Critical, __FILE__, __LINE__, __func__, fmt, \ - ##__VA_ARGS__) +#define SPDLOG_DEBUG(log_class, fmt, ...) \ + ::Log::SpdLogImpl(::Log::Class::log_class, ::Log::Level::Debug, __FILE__, __LINE__, __func__, \ + fmt, ##__VA_ARGS__) +#define SPDLOG_INFO(log_class, fmt, ...) \ + ::Log::SpdLogImpl(::Log::Class::log_class, ::Log::Level::Info, __FILE__, __LINE__, __func__, \ + fmt, ##__VA_ARGS__) +#define SPDLOG_WARNING(log_class, fmt, ...) \ + ::Log::SpdLogImpl(::Log::Class::log_class, ::Log::Level::Warning, __FILE__, __LINE__, \ + __func__, fmt, ##__VA_ARGS__) +#define SPDLOG_ERROR(log_class, fmt, ...) \ + ::Log::SpdLogImpl(::Log::Class::log_class, ::Log::Level::Error, __FILE__, __LINE__, __func__, \ + fmt, ##__VA_ARGS__) +#define SPDLOG_CRITICAL(log_class, fmt, ...) \ + ::Log::SpdLogImpl(::Log::Class::log_class, ::Log::Level::Critical, __FILE__, __LINE__, \ + __func__, fmt, ##__VA_ARGS__)