Skip to content
Permalink
Browse files
Merge pull request #9173 from lioncash/fmtlog
Common/Log: Add basic fmt-capable functions to the interface.
  • Loading branch information
leoetlino committed Oct 21, 2020
2 parents f265c41 + 425f2aa commit 09e87b7
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 26 deletions.
@@ -18,19 +18,19 @@ static ptrdiff_t s_path_cutoff_point = 0;

static void LogCallback(const char* format, ...)
{
if (!Common::Log::LogManager::GetInstance())
auto* instance = Common::Log::LogManager::GetInstance();
if (instance == nullptr)
return;

va_list args;
va_start(args, format);

const char* filename = va_arg(args, const char*) + s_path_cutoff_point;
int lineno = va_arg(args, int);
std::string adapted_format(StripSpaces(format + strlen("%s:%d:")));

Common::Log::LogManager::GetInstance()->LogWithFullPath(
Common::Log::LNOTICE, Common::Log::AUDIO, filename, lineno, adapted_format.c_str(), args);
const int lineno = va_arg(args, int);
const std::string adapted_format(StripSpaces(format + strlen("%s:%d:")));
const std::string message = StringFromFormatV(adapted_format.c_str(), args);
va_end(args);

instance->Log(Common::Log::LNOTICE, Common::Log::AUDIO, filename, lineno, message.c_str());
}

static void DestroyContext(cubeb* ctx)
@@ -4,6 +4,9 @@

#pragma once

#include <fmt/format.h>
#include <string_view>

namespace Common::Log
{
enum LOG_TYPE
@@ -72,8 +75,17 @@ enum LOG_LEVELS

static const char LOG_LEVEL_TO_CHAR[7] = "-NEWID";

void GenericLog(Common::Log::LOG_LEVELS level, Common::Log::LOG_TYPE type, const char* file,
int line, const char* fmt, ...)
void GenericLogFmtImpl(LOG_LEVELS level, LOG_TYPE type, const char* file, int line,
std::string_view format, const fmt::format_args& args);

template <typename... Args>
void GenericLogFmt(LOG_LEVELS level, LOG_TYPE type, const char* file, int line,
std::string_view format, const Args&... args)
{
GenericLogFmtImpl(level, type, file, line, format, fmt::make_format_args(args...));
}

void GenericLog(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, const char* fmt, ...)
#ifdef __GNUC__
__attribute__((format(printf, 5, 6)))
#endif
@@ -121,3 +133,38 @@ void GenericLog(Common::Log::LOG_LEVELS level, Common::Log::LOG_TYPE type, const
{ \
GENERIC_LOG(Common::Log::t, Common::Log::LDEBUG, __VA_ARGS__); \
} while (0)

// fmtlib capable API

#define GENERIC_LOG_FMT(t, v, ...) \
do \
{ \
if (v <= MAX_LOGLEVEL) \
Common::Log::GenericLogFmt(v, t, __FILE__, __LINE__, __VA_ARGS__); \
} while (0)

#define ERROR_LOG_FMT(t, ...) \
do \
{ \
GENERIC_LOG_FMT(Common::Log::t, Common::Log::LERROR, __VA_ARGS__); \
} while (0)
#define WARN_LOG_FMT(t, ...) \
do \
{ \
GENERIC_LOG_FMT(Common::Log::t, Common::Log::LWARNING, __VA_ARGS__); \
} while (0)
#define NOTICE_LOG_FMT(t, ...) \
do \
{ \
GENERIC_LOG_FMT(Common::Log::t, Common::Log::LNOTICE, __VA_ARGS__); \
} while (0)
#define INFO_LOG_FMT(t, ...) \
do \
{ \
GENERIC_LOG_FMT(Common::Log::t, Common::Log::LINFO, __VA_ARGS__); \
} while (0)
#define DEBUG_LOG_FMT(t, ...) \
do \
{ \
GENERIC_LOG_FMT(Common::Log::t, Common::Log::LDEBUG, __VA_ARGS__); \
} while (0)
@@ -64,11 +64,34 @@ class FileLogListener : public LogListener

void GenericLog(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, const char* fmt, ...)
{
auto* instance = LogManager::GetInstance();
if (instance == nullptr)
return;

if (!instance->IsEnabled(type, level))
return;

va_list args;
va_start(args, fmt);
if (LogManager::GetInstance())
LogManager::GetInstance()->Log(level, type, file, line, fmt, args);
char message[MAX_MSGLEN];
CharArrayFromFormatV(message, MAX_MSGLEN, fmt, args);
va_end(args);

instance->Log(level, type, file, line, message);
}

void GenericLogFmtImpl(LOG_LEVELS level, LOG_TYPE type, const char* file, int line,
std::string_view format, const fmt::format_args& args)
{
auto* instance = LogManager::GetInstance();
if (instance == nullptr)
return;

if (!instance->IsEnabled(type, level))
return;

const auto message = fmt::vformat(format, args);
instance->Log(level, type, file, line, message.c_str());
}

static size_t DeterminePathCutOffPoint()
@@ -196,27 +219,26 @@ void LogManager::SaveSettings()
}

void LogManager::Log(LOG_LEVELS level, LOG_TYPE type, const char* file, int line,
const char* format, va_list args)
{
return LogWithFullPath(level, type, file + m_path_cutoff_point, line, format, args);
}

void LogManager::LogWithFullPath(LOG_LEVELS level, LOG_TYPE type, const char* file, int line,
const char* format, va_list args)
const char* message)
{
if (!IsEnabled(type, level) || !static_cast<bool>(m_listener_ids))
return;

char temp[MAX_MSGLEN];
CharArrayFromFormatV(temp, MAX_MSGLEN, format, args);
LogWithFullPath(level, type, file + m_path_cutoff_point, line, message);
}

void LogManager::LogWithFullPath(LOG_LEVELS level, LOG_TYPE type, const char* file, int line,
const char* message)
{
const std::string msg =
fmt::format("{} {}:{} {}[{}]: {}\n", Common::Timer::GetTimeFormatted(), file, line,
LOG_LEVEL_TO_CHAR[static_cast<int>(level)], GetShortName(type), temp);
LOG_LEVEL_TO_CHAR[static_cast<int>(level)], GetShortName(type), message);

for (auto listener_id : m_listener_ids)
for (const auto listener_id : m_listener_ids)
{
if (m_listeners[listener_id])
m_listeners[listener_id]->Log(level, msg.c_str());
}
}

LOG_LEVELS LogManager::GetLogLevel() const
@@ -38,10 +38,7 @@ class LogManager
static void Init();
static void Shutdown();

void Log(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, const char* fmt,
va_list args);
void LogWithFullPath(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, const char* fmt,
va_list args);
void Log(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, const char* message);

LOG_LEVELS GetLogLevel() const;
void SetLogLevel(LOG_LEVELS level);
@@ -76,6 +73,9 @@ class LogManager
LogManager(LogManager&&) = delete;
LogManager& operator=(LogManager&&) = delete;

void LogWithFullPath(LOG_LEVELS level, LOG_TYPE type, const char* file, int line,
const char* message);

LOG_LEVELS m_level;
std::array<LogContainer, NUMBER_OF_LOGS> m_log{};
std::array<LogListener*, LogListener::NUMBER_OF_LISTENERS> m_listeners{};

0 comments on commit 09e87b7

Please sign in to comment.