Skip to content

Commit e3a044a

Browse files
committed
Log: Move FastWrite() into macro
That way the format args can be packed inside the conditional, potentially outside of the hot path.
1 parent b4f9bc7 commit e3a044a

File tree

9 files changed

+104
-128
lines changed

9 files changed

+104
-128
lines changed

src/common/log.cpp

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,14 @@ Log::Color Log::GetColorForLevel(Level level)
194194
return s_level_colours[static_cast<size_t>(level)];
195195
}
196196

197-
void Log::ExecuteCallbacks(MessageCategory cat, const char* functionName, std::string_view message)
197+
void Log::ExecuteCallbacks(MessageCategory cat, const char* function_name, std::string_view message)
198198
{
199199
for (RegisteredCallback& callback : s_state.callbacks)
200-
callback.Function(callback.Parameter, cat, functionName, message);
200+
callback.Function(callback.Parameter, cat, function_name, message);
201201
}
202202

203203
ALWAYS_INLINE_RELEASE void Log::FormatLogMessageForDisplay(fmt::memory_buffer& buffer, MessageCategory cat,
204-
const char* functionName, std::string_view message,
204+
const char* function_name, std::string_view message,
205205
bool timestamp, bool ansi_color_code)
206206
{
207207
static constexpr const std::array s_ansi_color_codes = {
@@ -249,10 +249,10 @@ ALWAYS_INLINE_RELEASE void Log::FormatLogMessageForDisplay(fmt::memory_buffer& b
249249
(pos == std::string_view::npos) ? message.substr(start) : message.substr(start, pos - start);
250250
const std::string_view end_message = sub_message.ends_with('\n') ? ""sv : "\n"sv;
251251

252-
if (functionName)
252+
if (function_name)
253253
{
254254
fmt::format_to(appender, "[{:10.4f}] {}{}({}): {}{}{}", message_time, color_start,
255-
s_log_level_characters[static_cast<size_t>(level)], functionName, sub_message, color_end,
255+
s_log_level_characters[static_cast<size_t>(level)], function_name, sub_message, color_end,
256256
end_message);
257257
}
258258
else
@@ -270,10 +270,10 @@ ALWAYS_INLINE_RELEASE void Log::FormatLogMessageForDisplay(fmt::memory_buffer& b
270270
}
271271
else
272272
{
273-
if (functionName)
273+
if (function_name)
274274
{
275275
fmt::format_to(appender, "{}{}({}): {}{}\n", color_start, s_log_level_characters[static_cast<size_t>(level)],
276-
functionName, message, color_end);
276+
function_name, message, color_end);
277277
}
278278
else
279279
{
@@ -284,24 +284,24 @@ ALWAYS_INLINE_RELEASE void Log::FormatLogMessageForDisplay(fmt::memory_buffer& b
284284
}
285285

286286
template<typename T>
287-
ALWAYS_INLINE_RELEASE void Log::FormatLogMessageAndPrint(MessageCategory cat, const char* functionName,
287+
ALWAYS_INLINE_RELEASE void Log::FormatLogMessageAndPrint(MessageCategory cat, const char* function_name,
288288
std::string_view message, bool timestamp, bool ansi_color_code,
289289
const T& callback)
290290
{
291291
fmt::memory_buffer buffer;
292-
FormatLogMessageForDisplay(buffer, cat, functionName, message, timestamp, ansi_color_code);
292+
FormatLogMessageForDisplay(buffer, cat, function_name, message, timestamp, ansi_color_code);
293293
callback(std::string_view(buffer.data(), buffer.size()));
294294
}
295295

296296
#ifdef _WIN32
297297

298298
template<typename T>
299-
ALWAYS_INLINE_RELEASE void Log::FormatLogMessageAndPrintW(MessageCategory cat, const char* functionName,
299+
ALWAYS_INLINE_RELEASE void Log::FormatLogMessageAndPrintW(MessageCategory cat, const char* function_name,
300300
std::string_view message, bool timestamp,
301301
bool ansi_color_code, const T& callback)
302302
{
303303
fmt::memory_buffer buffer;
304-
FormatLogMessageForDisplay(buffer, cat, functionName, message, timestamp, ansi_color_code);
304+
FormatLogMessageForDisplay(buffer, cat, function_name, message, timestamp, ansi_color_code);
305305

306306
// Convert to UTF-16 first so unicode characters display correctly. NT is going to do it
307307
// anyway...
@@ -341,36 +341,36 @@ static bool EnableVirtualTerminalProcessing(HANDLE hConsole)
341341

342342
#endif
343343

344-
void Log::ConsoleOutputLogCallback(void* pUserParam, MessageCategory cat, const char* functionName,
344+
void Log::ConsoleOutputLogCallback(void* pUserParam, MessageCategory cat, const char* function_name,
345345
std::string_view message)
346346
{
347347
if (!s_state.console_output_enabled)
348348
return;
349349

350350
#if defined(_WIN32)
351351
FormatLogMessageAndPrintW(
352-
cat, functionName, message, s_state.console_output_timestamps, true, [cat](const std::wstring_view& message) {
352+
cat, function_name, message, s_state.console_output_timestamps, true, [cat](const std::wstring_view& message) {
353353
HANDLE hOutput = (UnpackLevel(cat) <= Level::Warning) ? s_state.hConsoleStdErr : s_state.hConsoleStdOut;
354354
DWORD chars_written;
355355
WriteConsoleW(hOutput, message.data(), static_cast<DWORD>(message.length()), &chars_written, nullptr);
356356
});
357357
#elif !defined(__ANDROID__)
358358
FormatLogMessageAndPrint(
359-
cat, functionName, message, s_state.console_output_timestamps, true, [cat](std::string_view message) {
359+
cat, function_name, message, s_state.console_output_timestamps, true, [cat](std::string_view message) {
360360
const int outputFd = (UnpackLevel(cat) <= Log::Level::Warning) ? STDERR_FILENO : STDOUT_FILENO;
361361
write(outputFd, message.data(), message.length());
362362
});
363363
#endif
364364
}
365365

366-
void Log::DebugOutputLogCallback(void* pUserParam, MessageCategory cat, const char* functionName,
366+
void Log::DebugOutputLogCallback(void* pUserParam, MessageCategory cat, const char* function_name,
367367
std::string_view message)
368368
{
369369
if (!s_state.debug_output_enabled)
370370
return;
371371

372372
#if defined(_WIN32)
373-
FormatLogMessageAndPrintW(cat, functionName, message, false, false,
373+
FormatLogMessageAndPrintW(cat, function_name, message, false, false,
374374
[](const std::wstring_view& message) { OutputDebugStringW(message.data()); });
375375
#elif defined(__ANDROID__)
376376
if (message.empty())
@@ -485,13 +485,13 @@ void Log::SetDebugOutputParams(bool enabled)
485485
UnregisterCallback(DebugOutputLogCallback, nullptr, lock);
486486
}
487487

488-
void Log::FileOutputLogCallback(void* pUserParam, MessageCategory cat, const char* functionName,
488+
void Log::FileOutputLogCallback(void* pUserParam, MessageCategory cat, const char* function_name,
489489
std::string_view message)
490490
{
491491
if (!s_state.file_output_enabled)
492492
return;
493493

494-
FormatLogMessageAndPrint(cat, functionName, message, s_state.file_output_timestamp, false,
494+
FormatLogMessageAndPrint(cat, function_name, message, s_state.file_output_timestamp, false,
495495
[](std::string_view message) {
496496
std::fwrite(message.data(), 1, message.size(), s_state.file_handle.get());
497497
std::fflush(s_state.file_handle.get());
@@ -571,13 +571,13 @@ void Log::Write(MessageCategory cat, std::string_view message)
571571
ExecuteCallbacks(cat, nullptr, message);
572572
}
573573

574-
void Log::Write(MessageCategory cat, const char* functionName, std::string_view message)
574+
void Log::WriteFuncName(MessageCategory cat, const char* function_name, std::string_view message)
575575
{
576576
if (!FilterTest(UnpackChannel(cat), UnpackLevel(cat)))
577577
return;
578578

579579
std::unique_lock lock(s_state.callbacks_mutex);
580-
ExecuteCallbacks(cat, functionName, message);
580+
ExecuteCallbacks(cat, function_name, message);
581581
}
582582

583583
void Log::WriteFmtArgs(MessageCategory cat, fmt::string_view fmt, fmt::format_args args)
@@ -592,7 +592,8 @@ void Log::WriteFmtArgs(MessageCategory cat, fmt::string_view fmt, fmt::format_ar
592592
ExecuteCallbacks(cat, nullptr, std::string_view(buffer.data(), buffer.size()));
593593
}
594594

595-
void Log::WriteFmtArgs(MessageCategory cat, const char* functionName, fmt::string_view fmt, fmt::format_args args)
595+
void Log::WriteFuncNameFmtArgs(MessageCategory cat, const char* function_name, fmt::string_view fmt,
596+
fmt::format_args args)
596597
{
597598
if (!FilterTest(UnpackChannel(cat), UnpackLevel(cat)))
598599
return;
@@ -601,5 +602,5 @@ void Log::WriteFmtArgs(MessageCategory cat, const char* functionName, fmt::strin
601602
fmt::vformat_to(std::back_inserter(buffer), fmt, args);
602603

603604
std::unique_lock lock(s_state.callbacks_mutex);
604-
ExecuteCallbacks(cat, functionName, std::string_view(buffer.data(), buffer.size()));
605+
ExecuteCallbacks(cat, function_name, std::string_view(buffer.data(), buffer.size()));
605606
}

src/common/log.h

Lines changed: 43 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ static constexpr Log::Level DEFAULT_LOG_LEVEL = Log::Level::Info;
6868

6969
// Packs a level and channel into one 16-bit number.
7070
using MessageCategory = u32;
71-
[[maybe_unused]] ALWAYS_INLINE constexpr u32 PackCategory(Channel channel, Level level, Color colour)
71+
[[maybe_unused]] ALWAYS_INLINE constexpr u32 PackCategory(Channel channel, Level level, Color color)
7272
{
73-
return ((static_cast<MessageCategory>(colour) << 10) | (static_cast<MessageCategory>(channel) << 3) |
73+
return ((static_cast<MessageCategory>(color) << 10) | (static_cast<MessageCategory>(channel) << 3) |
7474
static_cast<MessageCategory>(level));
7575
}
7676
[[maybe_unused]] ALWAYS_INLINE constexpr Color UnpackColor(MessageCategory cat)
@@ -130,106 +130,75 @@ void SetLogChannelEnabled(Channel channel, bool enabled);
130130
// Returns the name of the specified log channel.
131131
const char* GetChannelName(Channel channel);
132132

133-
// Returns the default colour for a log level.
133+
// Returns the default color for a log level.
134134
Color GetColorForLevel(Level level);
135135

136136
// writes a message to the log
137137
void Write(MessageCategory cat, std::string_view message);
138-
void Write(MessageCategory cat, const char* functionName, std::string_view message);
138+
void WriteFuncName(MessageCategory cat, const char* function_name, std::string_view message);
139139
void WriteFmtArgs(MessageCategory cat, fmt::string_view fmt, fmt::format_args args);
140-
void WriteFmtArgs(MessageCategory cat, const char* functionName, fmt::string_view fmt, fmt::format_args args);
140+
void WriteFuncNameFmtArgs(MessageCategory cat, const char* function_name, fmt::string_view fmt, fmt::format_args args);
141141

142-
ALWAYS_INLINE void FastWrite(Channel channel, Level level, std::string_view message)
143-
{
144-
if (level <= GetLogLevel()) [[unlikely]]
145-
Write(PackCategory(channel, level, Color::Default), message);
146-
}
147-
ALWAYS_INLINE void FastWrite(Channel channel, const char* functionName, Level level, std::string_view message)
148-
{
149-
if (level <= GetLogLevel()) [[unlikely]]
150-
Write(PackCategory(channel, level, Color::Default), functionName, message);
151-
}
152142
template<typename... T>
153-
ALWAYS_INLINE void FastWrite(Channel channel, Level level, fmt::format_string<T...> fmt, T&&... args)
154-
{
155-
if (level <= GetLogLevel()) [[unlikely]]
156-
WriteFmtArgs(PackCategory(channel, level, Color::Default), fmt, fmt::make_format_args(args...));
157-
}
158-
template<typename... T>
159-
ALWAYS_INLINE void FastWrite(Channel channel, const char* functionName, Level level, fmt::format_string<T...> fmt,
160-
T&&... args)
161-
{
162-
if (level <= GetLogLevel()) [[unlikely]]
163-
WriteFmtArgs(PackCategory(channel, level, Color::Default), functionName, fmt, fmt::make_format_args(args...));
164-
}
165-
ALWAYS_INLINE void FastWrite(Channel channel, Level level, Color colour, std::string_view message)
143+
ALWAYS_INLINE void Write(MessageCategory cat, fmt::format_string<T...> fmt, T&&... args)
166144
{
167-
if (level <= GetLogLevel()) [[unlikely]]
168-
Write(PackCategory(channel, level, colour), message);
169-
}
170-
ALWAYS_INLINE void FastWrite(Channel channel, const char* functionName, Level level, Color colour,
171-
std::string_view message)
172-
{
173-
if (level <= GetLogLevel()) [[unlikely]]
174-
Write(PackCategory(channel, level, colour), functionName, message);
175-
}
176-
template<typename... T>
177-
ALWAYS_INLINE void FastWrite(Channel channel, Level level, Color colour, fmt::format_string<T...> fmt, T&&... args)
178-
{
179-
if (level <= GetLogLevel()) [[unlikely]]
180-
WriteFmtArgs(PackCategory(channel, level, colour), fmt, fmt::make_format_args(args...));
145+
WriteFmtArgs(cat, fmt, fmt::make_format_args(args...));
181146
}
147+
182148
template<typename... T>
183-
ALWAYS_INLINE void FastWrite(Channel channel, const char* functionName, Level level, Color colour,
184-
fmt::format_string<T...> fmt, T&&... args)
149+
ALWAYS_INLINE void WriteFuncName(MessageCategory cat, const char* function_name, fmt::format_string<T...> fmt,
150+
T&&... args)
185151
{
186-
if (level <= GetLogLevel()) [[unlikely]]
187-
WriteFmtArgs(PackCategory(channel, level, colour), functionName, fmt, fmt::make_format_args(args...));
152+
WriteFuncNameFmtArgs(cat, function_name, fmt, fmt::make_format_args(args...));
188153
}
154+
189155
} // namespace Log
190156

191157
// log wrappers
192158
#define LOG_CHANNEL(name) [[maybe_unused]] static constexpr Log::Channel ___LogChannel___ = Log::Channel::name;
193159

194-
#define ERROR_LOG(...) Log::FastWrite(___LogChannel___, __func__, Log::Level::Error, __VA_ARGS__)
195-
#define WARNING_LOG(...) Log::FastWrite(___LogChannel___, __func__, Log::Level::Warning, __VA_ARGS__)
196-
#define INFO_LOG(...) Log::FastWrite(___LogChannel___, Log::Level::Info, __VA_ARGS__)
197-
#define VERBOSE_LOG(...) Log::FastWrite(___LogChannel___, Log::Level::Verbose, __VA_ARGS__)
198-
#define DEV_LOG(...) Log::FastWrite(___LogChannel___, Log::Level::Dev, __VA_ARGS__)
199-
200-
#if defined(_DEBUG) || defined(_DEVEL)
201-
#define DEBUG_LOG(...) Log::FastWrite(___LogChannel___, Log::Level::Debug, __VA_ARGS__)
202-
#define TRACE_LOG(...) Log::FastWrite(___LogChannel___, Log::Level::Trace, __VA_ARGS__)
203-
#else
204-
#define DEBUG_LOG(...) \
160+
#define GENERIC_LOG(channel, level, color, ...) \
205161
do \
206162
{ \
163+
if ((level) <= Log::GetLogLevel()) [[unlikely]] \
164+
Log::Write(Log::PackCategory((channel), (level), (color)), __VA_ARGS__); \
207165
} while (0)
208-
#define TRACE_LOG(...) \
166+
167+
#define GENERIC_FUNC_LOG(channel, level, color, ...) \
209168
do \
210169
{ \
170+
if ((level) <= Log::GetLogLevel()) [[unlikely]] \
171+
Log::WriteFuncName(Log::PackCategory((channel), (level), (color)), __func__, __VA_ARGS__); \
211172
} while (0)
212-
#endif
213173

214174
// clang-format off
215-
#define ERROR_COLOR_LOG(colour, ...) Log::FastWrite(___LogChannel___, __func__, Log::Level::Error, Log::Color::colour, __VA_ARGS__)
216-
#define WARNING_COLOR_LOG(colour, ...) Log::FastWrite(___LogChannel___, __func__, Log::Level::Warning, Log::Color::colour, __VA_ARGS__)
217-
#define INFO_COLOR_LOG(colour, ...) Log::FastWrite(___LogChannel___, Log::Level::Info, Log::Color::colour, __VA_ARGS__)
218-
#define VERBOSE_COLOR_LOG(colour, ...) Log::FastWrite(___LogChannel___, Log::Level::Verbose, Log::Color::colour, __VA_ARGS__)
219-
#define DEV_COLOR_LOG(colour, ...) Log::FastWrite(___LogChannel___, Log::Level::Dev, Log::Color::colour, __VA_ARGS__)
175+
176+
#define ERROR_LOG(...) GENERIC_FUNC_LOG(___LogChannel___, Log::Level::Error, Log::Color::Default, __VA_ARGS__)
177+
#define WARNING_LOG(...) GENERIC_FUNC_LOG(___LogChannel___, Log::Level::Warning, Log::Color::Default, __VA_ARGS__)
178+
#define INFO_LOG(...) GENERIC_FUNC_LOG(___LogChannel___, Log::Level::Info, Log::Color::Default, __VA_ARGS__)
179+
#define VERBOSE_LOG(...) GENERIC_FUNC_LOG(___LogChannel___, Log::Level::Verbose, Log::Color::Default, __VA_ARGS__)
180+
#define DEV_LOG(...) GENERIC_FUNC_LOG(___LogChannel___, Log::Level::Dev, Log::Color::Default, __VA_ARGS__)
220181

221182
#if defined(_DEBUG) || defined(_DEVEL)
222-
#define DEBUG_COLOR_LOG(colour, ...) Log::FastWrite(___LogChannel___, Log::Level::Debug, Log::Color::colour, __VA_ARGS__)
223-
#define TRACE_COLOR_LOG(colour, ...) Log::FastWrite(___LogChannel___, Log::Level::Trace, Log::Color::colour,__VA_ARGS__)
183+
#define DEBUG_LOG(...) GENERIC_FUNC_LOG(___LogChannel___, Log::Level::Debug, Log::Color::Default, __VA_ARGS__)
184+
#define TRACE_LOG(...) GENERIC_FUNC_LOG(___LogChannel___, Log::Level::Trace, Log::Color::Default, __VA_ARGS__)
224185
#else
225-
#define DEBUG_COLOR_LOG(colour, ...) \
226-
do \
227-
{ \
228-
} while (0)
229-
#define TRACE_COLOR_LOG(colour, ...) \
230-
do \
231-
{ \
232-
} while (0)
186+
#define DEBUG_LOG(...) do { } while (0)
187+
#define TRACE_LOG(...) do { } while (0)
188+
#endif
189+
190+
#define ERROR_COLOR_LOG(color, ...) GENERIC_FUNC_LOG(___LogChannel___, Log::Level::Error, Log::Color::color, __VA_ARGS__)
191+
#define WARNING_COLOR_LOG(color, ...) GENERIC_FUNC_LOG(___LogChannel___, Log::Level::Warning, Log::Color::color, __VA_ARGS__)
192+
#define INFO_COLOR_LOG(color, ...) GENERIC_FUNC_LOG(___LogChannel___, Log::Level::Info, Log::Color::color, __VA_ARGS__)
193+
#define VERBOSE_COLOR_LOG(color, ...) GENERIC_FUNC_LOG(___LogChannel___, Log::Level::Verbose, Log::Color::color, __VA_ARGS__)
194+
#define DEV_COLOR_LOG(color, ...) GENERIC_FUNC_LOG(___LogChannel___, Log::Level::Dev, Log::Color::color, __VA_ARGS__)
195+
196+
#if defined(_DEBUG) || defined(_DEVEL)
197+
#define DEBUG_COLOR_LOG(color, ...) GENERIC_FUNC_LOG(___LogChannel___, Log::Level::Debug, Log::Color::color, __VA_ARGS__)
198+
#define TRACE_COLOR_LOG(color, ...) GENERIC_FUNC_LOG(___LogChannel___, Log::Level::Trace, Log::Color::color, __VA_ARGS__)
199+
#else
200+
#define DEBUG_COLOR_LOG(color, ...) do { } while (0)
201+
#define TRACE_COLOR_LOG(color, ...) do { } while (0)
233202
#endif
234203

235204
// clang-format on

0 commit comments

Comments
 (0)