Skip to content

Commit 59aeacb

Browse files
Avoid conditional compilation on whether tracing is enabled (#914)
1 parent 54e5ecb commit 59aeacb

11 files changed

+163
-174
lines changed

src/libmongoc/CMakeLists.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,7 @@ set (MONGOC_CC ${CMAKE_C_COMPILER})
351351
set (MONGOC_USER_SET_CFLAGS ${CMAKE_C_FLAGS})
352352
set (MONGOC_USER_SET_LDFLAGS ${CMAKE_EXE_LINKER_FLAGS})
353353

354-
set (MONGOC_TRACE 0)
355-
356-
if (ENABLE_TRACING)
357-
set (MONGOC_TRACE 1)
358-
endif ()
354+
set (MONGOC_TRACE "${ENABLE_TRACING}")
359355

360356
# Sets SNAPPY_LIBRARIES and SNAPPY_INCLUDE_DIRS.
361357
include (FindSnappy)

src/libmongoc/src/mongoc/mongoc-config.h.in

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@
2222
#ifndef MONGOC_CONFIG_H
2323
#define MONGOC_CONFIG_H
2424

25+
/* clang-format off */
26+
27+
/*
28+
* NOTICE:
29+
* If you're about to update this file and add a config flag, make sure to
30+
* update:
31+
* o The bitfield in mongoc-handshake-private.h
32+
* o _mongoc_handshake_get_config_hex_string() in mongoc-handshake.c
33+
* o examples/parse_handshake_cfg.py
34+
* o test_handshake_config_string in test-mongoc-handshake.c
35+
*/
36+
2537
/* MONGOC_USER_SET_CFLAGS is set from config based on what compiler flags were
2638
* used to compile mongoc */
2739
#define MONGOC_USER_SET_CFLAGS "@MONGOC_USER_SET_CFLAGS@"
@@ -353,13 +365,18 @@
353365
/*
354366
* Set if tracing is enabled. Logs things like network communication and
355367
* entry/exit of certain functions.
356-
*
357368
*/
358-
#define MONGOC_TRACE @MONGOC_TRACE@
369+
#cmakedefine01 MONGOC_TRACE
359370

360-
#if MONGOC_TRACE != 1
361-
# undef MONGOC_TRACE
362-
#endif
371+
enum {
372+
/**
373+
* @brief Compile-time constant determining whether the mongoc library was
374+
* compiled with tracing enabled.
375+
*
376+
* Can be controlled with the 'ENABLE_TRACING" configure-time boolean option
377+
*/
378+
MONGOC_TRACE_ENABLED = MONGOC_TRACE
379+
};
363380

364381
/*
365382
* Set if we have ICU support.
@@ -401,14 +418,6 @@
401418
# undef MONGOC_ENABLE_MONGODB_AWS_AUTH
402419
#endif
403420

404-
/*
405-
* NOTICE:
406-
* If you're about to update this file and add a config flag, make sure to
407-
* update:
408-
* o The bitfield in mongoc-handshake-private.h
409-
* o _mongoc_handshake_get_config_hex_string() in mongoc-handshake.c
410-
* o examples/parse_handshake_cfg.py
411-
* o test_handshake_config_string in test-mongoc-handshake.c
412-
*/
421+
/* clang-format on */
413422

414423
#endif /* MONGOC_CONFIG_H */

src/libmongoc/src/mongoc/mongoc-handshake.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ _mongoc_handshake_get_config_hex_string (void)
182182
_set_bit (bf, byte_count, MONGOC_MD_FLAG_ENABLE_SHM_COUNTERS);
183183
#endif
184184

185-
#ifdef MONGOC_TRACE
186-
_set_bit (bf, byte_count, MONGOC_MD_FLAG_TRACE);
187-
#endif
185+
if (MONGOC_TRACE_ENABLED) {
186+
_set_bit (bf, byte_count, MONGOC_MD_FLAG_TRACE);
187+
}
188188

189189
#ifdef MONGOC_ENABLE_ICU
190190
_set_bit (bf, byte_count, MONGOC_MD_FLAG_ENABLE_ICU);

src/libmongoc/src/mongoc/mongoc-log.c

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@
3939
static bson_once_t once = BSON_ONCE_INIT;
4040
static bson_mutex_t gLogMutex;
4141
static mongoc_log_func_t gLogFunc = mongoc_log_default_handler;
42-
#ifdef MONGOC_TRACE
43-
static bool gLogTrace = true;
44-
#endif
42+
static bool gLogTrace = MONGOC_TRACE_ENABLED;
4543
static void *gLogData;
4644

4745
static BSON_ONCE_FUN (_mongoc_ensure_mutex_once)
@@ -62,6 +60,26 @@ mongoc_log_set_handler (mongoc_log_func_t log_func, void *user_data)
6260
bson_mutex_unlock (&gLogMutex);
6361
}
6462

63+
bool
64+
_mongoc_log_trace_is_enabled (void)
65+
{
66+
return gLogTrace && MONGOC_TRACE_ENABLED;
67+
}
68+
69+
void
70+
mongoc_log_trace_enable (void)
71+
{
72+
/* Enable trace logging if-and-only-if tracing is enabled at configure-time,
73+
* otherwise tracing remains disabled.
74+
*/
75+
gLogTrace = MONGOC_TRACE_ENABLED;
76+
}
77+
78+
void
79+
mongoc_log_trace_disable (void)
80+
{
81+
gLogTrace = false;
82+
}
6583

6684
/* just for testing */
6785
void
@@ -85,10 +103,8 @@ mongoc_log (mongoc_log_level_t log_level,
85103
bson_once (&once, &_mongoc_ensure_mutex_once);
86104

87105
stop_logging = !gLogFunc;
88-
#ifdef MONGOC_TRACE
89-
stop_logging =
90-
stop_logging || (log_level == MONGOC_LOG_LEVEL_TRACE && !gLogTrace);
91-
#endif
106+
stop_logging = stop_logging || (log_level == MONGOC_LOG_LEVEL_TRACE &&
107+
!_mongoc_log_trace_is_enabled ());
92108
if (stop_logging) {
93109
return;
94110
}
@@ -203,44 +219,16 @@ mongoc_log_default_handler (mongoc_log_level_t log_level,
203219
message);
204220
}
205221

206-
bool
207-
_mongoc_log_trace_is_enabled (void)
208-
{
209-
#ifdef MONGOC_TRACE
210-
return gLogTrace;
211-
#else
212-
return false;
213-
#endif
214-
}
215-
216-
void
217-
mongoc_log_trace_enable (void)
218-
{
219-
#ifdef MONGOC_TRACE
220-
gLogTrace = true;
221-
#endif
222-
}
223-
224-
void
225-
mongoc_log_trace_disable (void)
226-
{
227-
#ifdef MONGOC_TRACE
228-
gLogTrace = false;
229-
#endif
230-
}
231-
232222
void
233223
mongoc_log_trace_bytes (const char *domain, const uint8_t *_b, size_t _l)
234224
{
235225
bson_string_t *str, *astr;
236226
int32_t _i;
237227
uint8_t _v;
238228

239-
#ifdef MONGOC_TRACE
240-
if (!gLogTrace) {
229+
if (!_mongoc_log_trace_is_enabled ()) {
241230
return;
242231
}
243-
#endif
244232

245233
str = bson_string_new (NULL);
246234
astr = bson_string_new (NULL);
@@ -291,11 +279,9 @@ mongoc_log_trace_iovec (const char *domain,
291279
size_t _l = 0;
292280
uint8_t _v;
293281

294-
#ifdef MONGOC_TRACE
295-
if (!gLogTrace) {
282+
if (!_mongoc_log_trace_is_enabled ()) {
296283
return;
297284
}
298-
#endif
299285

300286
for (_i = 0; _i < _iovcnt; _i++) {
301287
_l += _iov[_i].iov_len;

src/libmongoc/src/mongoc/mongoc-server-monitor.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ static BSON_GNUC_PRINTF (3, 4) void _server_monitor_log (
111111
bson_free (msg);
112112
}
113113

114-
#ifdef MONGOC_TRACE
115-
#define MONITOR_LOG(sm, ...) \
116-
_server_monitor_log (sm, MONGOC_LOG_LEVEL_TRACE, __VA_ARGS__)
117-
#else
118-
#define MONITOR_LOG(sm, ...) (void) 0
119-
#endif
114+
#define MONITOR_LOG(sm, ...) \
115+
do { \
116+
if (MONGOC_TRACE_ENABLED) { \
117+
_server_monitor_log (sm, MONGOC_LOG_LEVEL_TRACE, __VA_ARGS__); \
118+
} \
119+
} while (0)
120120

121121
/* TODO CDRIVER-3710 use MONGOC_LOG_LEVEL_ERROR */
122122
#define MONITOR_LOG_ERROR(sm, ...) \

src/libmongoc/src/mongoc/mongoc-socket.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ _mongoc_socket_setkeepalive_windows (SOCKET sd)
437437
}
438438
}
439439
#else
440-
#ifdef MONGOC_TRACE
440+
441441
static const char *
442442
_mongoc_socket_sockopt_value_to_name (int value)
443443
{
@@ -463,7 +463,7 @@ _mongoc_socket_sockopt_value_to_name (int value)
463463
return "Unknown option name";
464464
}
465465
}
466-
#endif
466+
467467
static void
468468
_mongoc_socket_set_sockopt_if_less (int sd, int name, int value)
469469
{

src/libmongoc/src/mongoc/mongoc-topology-description.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,7 +1857,6 @@ transition_t gSDAMTransitionTable
18571857
NULL,
18581858
_mongoc_topology_description_check_if_has_primary}};
18591859

1860-
#ifdef MONGOC_TRACE
18611860
/*
18621861
*--------------------------------------------------------------------------
18631862
*
@@ -1896,7 +1895,6 @@ _mongoc_topology_description_type (mongoc_topology_description_t *topology)
18961895
return "Invalid";
18971896
}
18981897
}
1899-
#endif
19001898

19011899

19021900
/*

0 commit comments

Comments
 (0)