Skip to content

Commit 736330b

Browse files
committed
Make libfmt or stb either/or
1 parent b1a2933 commit 736330b

File tree

5 files changed

+55
-33
lines changed

5 files changed

+55
-33
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ if(NOT TARGET readerwriterqueue)
5353
FetchContent_MakeAvailable(ReaderWriterQueue)
5454
endif()
5555

56-
if(NOT TARGET stb::stb)
56+
if(NOT RTSAN_USE_FMTLIB AND NOT TARGET stb::stb)
5757
# Avoid warning about DOWNLOAD_EXTRACT_TIMESTAMP in CMake 3.24:
5858
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
5959
cmake_policy(SET CMP0135 NEW)
@@ -98,6 +98,7 @@ target_compile_definitions(rtlog
9898
INTERFACE
9999
STB_SPRINTF_IMPLEMENTATION
100100
$<$<BOOL:${RTLOG_USE_FMTLIB}>:RTLOG_USE_FMTLIB>
101+
$<$<NOT:$<BOOL:${RTLOG_USE_FMTLIB}>>:RTLOG_USE_STB>
101102
$<$<CONFIG:Debug>:DEBUG>
102103
$<$<CONFIG:Release>:NDEBUG>
103104
)

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ RealtimeLogger logger;
8080
void SomeRealtimeCallback()
8181
{
8282
logger.Log({ExampleLogLevel::Debug, ExampleLogRegion::Audio}, "Hello, world! %i", 42);
83-
logger.LogFmt({ExampleLogData::Debug, ExampleLogRegion::Audio, FMT_STRING("Hello, world! {}", 42);
83+
84+
// using RTSAN_USE_LIBFMT
85+
logger.Log({ExampleLogData::Debug, ExampleLogRegion::Audio, FMT_STRING("Hello, world! {}", 42);
8486
}
8587

8688
...

examples/everlog/everlogmain.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ static rtlog::Logger<LogData, MAX_NUM_LOG_MESSAGES, MAX_LOG_MESSAGE_LENGTH,
121121
PrintMessage({LogLevel::Critical, Region}, ++gSequenceNumber, fstring, \
122122
##__VA_ARGS__)
123123

124+
#ifdef RTLOG_USE_STB
124125
#define EVR_RTLOG_DEBUG(Region, fstring, ...) \
125126
gRealtimeLogger.Log({LogLevel::Debug, Region}, fstring, ##__VA_ARGS__)
126127
#define EVR_RTLOG_INFO(Region, fstring, ...) \
@@ -129,21 +130,27 @@ static rtlog::Logger<LogData, MAX_NUM_LOG_MESSAGES, MAX_LOG_MESSAGE_LENGTH,
129130
gRealtimeLogger.Log({LogLevel::Warning, Region}, fstring, ##__VA_ARGS__)
130131
#define EVR_RTLOG_CRITICAL(Region, fstring, ...) \
131132
gRealtimeLogger.Log({LogLevel::Critical, Region}, fstring, ##__VA_ARGS__)
133+
#else
134+
#define EVR_RTLOG_DEBUG(Region, fstring, ...) (void)0
135+
#define EVR_RTLOG_INFO(Region, fstring, ...) (void)0
136+
#define EVR_RTLOG_WARNING(Region, fstring, ...) (void)0
137+
#define EVR_RTLOG_CRITICAL(Region, fstring, ...) (void)0
138+
#endif // RTLOG_USE_STB
132139

133140
#ifdef RTLOG_USE_FMTLIB
134141

135142
#define EVR_RTLOG_FMT_DEBUG(Region, fstring, ...) \
136-
gRealtimeLogger.LogFmt({LogLevel::Debug, Region}, FMT_STRING(fstring), \
137-
##__VA_ARGS__)
143+
gRealtimeLogger.Log({LogLevel::Debug, Region}, FMT_STRING(fstring), \
144+
##__VA_ARGS__)
138145
#define EVR_RTLOG_FMT_INFO(Region, fstring, ...) \
139-
gRealtimeLogger.LogFmt({LogLevel::Info, Region}, FMT_STRING(fstring), \
140-
##__VA_ARGS__)
146+
gRealtimeLogger.Log({LogLevel::Info, Region}, FMT_STRING(fstring), \
147+
##__VA_ARGS__)
141148
#define EVR_RTLOG_FMT_WARNING(Region, fstring, ...) \
142-
gRealtimeLogger.LogFmt({LogLevel::Warning, Region}, FMT_STRING(fstring), \
143-
##__VA_ARGS__)
149+
gRealtimeLogger.Log({LogLevel::Warning, Region}, FMT_STRING(fstring), \
150+
##__VA_ARGS__)
144151
#define EVR_RTLOG_FMT_CRITICAL(Region, fstring, ...) \
145-
gRealtimeLogger.LogFmt({LogLevel::Critical, Region}, FMT_STRING(fstring), \
146-
##__VA_ARGS__)
152+
gRealtimeLogger.Log({LogLevel::Critical, Region}, FMT_STRING(fstring), \
153+
##__VA_ARGS__)
147154

148155
#else
149156

include/rtlog/rtlog.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@
66
#include <cstdio>
77
#include <thread>
88

9+
// assert RTLOG_USE_FMTLIB or RTLOG_USE_STB is defined
10+
#if !defined(RTLOG_USE_FMTLIB) && !defined(RTLOG_USE_STB)
11+
#error "You must define RTLOG_USE_FMTLIB or RTLOG_USE_STB"
12+
#endif
13+
914
#ifdef RTLOG_USE_FMTLIB
1015
#include <fmt/format.h>
1116
#endif // RTLOG_USE_FMTLIB
1217

1318
#include <readerwriterqueue.h>
1419

20+
#ifdef RTLOG_USE_STB
1521
#ifndef STB_SPRINTF_IMPLEMENTATION
1622
#define STB_SPRINTF_IMPLEMENTATION
1723
#endif
@@ -21,6 +27,7 @@
2127
#endif
2228

2329
#include <stb_sprintf.h>
30+
#endif // RTLOG_USE_STB
2431

2532
#if defined(__has_feature)
2633
#if __has_feature(realtime_sanitizer)
@@ -96,6 +103,7 @@ class Logger {
96103
* message was truncated, the function returns
97104
* `Status::Error_MessageTruncated`. Otherwise, it returns `Status::Success`.
98105
*/
106+
#ifdef RTLOG_USE_STB
99107
Status Logv(LogData &&inputData, const char *format,
100108
va_list args) noexcept RTLOG_NONBLOCKING {
101109
auto retVal = Status::Success;
@@ -156,6 +164,7 @@ class Logger {
156164
va_end(args);
157165
return retVal;
158166
}
167+
#endif // RTLOG_USE_STB
159168

160169
#ifdef RTLOG_USE_FMTLIB
161170

@@ -187,8 +196,8 @@ class Logger {
187196
* `Status::Error_MessageTruncated`. Otherwise, it returns `Status::Success`.
188197
*/
189198
template <typename... T>
190-
Status LogFmt(LogData &&inputData, fmt::format_string<T...> fmtString,
191-
T &&...args) noexcept RTLOG_NONBLOCKING {
199+
Status Log(LogData &&inputData, fmt::format_string<T...> fmtString,
200+
T &&...args) noexcept RTLOG_NONBLOCKING {
192201
auto retVal = Status::Success;
193202

194203
InternalLogData dataToQueue;

test/test_rtlog.cpp

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ static auto PrintMessage = [](const ExampleLogData &data, size_t sequenceNumber,
6666

6767
using namespace rtlog::test;
6868

69+
#ifdef RTLOG_USE_STB
70+
6971
TEST(RtlogTest, BasicConstruction) {
7072
rtlog::Logger<ExampleLogData, MAX_NUM_LOG_MESSAGES, MAX_LOG_MESSAGE_LENGTH,
7173
gSequenceNumber>
@@ -193,6 +195,7 @@ TEST(RtlogTest, ErrorsReturnedFromLog) {
193195
};
194196
EXPECT_EQ(truncatedLogger.PrintAndClearLogQueue(InspectLogMessage), 1);
195197
}
198+
#endif // RTLOG_USE_STB
196199

197200
#ifdef RTLOG_USE_FMTLIB
198201

@@ -201,39 +204,39 @@ TEST(LoggerTest, FormatLibVersionWorksAsIntended) {
201204
gSequenceNumber>
202205
logger;
203206

204-
logger.LogFmt({ExampleLogLevel::Debug, ExampleLogRegion::Engine},
205-
FMT_STRING("Hello, {}!"), 123l);
206-
logger.LogFmt({ExampleLogLevel::Info, ExampleLogRegion::Game},
207-
FMT_STRING("Hello, {}!"), 123.0f);
208-
logger.LogFmt({ExampleLogLevel::Warning, ExampleLogRegion::Network},
209-
FMT_STRING("Hello, {}!"), 123.0);
210-
logger.LogFmt({ExampleLogLevel::Critical, ExampleLogRegion::Audio},
211-
FMT_STRING("Hello, {}!"), (void *)123);
212-
logger.LogFmt({ExampleLogLevel::Debug, ExampleLogRegion::Engine},
213-
FMT_STRING("Hello, {}!"), 123);
214-
logger.LogFmt({ExampleLogLevel::Critical, ExampleLogRegion::Audio},
215-
FMT_STRING("Hello, {}!"), "world");
207+
logger.Log({ExampleLogLevel::Debug, ExampleLogRegion::Engine},
208+
FMT_STRING("Hello, {}!"), 123l);
209+
logger.Log({ExampleLogLevel::Info, ExampleLogRegion::Game},
210+
FMT_STRING("Hello, {}!"), 123.0f);
211+
logger.Log({ExampleLogLevel::Warning, ExampleLogRegion::Network},
212+
FMT_STRING("Hello, {}!"), 123.0);
213+
logger.Log({ExampleLogLevel::Critical, ExampleLogRegion::Audio},
214+
FMT_STRING("Hello, {}!"), (void *)123);
215+
logger.Log({ExampleLogLevel::Debug, ExampleLogRegion::Engine},
216+
FMT_STRING("Hello, {}!"), 123);
217+
logger.Log({ExampleLogLevel::Critical, ExampleLogRegion::Audio},
218+
FMT_STRING("Hello, {}!"), "world");
216219

217220
EXPECT_EQ(logger.PrintAndClearLogQueue(PrintMessage), 6);
218221
}
219222

220-
TEST(LoggerTest, LogFmtReturnsSuccessOnNormalEnqueue) {
223+
TEST(LoggerTest, LogReturnsSuccessOnNormalEnqueue) {
221224
rtlog::Logger<ExampleLogData, MAX_NUM_LOG_MESSAGES, MAX_LOG_MESSAGE_LENGTH,
222225
gSequenceNumber>
223226
logger;
224-
EXPECT_EQ(logger.LogFmt({ExampleLogLevel::Debug, ExampleLogRegion::Engine},
225-
FMT_STRING("Hello, {}!"), 123l),
227+
EXPECT_EQ(logger.Log({ExampleLogLevel::Debug, ExampleLogRegion::Engine},
228+
FMT_STRING("Hello, {}!"), 123l),
226229
rtlog::Status::Success);
227230
}
228231

229-
TEST(LoggerTest, LogFmtHandlesLongMessageTruncation) {
232+
TEST(LoggerTest, LogHandlesLongMessageTruncation) {
230233
const auto maxMessageLength = 10;
231234
rtlog::Logger<ExampleLogData, MAX_NUM_LOG_MESSAGES, maxMessageLength,
232235
gSequenceNumber>
233236
logger;
234237

235-
EXPECT_EQ(logger.LogFmt({ExampleLogLevel::Debug, ExampleLogRegion::Engine},
236-
FMT_STRING("Hello, {}! xxxxxxxxxxx"), 123l),
238+
EXPECT_EQ(logger.Log({ExampleLogLevel::Debug, ExampleLogRegion::Engine},
239+
FMT_STRING("Hello, {}! xxxxxxxxxxx"), 123l),
237240
rtlog::Status::Error_MessageTruncated);
238241

239242
auto InspectLogMessage = [=](const ExampleLogData &data,
@@ -257,7 +260,7 @@ TEST(LoggerTest, LogFmtHandlesLongMessageTruncation) {
257260
EXPECT_EQ(logger.PrintAndClearLogQueue(InspectLogMessage), 1);
258261
}
259262

260-
TEST(LoggerTest, LogFmtHandlesQueueFullError) {
263+
TEST(LoggerTest, LogHandlesQueueFullError) {
261264
const auto maxNumMessages = 10;
262265
rtlog::Logger<ExampleLogData, maxNumMessages, MAX_LOG_MESSAGE_LENGTH,
263266
gSequenceNumber>
@@ -266,8 +269,8 @@ TEST(LoggerTest, LogFmtHandlesQueueFullError) {
266269
auto status = rtlog::Status::Success;
267270

268271
while (status == rtlog::Status::Success) {
269-
status = logger.LogFmt({ExampleLogLevel::Debug, ExampleLogRegion::Engine},
270-
FMT_STRING("Hello, {}!"), "world");
272+
status = logger.Log({ExampleLogLevel::Debug, ExampleLogRegion::Engine},
273+
FMT_STRING("Hello, {}!"), "world");
271274
}
272275

273276
EXPECT_EQ(status, rtlog::Status::Error_QueueFull);

0 commit comments

Comments
 (0)