Logging: Changed to a class based logger like the original logger

This commit is contained in:
James Rowe 2017-05-30 21:50:33 -06:00
parent f9d6f7bc32
commit 8fed8e439d
6 changed files with 57 additions and 85 deletions

View File

@ -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);
}

View File

@ -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));
}

View File

@ -1,10 +1,13 @@
#include <spdlog/spdlog.h>
#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<Formatter>());
std::vector<spdlog::sink_ptr> sinks;
// Define the sinks to be passed to the loggers
// true means truncate file
auto file_sink = std::make_shared<spdlog::sinks::simple_file_sink_mt>("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<spdlog::sinks::ansicolor_sink>(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<u8>(Log::Class::Count); ++log_class) {
spdlog::create(GetLogClassName(static_cast<Log::Class>(log_class)), begin(sinks),
end(sinks));
}
}
SpdLogBackend::~SpdLogBackend() {
spdlog::drop_all();
}
const std::shared_ptr<spdlog::logger>& 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

View File

@ -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<spdlog::logger>& GetLogger(u32 logger) const;
const SpdLogBackend& operator=(SpdLogBackend const&) = delete;
private:
SpdLogBackend();
~SpdLogBackend();
std::vector<std::shared_ptr<spdlog::logger>> loggers;
std::vector<spdlog::sink_ptr> sinks;
};
} // namespace Log

View File

@ -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<std::chrono::microseconds>(steady_clock::now() - time_origin);
unsigned int time_seconds = static_cast<unsigned int>(timestamp.count() / 1000000);
unsigned int time_fractional = static_cast<unsigned int>(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

View File

@ -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 <typename Arg1, typename... Args>
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<sizeof...(Args)> ArgArray;
typename ArgArray::Type array{ArgArray::template make<fmt::BasicFormatter<char>>(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__)