Skip to content

Commit

Permalink
spdlog
Browse files Browse the repository at this point in the history
  • Loading branch information
scribam committed Sep 25, 2023
1 parent ed59cbd commit ba3bf71
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 42 deletions.
40 changes: 35 additions & 5 deletions core/log/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <string>
#include <vector>

#if 0

class custom_path_formatter_flag : public spdlog::custom_flag_formatter
{
public:
Expand Down Expand Up @@ -86,9 +88,12 @@ class custom_time_formatter_flag : public spdlog::custom_flag_formatter
}
};
#endif
#endif

namespace LogManager
{
std::array<std::shared_ptr<spdlog::logger>, 24> loggers;

void Init([[maybe_unused]] void *log_cb)
{
std::vector<spdlog::sink_ptr> sinks;
Expand All @@ -109,18 +114,42 @@ namespace LogManager
}
#endif

for (const auto &name: names) {
#define CREATE_LOGGER(n) loggers[Name::n] = std::make_shared<spdlog::logger>(#n, sinks.begin(), sinks.end())
CREATE_LOGGER(AICA);
CREATE_LOGGER(AICA_ARM);
CREATE_LOGGER(AUDIO);
CREATE_LOGGER(BOOT);
CREATE_LOGGER(COMMON);
CREATE_LOGGER(DYNAREC);
CREATE_LOGGER(FLASHROM);
CREATE_LOGGER(GDROM);
CREATE_LOGGER(HOLLY);
CREATE_LOGGER(INPUT);
CREATE_LOGGER(INTERPRETER);
CREATE_LOGGER(JVS);
CREATE_LOGGER(MAPLE);
CREATE_LOGGER(MEMORY);
CREATE_LOGGER(MODEM);
CREATE_LOGGER(NAOMI);
CREATE_LOGGER(NETWORK);
CREATE_LOGGER(PROFILER);
CREATE_LOGGER(PVR);
CREATE_LOGGER(REIOS);
CREATE_LOGGER(RENDERER);
CREATE_LOGGER(SAVESTATE);
CREATE_LOGGER(SH4);
CREATE_LOGGER(VMEM);

for (const auto &logger: loggers) {
#ifdef LIBRETRO
spdlog::level::level_enum level = spdlog::level::trace;
#else
spdlog::level::level_enum level = static_cast<spdlog::level::level_enum>(cfgLoadInt("log", name.data(), spdlog::level::info));
spdlog::level::level_enum level = static_cast<spdlog::level::level_enum>(cfgLoadInt("log", logger->name(), spdlog::level::info));
#endif

auto logger = std::make_shared<spdlog::logger>(name.data(), sinks.begin(), sinks.end());
logger->set_level(level);
spdlog::register_logger(logger);
}

#if 0
auto formatter = spdlog::details::make_unique<spdlog::pattern_formatter>();
formatter->add_flag<custom_path_formatter_flag>('j');
#ifdef LIBRETRO
Expand All @@ -130,6 +159,7 @@ namespace LogManager
formatter->set_pattern("%k %j %L[%n]: %v");
#endif
spdlog::set_formatter(std::move(formatter));
#endif
}

void Shutdown()
Expand Down
73 changes: 38 additions & 35 deletions core/log/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,51 @@

namespace LogManager
{
constexpr std::array<std::string_view, 24> names = {
"AICA",
"AICA_ARM",
"AUDIO",
"BOOT",
"COMMON",
"DYNAREC",
"FLASHROM",
"GDROM",
"HOLLY",
"INPUT",
"JVS",
"MAPLE",
"INTERPRETER",
"MEMORY",
"NETWORK",
"PROFILER",
"VMEM",
"MODEM",
"NAOMI",
"PVR",
"REIOS",
"RENDERER",
"SAVESTATE",
"SH4"
enum Name
{
AICA,
AICA_ARM,
AUDIO,
BOOT,
COMMON,
DYNAREC,
FLASHROM,
GDROM,
HOLLY,
INPUT,
INTERPRETER,
JVS,
MAPLE,
MEMORY,
MODEM,
NAOMI,
NETWORK,
PROFILER,
PVR,
REIOS,
RENDERER,
SAVESTATE,
SH4,
VMEM
};

extern std::array<std::shared_ptr<spdlog::logger>, 24> loggers;

void Init(void* log_cb = nullptr);
void Shutdown();
};

#define ERROR_LOG(t, ...) \
SPDLOG_LOGGER_ERROR(spdlog::get(#t), fmt::sprintf(__VA_ARGS__))
#define ERROR_LOG(n, ...) \
SPDLOG_LOGGER_ERROR(LogManager::loggers[LogManager::Name::n], fmt::sprintf(__VA_ARGS__))

#define WARN_LOG(t, ...) \
SPDLOG_LOGGER_WARN(spdlog::get(#t), fmt::sprintf(__VA_ARGS__))
#define WARN_LOG(n, ...) \
SPDLOG_LOGGER_WARN(LogManager::loggers[LogManager::Name::n], fmt::sprintf(__VA_ARGS__))

#define NOTICE_LOG(t, ...) \
SPDLOG_LOGGER_INFO(spdlog::get(#t), fmt::sprintf(__VA_ARGS__))
#define NOTICE_LOG(n, ...) \
SPDLOG_LOGGER_INFO(LogManager::loggers[LogManager::Name::n], fmt::sprintf(__VA_ARGS__))

#define INFO_LOG(t, ...) \
SPDLOG_LOGGER_INFO(spdlog::get(#t), fmt::sprintf(__VA_ARGS__))
#define INFO_LOG(n, ...) \
SPDLOG_LOGGER_INFO(LogManager::loggers[LogManager::Name::n], fmt::sprintf(__VA_ARGS__))

#define DEBUG_LOG(t, ...) \
SPDLOG_LOGGER_DEBUG(spdlog::get(#t), fmt::sprintf(__VA_ARGS__))
#define DEBUG_LOG(n, ...) \
SPDLOG_LOGGER_DEBUG(LogManager::loggers[LogManager::Name::n], fmt::sprintf(__VA_ARGS__))
3 changes: 1 addition & 2 deletions core/rend/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1344,8 +1344,7 @@ static inline void gui_debug_tab()
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, normal_padding);
header("Logging");
{
for (const auto &name: LogManager::names) {
auto logger = spdlog::get(name.data());
for (const auto &logger: LogManager::loggers) {
if (ImGui::BeginCombo(logger->name().c_str(), spdlog::level::to_string_view(logger->level()).data())) {
for (int i = 0; i < spdlog::level::level_enum::n_levels; i++) {
auto level = static_cast<spdlog::level::level_enum>(i);
Expand Down

0 comments on commit ba3bf71

Please sign in to comment.