Log: Simplify macros

This commit is contained in:
Stenzek
2024-05-23 20:55:28 +10:00
parent 792717e03e
commit 4e922a34a7
144 changed files with 2273 additions and 2363 deletions

View File

@ -283,11 +283,11 @@ public:
#if defined(_WIN32)
// delete the temporary file
if (!DeleteFileW(FileSystem::GetWin32Path(m_temporaryFileName).c_str()))
Log_WarningFmt("Failed to delete temporary file '{}'", m_temporaryFileName);
WARNING_LOG("Failed to delete temporary file '{}'", m_temporaryFileName);
#else
// delete the temporary file
if (remove(m_temporaryFileName.c_str()) < 0)
Log_WarningFmt("Failed to delete temporary file '{}'", m_temporaryFileName);
WARNING_LOG("Failed to delete temporary file '{}'", m_temporaryFileName);
#endif
}
else if (!m_committed)
@ -311,7 +311,7 @@ public:
if (!MoveFileExW(FileSystem::GetWin32Path(m_temporaryFileName).c_str(),
FileSystem::GetWin32Path(m_originalFileName).c_str(), MOVEFILE_REPLACE_EXISTING))
{
Log_WarningFmt("Failed to rename temporary file '{}' to '{}'", m_temporaryFileName, m_originalFileName);
WARNING_LOG("Failed to rename temporary file '{}' to '{}'", m_temporaryFileName, m_originalFileName);
m_discarded = true;
}
else
@ -322,7 +322,7 @@ public:
// move the atomic file name to the original file name
if (rename(m_temporaryFileName.c_str(), m_originalFileName.c_str()) < 0)
{
Log_WarningFmt("Failed to rename temporary file '{}' to '{}'", m_temporaryFileName, m_originalFileName);
WARNING_LOG("Failed to rename temporary file '{}' to '{}'", m_temporaryFileName, m_originalFileName);
m_discarded = true;
}
else

View File

@ -30,7 +30,7 @@ DynamicLibrary::DynamicLibrary(const char* filename)
{
Error error;
if (!Open(filename, &error))
Log_ErrorPrint(error.GetDescription());
ERROR_LOG(error.GetDescription());
}
DynamicLibrary::DynamicLibrary(DynamicLibrary&& move) : m_handle(move.m_handle)

View File

@ -266,7 +266,7 @@ bool FileSystem::GetWin32Path(std::wstring* dest, std::string_view str)
}
else [[unlikely]]
{
Log_ErrorFmt("PathCchCanonicalizeEx() returned {:08X}", static_cast<unsigned>(hr));
ERROR_LOG("PathCchCanonicalizeEx() returned {:08X}", static_cast<unsigned>(hr));
_freea(wstr_buf);
return false;
}
@ -2407,13 +2407,13 @@ static bool SetLock(int fd, bool lock)
const off_t offs = lseek(fd, 0, SEEK_CUR);
if (offs < 0)
{
Log_ErrorFmt("lseek({}) failed: {}", fd, errno);
ERROR_LOG("lseek({}) failed: {}", fd, errno);
return false;
}
if (offs != 0 && lseek(fd, 0, SEEK_SET) < 0)
{
Log_ErrorFmt("lseek({}, 0) failed: {}", fd, errno);
ERROR_LOG("lseek({}, 0) failed: {}", fd, errno);
return false;
}
@ -2422,7 +2422,7 @@ static bool SetLock(int fd, bool lock)
Panic("Repositioning file descriptor after lock failed.");
if (!res)
Log_ErrorFmt("lockf() for {} failed: {}", lock ? "lock" : "unlock", errno);
ERROR_LOG("lockf() for {} failed: {}", lock ? "lock" : "unlock", errno);
return res;
}

View File

@ -59,7 +59,7 @@ static void FormatLogMessageAndPrintW(const char* channelName, const char* funct
const T& callback);
#endif
static const char s_log_level_characters[LOGLEVEL_COUNT] = {'X', 'E', 'W', 'P', 'I', 'V', 'D', 'R', 'B', 'T'};
static const char s_log_level_characters[LOGLEVEL_COUNT] = {'X', 'E', 'W', 'I', 'V', 'D', 'B', 'T'};
static std::vector<RegisteredCallback> s_callbacks;
static std::mutex s_callback_mutex;
@ -154,11 +154,9 @@ ALWAYS_INLINE_RELEASE void Log::FormatLogMessageForDisplay(fmt::memory_buffer& b
"\033[0m"sv, // NONE
"\033[1;31m"sv, // ERROR
"\033[1;33m"sv, // WARNING
"\033[1;35m"sv, // PERF
"\033[1;37m"sv, // INFO
"\033[1;32m"sv, // VERBOSE
"\033[0;37m"sv, // DEV
"\033[1;36m"sv, // PROFILE
"\033[0;32m"sv, // DEBUG
"\033[0;34m"sv, // TRACE
};
@ -174,7 +172,7 @@ ALWAYS_INLINE_RELEASE void Log::FormatLogMessageForDisplay(fmt::memory_buffer& b
// find time since start of process
const float message_time = Log::GetCurrentMessageTime();
if (level <= LOGLEVEL_PERF)
if (functionName)
{
fmt::format_to(appender, "[{:10.4f}] {}{}({}): {}{}{}", message_time, color_start, s_log_level_characters[level],
functionName, message, color_end, message_end);
@ -187,7 +185,7 @@ ALWAYS_INLINE_RELEASE void Log::FormatLogMessageForDisplay(fmt::memory_buffer& b
}
else
{
if (level <= LOGLEVEL_PERF)
if (functionName)
{
fmt::format_to(appender, "{}{}({}): {}{}{}", color_start, s_log_level_characters[level], functionName, message,
color_end, message_end);
@ -480,6 +478,15 @@ ALWAYS_INLINE_RELEASE bool Log::FilterTest(LOGLEVEL level, const char* channelNa
return (level <= s_log_level && s_log_filter.find(channelName) == std::string::npos);
}
void Log::Write(const char* channelName, LOGLEVEL level, std::string_view message)
{
std::unique_lock lock(s_callback_mutex);
if (!FilterTest(level, channelName, lock))
return;
ExecuteCallbacks(channelName, nullptr, level, message, lock);
}
void Log::Write(const char* channelName, const char* functionName, LOGLEVEL level, std::string_view message)
{
std::unique_lock lock(s_callback_mutex);
@ -489,45 +496,16 @@ void Log::Write(const char* channelName, const char* functionName, LOGLEVEL leve
ExecuteCallbacks(channelName, functionName, level, message, lock);
}
void Log::Writef(const char* channelName, const char* functionName, LOGLEVEL level, const char* format, ...)
{
std::va_list ap;
va_start(ap, format);
Writev(channelName, functionName, level, format, ap);
va_end(ap);
}
void Log::Writev(const char* channelName, const char* functionName, LOGLEVEL level, const char* format, va_list ap)
void Log::WriteFmtArgs(const char* channelName, LOGLEVEL level, fmt::string_view fmt, fmt::format_args args)
{
std::unique_lock lock(s_callback_mutex);
if (!FilterTest(level, channelName, lock))
return;
std::va_list apCopy;
va_copy(apCopy, ap);
fmt::memory_buffer buffer;
fmt::vformat_to(std::back_inserter(buffer), fmt, args);
#ifdef _WIN32
u32 requiredSize = static_cast<u32>(_vscprintf(format, apCopy));
#else
u32 requiredSize = std::vsnprintf(nullptr, 0, format, apCopy);
#endif
va_end(apCopy);
if (requiredSize < 512)
{
char buffer[512];
const int len = std::vsnprintf(buffer, countof(buffer), format, ap);
if (len > 0)
ExecuteCallbacks(channelName, functionName, level, std::string_view(buffer, static_cast<size_t>(len)), lock);
}
else
{
char* buffer = new char[requiredSize + 1];
const int len = std::vsnprintf(buffer, requiredSize + 1, format, ap);
if (len > 0)
ExecuteCallbacks(channelName, functionName, level, std::string_view(buffer, static_cast<size_t>(len)), lock);
delete[] buffer;
}
ExecuteCallbacks(channelName, nullptr, level, std::string_view(buffer.data(), buffer.size()), lock);
}
void Log::WriteFmtArgs(const char* channelName, const char* functionName, LOGLEVEL level, fmt::string_view fmt,

View File

@ -14,17 +14,16 @@
enum LOGLEVEL
{
LOGLEVEL_NONE = 0, // Silences all log traffic
LOGLEVEL_ERROR = 1, // "ErrorPrint"
LOGLEVEL_WARNING = 2, // "WarningPrint"
LOGLEVEL_PERF = 3, // "PerfPrint" // TODO: Purge
LOGLEVEL_INFO = 4, // "InfoPrint"
LOGLEVEL_VERBOSE = 5, // "VerbosePrint"
LOGLEVEL_DEV = 6, // "DevPrint"
LOGLEVEL_PROFILE = 7, // "ProfilePrint" // TODO: Purge
LOGLEVEL_DEBUG = 8, // "DebugPrint"
LOGLEVEL_TRACE = 9, // "TracePrint"
LOGLEVEL_COUNT = 10
LOGLEVEL_NONE, // Silences all log traffic
LOGLEVEL_ERROR,
LOGLEVEL_WARNING,
LOGLEVEL_INFO,
LOGLEVEL_VERBOSE,
LOGLEVEL_DEV,
LOGLEVEL_DEBUG,
LOGLEVEL_TRACE,
LOGLEVEL_COUNT
};
namespace Log {
@ -65,91 +64,57 @@ void SetLogLevel(LOGLEVEL level);
void SetLogFilter(std::string_view filter);
// writes a message to the log
void Write(const char* channelName, LOGLEVEL level, std::string_view message);
void Write(const char* channelName, const char* functionName, LOGLEVEL level, std::string_view message);
void Writef(const char* channelName, const char* functionName, LOGLEVEL level, const char* format, ...)
printflike(4, 5);
void Writev(const char* channelName, const char* functionName, LOGLEVEL level, const char* format, va_list ap);
void WriteFmtArgs(const char* channelName, LOGLEVEL level, fmt::string_view fmt, fmt::format_args args);
void WriteFmtArgs(const char* channelName, const char* functionName, LOGLEVEL level, fmt::string_view fmt,
fmt::format_args args);
template<typename... T>
ALWAYS_INLINE static void WriteFmt(const char* channelName, const char* functionName, LOGLEVEL level,
fmt::format_string<T...> fmt, T&&... args)
ALWAYS_INLINE static void FastWrite(const char* channelName, LOGLEVEL level, std::string_view message)
{
if (level <= GetLogLevel())
return WriteFmtArgs(channelName, functionName, level, fmt, fmt::make_format_args(args...));
Write(channelName, level, message);
}
ALWAYS_INLINE static void FastWrite(const char* channelName, const char* functionName, LOGLEVEL level,
std::string_view message)
{
if (level <= GetLogLevel())
Write(channelName, functionName, level, message);
}
template<typename... T>
ALWAYS_INLINE static void FastWrite(const char* channelName, LOGLEVEL level, fmt::format_string<T...> fmt, T&&... args)
{
if (level <= GetLogLevel())
WriteFmtArgs(channelName, level, fmt, fmt::make_format_args(args...));
}
template<typename... T>
ALWAYS_INLINE static void FastWrite(const char* channelName, const char* functionName, LOGLEVEL level,
fmt::format_string<T...> fmt, T&&... args)
{
if (level <= GetLogLevel())
WriteFmtArgs(channelName, functionName, level, fmt, fmt::make_format_args(args...));
}
} // namespace Log
// log wrappers
#define Log_SetChannel(ChannelName) \
[[maybe_unused]] [[maybe_unused]] static const char* ___LogChannel___ = #ChannelName;
#define Log_ErrorPrint(msg) Log::Write(___LogChannel___, __func__, LOGLEVEL_ERROR, msg)
#define Log_ErrorPrintf(...) Log::Writef(___LogChannel___, __func__, LOGLEVEL_ERROR, __VA_ARGS__)
#define Log_ErrorFmt(...) Log::WriteFmt(___LogChannel___, __func__, LOGLEVEL_ERROR, __VA_ARGS__)
#define Log_WarningPrint(msg) Log::Write(___LogChannel___, __func__, LOGLEVEL_WARNING, msg)
#define Log_WarningPrintf(...) Log::Writef(___LogChannel___, __func__, LOGLEVEL_WARNING, __VA_ARGS__)
#define Log_WarningFmt(...) Log::WriteFmt(___LogChannel___, __func__, LOGLEVEL_WARNING, __VA_ARGS__)
#define Log_PerfPrint(msg) Log::Write(___LogChannel___, __func__, LOGLEVEL_PERF, msg)
#define Log_PerfPrintf(...) Log::Writef(___LogChannel___, __func__, LOGLEVEL_PERF, __VA_ARGS__)
#define Log_PerfFmt(...) Log::WriteFmt(___LogChannel___, __func__, LOGLEVEL_PERF, __VA_ARGS__)
#define Log_InfoPrint(msg) Log::Write(___LogChannel___, __func__, LOGLEVEL_INFO, msg)
#define Log_InfoPrintf(...) Log::Writef(___LogChannel___, __func__, LOGLEVEL_INFO, __VA_ARGS__)
#define Log_InfoFmt(...) Log::WriteFmt(___LogChannel___, __func__, LOGLEVEL_INFO, __VA_ARGS__)
#define Log_VerbosePrint(msg) Log::Write(___LogChannel___, __func__, LOGLEVEL_VERBOSE, msg)
#define Log_VerbosePrintf(...) Log::Writef(___LogChannel___, __func__, LOGLEVEL_VERBOSE, __VA_ARGS__)
#define Log_VerboseFmt(...) Log::WriteFmt(___LogChannel___, __func__, LOGLEVEL_VERBOSE, __VA_ARGS__)
#define Log_DevPrint(msg) Log::Write(___LogChannel___, __func__, LOGLEVEL_DEV, msg)
#define Log_DevPrintf(...) Log::Writef(___LogChannel___, __func__, LOGLEVEL_DEV, __VA_ARGS__)
#define Log_DevFmt(...) Log::WriteFmt(___LogChannel___, __func__, LOGLEVEL_DEV, __VA_ARGS__)
#define Log_ProfilePrint(msg) Log::Write(___LogChannel___, __func__, LOGLEVEL_PROFILE, msg)
#define Log_ProfilePrintf(...) Log::Writef(___LogChannel___, __func__, LOGLEVEL_PROFILE, __VA_ARGS__)
#define Log_ProfileFmt(...) Log::WriteFmt(___LogChannel___, __func__, LOGLEVEL_PROFILE, __VA_ARGS__)
#define Log_SetChannel(ChannelName) [[maybe_unused]] static const char* ___LogChannel___ = #ChannelName;
#define Log_ErrorVisible() Log::IsLogVisible(LOGLEVEL_ERROR, ___LogChannel___)
#define Log_WarningVisible() Log::IsLogVisible(LOGLEVEL_WARNING, ___LogChannel___)
#define Log_PerfVisible() Log::IsLogVisible(LOGLEVEL_PERF, ___LogChannel___)
#define Log_InfoVisible() Log::IsLogVisible(LOGLEVEL_INFO, ___LogChannel___)
#define Log_VerboseVisible() Log::IsLogVisible(LOGLEVEL_VERBOSE, ___LogChannel___)
#define Log_DevVisible() Log::IsLogVisible(LOGLEVEL_DEV, ___LogChannel___)
#define Log_ProfileVisible() Log::IsLogVisible(LOGLEVEL_PROFILE, ___LogChannel___)
#define ERROR_LOG(...) Log::FastWrite(___LogChannel___, __func__, LOGLEVEL_ERROR, __VA_ARGS__)
#define WARNING_LOG(...) Log::FastWrite(___LogChannel___, __func__, LOGLEVEL_WARNING, __VA_ARGS__)
#define INFO_LOG(...) Log::FastWrite(___LogChannel___, LOGLEVEL_INFO, __VA_ARGS__)
#define VERBOSE_LOG(...) Log::FastWrite(___LogChannel___, LOGLEVEL_VERBOSE, __VA_ARGS__)
#define DEV_LOG(...) Log::FastWrite(___LogChannel___, LOGLEVEL_DEV, __VA_ARGS__)
#ifdef _DEBUG
#define Log_DebugPrint(msg) Log::Write(___LogChannel___, __func__, LOGLEVEL_DEBUG, msg)
#define Log_DebugPrintf(...) Log::Writef(___LogChannel___, __func__, LOGLEVEL_DEBUG, __VA_ARGS__)
#define Log_DebugFmt(...) Log::WriteFmt(___LogChannel___, __func__, LOGLEVEL_DEBUG, __VA_ARGS__)
#define Log_TracePrint(msg) Log::Write(___LogChannel___, __func__, LOGLEVEL_TRACE, msg)
#define Log_TracePrintf(...) Log::Writef(___LogChannel___, __func__, LOGLEVEL_TRACE, __VA_ARGS__)
#define Log_TraceFmt(...) Log::WriteFmt(___LogChannel___, __func__, LOGLEVEL_TRACE, __VA_ARGS__)
#define Log_DebugVisible() Log::IsLogVisible(LOGLEVEL_DEBUG, ___LogChannel___)
#define Log_TraceVisible() Log::IsLogVisible(LOGLEVEL_TRACE, ___LogChannel___)
#define DEBUG_LOG(...) Log::FastWrite(___LogChannel___, LOGLEVEL_DEBUG, __VA_ARGS__)
#define TRACE_LOG(...) Log::FastWrite(___LogChannel___, LOGLEVEL_TRACE, __VA_ARGS__)
#else
#define Log_DebugPrint(msg) \
#define DEBUG_LOG(...) \
do \
{ \
} while (0)
#define Log_DebugPrintf(...) \
#define TRACE_LOG(...) \
do \
{ \
} while (0)
#define Log_DebugFmt(...) \
do \
{ \
} while (0)
#define Log_TracePrint(msg) \
do \
{ \
} while (0)
#define Log_TracePrintf(...) \
do \
{ \
} while (0)
#define Log_TraceFmt(...) \
do \
{ \
} while (0)
#define Log_DebugVisible() false
#define Log_TraceVisible() false
#endif

View File

@ -36,7 +36,7 @@ bool MemMap::MemProtect(void* baseaddr, size_t size, PageProtect mode)
DWORD old_protect;
if (!VirtualProtect(baseaddr, size, static_cast<DWORD>(mode), &old_protect))
{
Log_ErrorFmt("VirtualProtect() failed with error {}", GetLastError());
ERROR_LOG("VirtualProtect() failed with error {}", GetLastError());
return false;
}
@ -205,7 +205,7 @@ u8* SharedMemoryMappingArea::Map(void* file_handle, size_t file_offset, void* ma
if (!MapViewOfFile3(static_cast<HANDLE>(file_handle), GetCurrentProcess(), map_base, file_offset, map_size,
MEM_REPLACE_PLACEHOLDER, PAGE_READWRITE, nullptr, 0))
{
Log_ErrorFmt("MapViewOfFile3() failed: {}", GetLastError());
ERROR_LOG("MapViewOfFile3() failed: {}", GetLastError());
return nullptr;
}
@ -231,7 +231,7 @@ bool SharedMemoryMappingArea::Unmap(void* map_base, size_t map_size)
// unmap the specified range
if (!UnmapViewOfFile2(GetCurrentProcess(), map_base, MEM_PRESERVE_PLACEHOLDER))
{
Log_ErrorFmt("UnmapViewOfFile2() failed: {}", GetLastError());
ERROR_LOG("UnmapViewOfFile2() failed: {}", GetLastError());
return false;
}
@ -287,7 +287,7 @@ bool MemMap::MemProtect(void* baseaddr, size_t size, PageProtect mode)
const int result = mprotect(baseaddr, size, static_cast<int>(mode));
if (result != 0) [[unlikely]]
{
Log_ErrorFmt("mprotect() for {} at {} failed", size, baseaddr);
ERROR_LOG("mprotect() for {} at {} failed", size, baseaddr);
return false;
}

View File

@ -10,7 +10,9 @@
#include <limits>
Log_SetChannel(ProgressCallback);
ProgressCallback::~ProgressCallback() {}
ProgressCallback::~ProgressCallback()
{
}
void ProgressCallback::SetFormattedStatusText(const char* Format, ...)
{
@ -133,18 +135,18 @@ public:
void SetProgressValue(u32 value) override {}
void IncrementProgressValue() override {}
void DisplayError(const char* message) override { Log_ErrorPrint(message); }
void DisplayWarning(const char* message) override { Log_WarningPrint(message); }
void DisplayInformation(const char* message) override { Log_InfoPrint(message); }
void DisplayDebugMessage(const char* message) override { Log_DevPrint(message); }
void DisplayError(const char* message) override { ERROR_LOG(message); }
void DisplayWarning(const char* message) override { WARNING_LOG(message); }
void DisplayInformation(const char* message) override { INFO_LOG(message); }
void DisplayDebugMessage(const char* message) override { DEV_LOG(message); }
void ModalError(const char* message) override { Log_ErrorPrint(message); }
void ModalError(const char* message) override { ERROR_LOG(message); }
bool ModalConfirmation(const char* message) override
{
Log_InfoPrint(message);
INFO_LOG(message);
return false;
}
void ModalInformation(const char* message) override { Log_InfoPrint(message); }
void ModalInformation(const char* message) override { INFO_LOG(message); }
};
static NullProgressCallbacks s_nullProgressCallbacks;
@ -365,42 +367,42 @@ void ConsoleProgressCallback::Redraw(bool update_value_only)
void ConsoleProgressCallback::DisplayError(const char* message)
{
Clear();
Log_ErrorPrint(message);
ERROR_LOG(message);
Redraw(false);
}
void ConsoleProgressCallback::DisplayWarning(const char* message)
{
Clear();
Log_WarningPrint(message);
WARNING_LOG(message);
Redraw(false);
}
void ConsoleProgressCallback::DisplayInformation(const char* message)
{
Clear();
Log_InfoPrint(message);
INFO_LOG(message);
Redraw(false);
}
void ConsoleProgressCallback::DisplayDebugMessage(const char* message)
{
Clear();
Log_DevPrint(message);
DEV_LOG(message);
Redraw(false);
}
void ConsoleProgressCallback::ModalError(const char* message)
{
Clear();
Log_ErrorPrint(message);
ERROR_LOG(message);
Redraw(false);
}
bool ConsoleProgressCallback::ModalConfirmation(const char* message)
{
Clear();
Log_InfoPrint(message);
INFO_LOG(message);
Redraw(false);
return false;
}
@ -408,6 +410,6 @@ bool ConsoleProgressCallback::ModalConfirmation(const char* message)
void ConsoleProgressCallback::ModalInformation(const char* message)
{
Clear();
Log_InfoPrint(message);
INFO_LOG(message);
Redraw(false);
}