Skip to content

Commit c679931

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 c679931

File tree

7 files changed

+63
-106
lines changed

7 files changed

+63
-106
lines changed

src/common/log.h

Lines changed: 33 additions & 78 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,7 +130,7 @@ 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
@@ -139,97 +139,52 @@ void Write(MessageCategory cat, const char* functionName, std::string_view messa
139139
void WriteFmtArgs(MessageCategory cat, fmt::string_view fmt, fmt::format_args args);
140140
void WriteFmtArgs(MessageCategory cat, const char* functionName, 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-
}
152-
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-
}
158142
template<typename... T>
159-
ALWAYS_INLINE void FastWrite(Channel channel, const char* functionName, Level level, fmt::format_string<T...> fmt,
160-
T&&... args)
143+
ALWAYS_INLINE void Write(MessageCategory cat, const char* functionName, fmt::format_string<T...> fmt, T&&... args)
161144
{
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)
166-
{
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...));
181-
}
182-
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)
185-
{
186-
if (level <= GetLogLevel()) [[unlikely]]
187-
WriteFmtArgs(PackCategory(channel, level, colour), functionName, fmt, fmt::make_format_args(args...));
145+
WriteFmtArgs(cat, functionName, fmt, fmt::make_format_args(args...));
188146
}
147+
189148
} // namespace Log
190149

191150
// log wrappers
192151
#define LOG_CHANNEL(name) [[maybe_unused]] static constexpr Log::Channel ___LogChannel___ = Log::Channel::name;
193152

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(...) \
205-
do \
206-
{ \
207-
} while (0)
208-
#define TRACE_LOG(...) \
153+
#define GENERIC_LOG(channel, level, color, ...) \
209154
do \
210155
{ \
156+
if ((level) <= Log::GetLogLevel()) [[unlikely]] \
157+
Log::Write(Log::PackCategory((channel), (level), (color)), __func__, __VA_ARGS__); \
211158
} while (0)
212-
#endif
213159

214160
// 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__)
161+
162+
#define ERROR_LOG(...) GENERIC_LOG(___LogChannel___, Log::Level::Error, Log::Color::Default, __VA_ARGS__)
163+
#define WARNING_LOG(...) GENERIC_LOG(___LogChannel___, Log::Level::Warning, Log::Color::Default, __VA_ARGS__)
164+
#define INFO_LOG(...) GENERIC_LOG(___LogChannel___, Log::Level::Info, Log::Color::Default, __VA_ARGS__)
165+
#define VERBOSE_LOG(...) GENERIC_LOG(___LogChannel___, Log::Level::Verbose, Log::Color::Default, __VA_ARGS__)
166+
#define DEV_LOG(...) GENERIC_LOG(___LogChannel___, Log::Level::Dev, Log::Color::Default, __VA_ARGS__)
220167

221168
#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__)
169+
#define DEBUG_LOG(...) GENERIC_LOG(___LogChannel___, Log::Level::Debug, Log::Color::Default, __VA_ARGS__)
170+
#define TRACE_LOG(...) GENERIC_LOG(___LogChannel___, Log::Level::Trace, Log::Color::Default, __VA_ARGS__)
224171
#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)
172+
#define DEBUG_LOG(...) do { } while (0)
173+
#define TRACE_LOG(...) do { } while (0)
174+
#endif
175+
176+
#define ERROR_COLOR_LOG(color, ...) GENERIC_LOG(___LogChannel___, __func__, Log::Level::Error, Log::Color::color, __VA_ARGS__)
177+
#define WARNING_COLOR_LOG(color, ...) GENERIC_LOG(___LogChannel___, __func__, Log::Level::Warning, Log::Color::color, __VA_ARGS__)
178+
#define INFO_COLOR_LOG(color, ...) GENERIC_LOG(___LogChannel___, Log::Level::Info, Log::Color::color, __VA_ARGS__)
179+
#define VERBOSE_COLOR_LOG(color, ...) GENERIC_LOG(___LogChannel___, Log::Level::Verbose, Log::Color::color, __VA_ARGS__)
180+
#define DEV_COLOR_LOG(color, ...) GENERIC_LOG(___LogChannel___, Log::Level::Dev, Log::Color::color, __VA_ARGS__)
181+
182+
#if defined(_DEBUG) || defined(_DEVEL)
183+
#define DEBUG_COLOR_LOG(color, ...) GENERIC_LOG(___LogChannel___, Log::Level::Debug, Log::Color::color, __VA_ARGS__)
184+
#define TRACE_COLOR_LOG(color, ...) GENERIC_LOG(___LogChannel___, Log::Level::Trace, Log::Color::color,__VA_ARGS__)
185+
#else
186+
#define DEBUG_COLOR_LOG(color, ...) do { } while (0)
187+
#define TRACE_COLOR_LOG(color, ...) do { } while (0)
233188
#endif
234189

235190
// clang-format on

src/common/ryml_helpers.h

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,14 @@ inline bool GetUIntFromObject(const ryml::ConstNodeRef& object, std::string_view
6868
const c4::csubstr val = member.val();
6969
if (val.empty())
7070
{
71-
Log::FastWrite(Log::Channel::Log, Log::Level::Error, Log::Color::StrongOrange, "Unexpected empty value in {}", key);
71+
GENERIC_LOG(Log::Channel::Log, Log::Level::Error, Log::Color::StrongOrange, "Unexpected empty value in {}", key);
7272
return false;
7373
}
7474

7575
const std::optional<T> opt_value = StringUtil::FromChars<T>(to_stringview(val));
7676
if (!opt_value.has_value())
7777
{
78-
Log::FastWrite(Log::Channel::Log, Log::Level::Error, Log::Color::StrongOrange, "Unexpected non-uint value in {}",
79-
key);
78+
GENERIC_LOG(Log::Channel::Log, Log::Level::Error, Log::Color::StrongOrange, "Unexpected non-uint value in {}", key);
8079
return false;
8180
}
8281

@@ -100,25 +99,24 @@ inline std::optional<T> GetOptionalTFromObject(const ryml::ConstNodeRef& object,
10099
{
101100
if constexpr (std::is_same_v<T, bool>)
102101
{
103-
Log::FastWrite(Log::Channel::Log, Log::Level::Error, Log::Color::StrongOrange,
104-
"Unexpected non-bool value in {}", key);
102+
GENERIC_LOG(Log::Channel::Log, Log::Level::Error, Log::Color::StrongOrange, "Unexpected non-bool value in {}",
103+
key);
105104
}
106105
else if constexpr (std::is_floating_point_v<T>)
107106
{
108-
Log::FastWrite(Log::Channel::Log, Log::Level::Error, Log::Color::StrongOrange,
109-
"Unexpected non-float value in {}", key);
107+
GENERIC_LOG(Log::Channel::Log, Log::Level::Error, Log::Color::StrongOrange,
108+
"Unexpected non-float value in {}", key);
110109
}
111110
else if constexpr (std::is_integral_v<T>)
112111
{
113-
Log::FastWrite(Log::Channel::Log, Log::Level::Error, Log::Color::StrongOrange,
114-
"Unexpected non-int value in {}", key);
112+
GENERIC_LOG(Log::Channel::Log, Log::Level::Error, Log::Color::StrongOrange, "Unexpected non-int value in {}",
113+
key);
115114
}
116115
}
117116
}
118117
else
119118
{
120-
Log::FastWrite(Log::Channel::Log, Log::Level::Error, Log::Color::StrongOrange, "Unexpected empty value in {}",
121-
key);
119+
GENERIC_LOG(Log::Channel::Log, Log::Level::Error, Log::Color::StrongOrange, "Unexpected empty value in {}", key);
122120
}
123121
}
124122

@@ -139,13 +137,14 @@ inline std::optional<T> ParseOptionalTFromObject(const ryml::ConstNodeRef& objec
139137
{
140138
ret = from_string_function(TinyString(to_stringview(val)));
141139
if (!ret.has_value())
142-
Log::FastWrite(Log::Channel::Log, Log::Level::Error, Log::Color::StrongOrange, "Unknown value for {}: {}", key,
143-
to_stringview(val));
140+
{
141+
GENERIC_LOG(Log::Channel::Log, Log::Level::Error, Log::Color::StrongOrange, "Unknown value for {}: {}", key,
142+
to_stringview(val));
143+
}
144144
}
145145
else
146146
{
147-
Log::FastWrite(Log::Channel::Log, Log::Level::Error, Log::Color::StrongOrange, "Unexpected empty value in {}",
148-
key);
147+
GENERIC_LOG(Log::Channel::Log, Log::Level::Error, Log::Color::StrongOrange, "Unexpected empty value in {}", key);
149148
}
150149
}
151150

@@ -156,13 +155,13 @@ inline void SetRymlCallbacks()
156155
{
157156
ryml::Callbacks callbacks = ryml::get_callbacks();
158157
callbacks.m_error = [](const char* msg, size_t msg_len, ryml::Location loc, void* userdata) {
159-
Log::FastWrite(Log::Channel::Log, Log::Level::Error, Log::Color::StrongOrange,
160-
"YAML parse error at {}:{} (bufpos={}): {}", loc.line, loc.col, loc.offset,
161-
std::string_view(msg, msg_len));
158+
GENERIC_LOG(Log::Channel::Log, Log::Level::Error, Log::Color::StrongOrange,
159+
"YAML parse error at {}:{} (bufpos={}): {}", loc.line, loc.col, loc.offset,
160+
std::string_view(msg, msg_len));
162161
};
163162
ryml::set_callbacks(callbacks);
164163
c4::set_error_callback([](const char* msg, size_t msg_size) {
165-
Log::FastWrite(Log::Channel::Log, Log::Level::Error, Log::Color::StrongOrange, "C4 error: {}",
166-
std::string_view(msg, msg_size));
164+
GENERIC_LOG(Log::Channel::Log, Log::Level::Error, Log::Color::StrongOrange, "C4 error: {}",
165+
std::string_view(msg, msg_size));
167166
});
168167
}

src/common/zip_helpers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct ZipDeleter
3333
const int err = zip_close(zf);
3434
if (err != 0)
3535
{
36-
Log::FastWrite(Log::Channel::Ungrouped, __FUNCTION__, Log::Level::Error, "Failed to close zip file: {}", err);
36+
GENERIC_LOG(Log::Channel::Ungrouped, Log::Level::Error, Log::Color::Default, "Failed to close zip file: {}", err);
3737
zip_discard(zf);
3838
}
3939
}

src/core/bus.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ void Bus::AddTTYCharacter(char ch)
943943
{
944944
if (!s_tty_line_buffer.empty())
945945
{
946-
Log::FastWrite(Log::Channel::TTY, Log::Level::Info, Log::Color::StrongBlue, s_tty_line_buffer);
946+
GENERIC_LOG(Log::Channel::TTY, Log::Level::Info, Log::Color::StrongBlue, s_tty_line_buffer);
947947
#if defined(_DEBUG) || defined(_DEVEL)
948948
if (CPU::IsTraceEnabled())
949949
CPU::WriteToExecutionLog("TTY: %s\n", s_tty_line_buffer.c_str());

src/util/sdl_input_source.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ static void SDLLogCallback(void* userdata, int category, SDL_LogPriority priorit
212212
Log::Level::Error, // SDL_LOG_PRIORITY_CRITICAL
213213
};
214214

215-
Log::FastWrite(Log::Channel::SDL, priority_map[priority], message);
215+
GENERIC_LOG(Log::Channel::SDL, priority_map[priority], Log::Color::Default, message);
216216
}
217217

218218
bool SDLInputSource::ALLOW_EVENT_POLLING = true;

src/util/vulkan_builders.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,11 @@ const char* Vulkan::VkResultToString(VkResult res)
106106

107107
void Vulkan::LogVulkanResult(const char* func_name, VkResult res, std::string_view msg)
108108
{
109-
Log::FastWrite(Log::Channel::GPUDevice, func_name, Log::Level::Error, "{} (0x{:08X}: {})", msg,
110-
static_cast<unsigned>(res), VkResultToString(res));
109+
if (Log::GetLogLevel() < Log::Level::Error)
110+
return;
111+
112+
Log::Write(Log::PackCategory(Log::Channel::GPUDevice, Log::Level::Error, Log::Color::Default), func_name,
113+
"{} (0x{:08X}: {})", msg, static_cast<unsigned>(res), VkResultToString(res));
111114
}
112115

113116
void Vulkan::SetErrorObject(Error* errptr, std::string_view prefix, VkResult res)

src/util/vulkan_device.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -715,9 +715,9 @@ bool VulkanDevice::EnableOptionalDeviceExtensions(VkPhysicalDevice physical_devi
715715
enabled_features.textureCompressionBC |= features2.features.textureCompressionBC;
716716

717717
#define LOG_EXT(name, field) \
718-
Log::FastWrite(___LogChannel___, Log::Level::Info, \
719-
m_optional_extensions.field ? Log::Color::StrongGreen : Log::Color::StrongOrange, name " is {}", \
720-
m_optional_extensions.field ? "supported" : "NOT supported")
718+
GENERIC_LOG(___LogChannel___, Log::Level::Info, \
719+
m_optional_extensions.field ? Log::Color::StrongGreen : Log::Color::StrongOrange, name " is {}", \
720+
m_optional_extensions.field ? "supported" : "NOT supported")
721721

722722
LOG_EXT("VK_EXT_external_memory_host", vk_ext_external_memory_host);
723723
LOG_EXT("VK_EXT_fragment_shader_interlock", vk_ext_fragment_shader_interlock);

0 commit comments

Comments
 (0)