Skip to content

Commit 9adb365

Browse files
authored
Fix logger causes if statement mismatch (#203)
1 parent 59d0ba7 commit 9adb365

File tree

3 files changed

+62
-40
lines changed

3 files changed

+62
-40
lines changed

trantor/tests/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ add_executable(delayed_ssl_server_test DelayedSSLServerTest.cc)
2020
add_executable(delayed_ssl_client_test DelayedSSLClientTest.cc)
2121
add_executable(run_on_quit_test RunOnQuitTest.cc)
2222
add_executable(path_conversion_test PathConversionTest.cc)
23+
add_executable(logger_macro_test LoggerMacroTest.cc)
2324
set(targets_list
2425
ssl_server_test
2526
ssl_client_test
@@ -42,7 +43,8 @@ set(targets_list
4243
delayed_ssl_server_test
4344
delayed_ssl_client_test
4445
run_on_quit_test
45-
path_conversion_test)
46+
path_conversion_test
47+
logger_macro_test)
4648

4749
set_property(TARGET ${targets_list} PROPERTY CXX_STANDARD 14)
4850
set_property(TARGET ${targets_list} PROPERTY CXX_STANDARD_REQUIRED ON)

trantor/tests/LoggerMacroTest.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <trantor/utils/Logger.h>
2+
3+
using namespace trantor;
4+
5+
int main()
6+
{
7+
trantor::Logger::setLogLevel(trantor::Logger::kInfo);
8+
if (0)
9+
LOG_INFO << "dummy";
10+
else
11+
LOG_WARN << "it works";
12+
}

trantor/utils/Logger.h

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include <iostream>
2424
#include <vector>
2525

26+
#define TRANTOR_IF_(cond) for (int _r = 0; _r == 0 && (cond); _r = 1)
27+
2628
namespace trantor
2729
{
2830
/**
@@ -219,36 +221,36 @@ class TRANTOR_EXPORT RawLogger : public NonCopyable
219221
};
220222
#ifdef NDEBUG
221223
#define LOG_TRACE \
222-
if (0) \
224+
TRANTOR_IF_(0) \
223225
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kTrace, __func__) \
224226
.stream()
225227
#else
226228
#define LOG_TRACE \
227-
if (trantor::Logger::logLevel() <= trantor::Logger::kTrace) \
229+
TRANTOR_IF_(trantor::Logger::logLevel() <= trantor::Logger::kTrace) \
228230
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kTrace, __func__) \
229231
.stream()
230232
#define LOG_TRACE_TO(index) \
231-
if (trantor::Logger::logLevel() <= trantor::Logger::kTrace) \
233+
TRANTOR_IF_(trantor::Logger::logLevel() <= trantor::Logger::kTrace) \
232234
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kTrace, __func__) \
233235
.setIndex(index) \
234236
.stream()
235237

236238
#endif
237239

238240
#define LOG_DEBUG \
239-
if (trantor::Logger::logLevel() <= trantor::Logger::kDebug) \
241+
TRANTOR_IF_(trantor::Logger::logLevel() <= trantor::Logger::kDebug) \
240242
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kDebug, __func__) \
241243
.stream()
242244
#define LOG_DEBUG_TO(index) \
243-
if (trantor::Logger::logLevel() <= trantor::Logger::kDebug) \
245+
TRANTOR_IF_(trantor::Logger::logLevel() <= trantor::Logger::kDebug) \
244246
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kDebug, __func__) \
245247
.setIndex(index) \
246248
.stream()
247-
#define LOG_INFO \
248-
if (trantor::Logger::logLevel() <= trantor::Logger::kInfo) \
249+
#define LOG_INFO \
250+
TRANTOR_IF_(trantor::Logger::logLevel() <= trantor::Logger::kInfo) \
249251
trantor::Logger(__FILE__, __LINE__).stream()
250-
#define LOG_INFO_TO(index) \
251-
if (trantor::Logger::logLevel() <= trantor::Logger::kInfo) \
252+
#define LOG_INFO_TO(index) \
253+
TRANTOR_IF_(trantor::Logger::logLevel() <= trantor::Logger::kInfo) \
252254
trantor::Logger(__FILE__, __LINE__).setIndex(index).stream()
253255
#define LOG_WARN \
254256
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kWarn).stream()
@@ -276,79 +278,82 @@ class TRANTOR_EXPORT RawLogger : public NonCopyable
276278
#define LOG_RAW_TO(index) trantor::RawLogger().setIndex(index).stream()
277279

278280
#define LOG_TRACE_IF(cond) \
279-
if ((trantor::Logger::logLevel() <= trantor::Logger::kTrace) && (cond)) \
281+
TRANTOR_IF_((trantor::Logger::logLevel() <= trantor::Logger::kTrace) && \
282+
(cond)) \
280283
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kTrace, __func__) \
281284
.stream()
282285
#define LOG_DEBUG_IF(cond) \
283-
if ((trantor::Logger::logLevel() <= trantor::Logger::kDebug) && (cond)) \
286+
TRANTOR_IF_((trantor::Logger::logLevel() <= trantor::Logger::kDebug) && \
287+
(cond)) \
284288
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kDebug, __func__) \
285289
.stream()
286290
#define LOG_INFO_IF(cond) \
287-
if ((trantor::Logger::logLevel() <= trantor::Logger::kInfo) && (cond)) \
291+
TRANTOR_IF_((trantor::Logger::logLevel() <= trantor::Logger::kInfo) && \
292+
(cond)) \
288293
trantor::Logger(__FILE__, __LINE__).stream()
289294
#define LOG_WARN_IF(cond) \
290-
if (cond) \
295+
TRANTOR_IF_(cond) \
291296
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kWarn).stream()
292297
#define LOG_ERROR_IF(cond) \
293-
if (cond) \
298+
TRANTOR_IF_(cond) \
294299
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kError).stream()
295300
#define LOG_FATAL_IF(cond) \
296-
if (cond) \
301+
TRANTOR_IF_(cond) \
297302
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kFatal).stream()
298303

299304
#ifdef NDEBUG
300305
#define DLOG_TRACE \
301-
if (0) \
306+
TRANTOR_IF_(0) \
302307
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kTrace, __func__) \
303308
.stream()
304309
#define DLOG_DEBUG \
305-
if (0) \
310+
TRANTOR_IF_(0) \
306311
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kDebug, __func__) \
307312
.stream()
308-
#define DLOG_INFO \
309-
if (0) \
313+
#define DLOG_INFO \
314+
TRANTOR_IF_(0) \
310315
trantor::Logger(__FILE__, __LINE__).stream()
311-
#define DLOG_WARN \
312-
if (0) \
316+
#define DLOG_WARN \
317+
TRANTOR_IF_(0) \
313318
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kWarn).stream()
314319
#define DLOG_ERROR \
315-
if (0) \
320+
TRANTOR_IF_(0) \
316321
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kError).stream()
317322
#define DLOG_FATAL \
318-
if (0) \
323+
TRANTOR_IF_(0) \
319324
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kFatal).stream()
320325

321326
#define DLOG_TRACE_IF(cond) \
322-
if (0) \
327+
TRANTOR_IF_(0) \
323328
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kTrace, __func__) \
324329
.stream()
325330
#define DLOG_DEBUG_IF(cond) \
326-
if (0) \
331+
TRANTOR_IF_(0) \
327332
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kDebug, __func__) \
328333
.stream()
329334
#define DLOG_INFO_IF(cond) \
330-
if (0) \
335+
TRANTOR_IF_(0) \
331336
trantor::Logger(__FILE__, __LINE__).stream()
332337
#define DLOG_WARN_IF(cond) \
333-
if (0) \
338+
TRANTOR_IF_(0) \
334339
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kWarn).stream()
335340
#define DLOG_ERROR_IF(cond) \
336-
if (0) \
341+
TRANTOR_IF_(0) \
337342
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kError).stream()
338343
#define DLOG_FATAL_IF(cond) \
339-
if (0) \
344+
TRANTOR_IF_(0) \
340345
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kFatal).stream()
341346
#else
342347
#define DLOG_TRACE \
343-
if (trantor::Logger::logLevel() <= trantor::Logger::kTrace) \
348+
TRANTOR_IF_(trantor::Logger::logLevel() <= trantor::Logger::kTrace) \
344349
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kTrace, __func__) \
345350
.stream()
346351
#define DLOG_DEBUG \
347-
if (trantor::Logger::logLevel() <= trantor::Logger::kDebug) \
352+
TRANTOR_IF_(trantor::Logger::logLevel() <= trantor::Logger::kDebug) \
348353
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kDebug, __func__) \
349354
.stream()
350-
#define DLOG_INFO \
351-
if (trantor::Logger::logLevel() <= trantor::Logger::kInfo) \
355+
#define DLOG_INFO \
356+
TRANTOR_IF_(trantor::Logger::logLevel() <= trantor::Logger::kInfo) \
352357
trantor::Logger(__FILE__, __LINE__).stream()
353358
#define DLOG_WARN \
354359
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kWarn).stream()
@@ -358,24 +363,27 @@ class TRANTOR_EXPORT RawLogger : public NonCopyable
358363
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kFatal).stream()
359364

360365
#define DLOG_TRACE_IF(cond) \
361-
if ((trantor::Logger::logLevel() <= trantor::Logger::kTrace) && (cond)) \
366+
TRANTOR_IF_((trantor::Logger::logLevel() <= trantor::Logger::kTrace) && \
367+
(cond)) \
362368
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kTrace, __func__) \
363369
.stream()
364370
#define DLOG_DEBUG_IF(cond) \
365-
if ((trantor::Logger::logLevel() <= trantor::Logger::kDebug) && (cond)) \
371+
TRANTOR_IF_((trantor::Logger::logLevel() <= trantor::Logger::kDebug) && \
372+
(cond)) \
366373
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kDebug, __func__) \
367374
.stream()
368375
#define DLOG_INFO_IF(cond) \
369-
if ((trantor::Logger::logLevel() <= trantor::Logger::kInfo) && (cond)) \
376+
TRANTOR_IF_((trantor::Logger::logLevel() <= trantor::Logger::kInfo) && \
377+
(cond)) \
370378
trantor::Logger(__FILE__, __LINE__).stream()
371379
#define DLOG_WARN_IF(cond) \
372-
if (cond) \
380+
TRANTOR_IF_(cond) \
373381
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kWarn).stream()
374382
#define DLOG_ERROR_IF(cond) \
375-
if (cond) \
383+
TRANTOR_IF_(cond) \
376384
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kError).stream()
377385
#define DLOG_FATAL_IF(cond) \
378-
if (cond) \
386+
TRANTOR_IF_(cond) \
379387
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kFatal).stream()
380388
#endif
381389

0 commit comments

Comments
 (0)