Skip to content

Commit 2b8dca1

Browse files
committed
CDRIVER-620: Allowing explicit enabling/disabling of tracing
Tracing still needs to be compiled in! When it is compiled in, you can turn it off/on by using mongoc_log_trace_enable () / mongoc_log_trace_disable ()
1 parent 922b3f6 commit 2b8dca1

File tree

6 files changed

+243
-87
lines changed

6 files changed

+243
-87
lines changed

src/mongoc/mongoc-log-private.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,28 @@
2121
# error "Only <mongoc.h> can be included directly."
2222
#endif
2323

24+
#include "mongoc-iovec.h"
25+
2426
/* just for testing */
2527
void _mongoc_log_get_handler (mongoc_log_func_t *log_func,
2628
void **user_data);
2729

30+
void
31+
mongoc_log_trace_bytes (const char *domain,
32+
const uint8_t *_b,
33+
size_t _l);
34+
35+
void
36+
mongoc_log_trace_iovec (const char *domain,
37+
const mongoc_iovec_t *_iov,
38+
size_t _iovcnt);
39+
40+
void
41+
mongoc_log_trace_enable (void);
42+
43+
void
44+
mongoc_log_trace_disable (void);
45+
46+
47+
2848
#endif /* MONGOC_LOG_PRIVATE_H */

src/mongoc/mongoc-log.c

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@
2626
#include <time.h>
2727

2828
#include "mongoc-log.h"
29+
#include "mongoc-log-private.h"
2930
#include "mongoc-thread-private.h"
3031

3132

3233
static mongoc_mutex_t gLogMutex;
3334
static mongoc_log_func_t gLogFunc = mongoc_log_default_handler;
35+
#ifdef MONGOC_TRACE
36+
static bool gLogTrace = true;
37+
#endif
3438
static void *gLogData;
3539

3640
static MONGOC_ONCE_FUN( _mongoc_ensure_mutex_once)
@@ -76,8 +80,13 @@ mongoc_log (mongoc_log_level_t log_level,
7680

7781
mongoc_once(&once, &_mongoc_ensure_mutex_once);
7882

79-
if (!gLogFunc)
83+
if (!gLogFunc
84+
#if MONGOC_TRACE
85+
|| (log_level == MONGOC_LOG_LEVEL_TRACE && !gLogTrace)
86+
#endif
87+
) {
8088
return;
89+
}
8190

8291
bson_return_if_fail(format);
8392

@@ -176,3 +185,132 @@ mongoc_log_default_handler (mongoc_log_level_t log_level,
176185
log_domain,
177186
message);
178187
}
188+
189+
void
190+
mongoc_log_trace_enable (void)
191+
{
192+
#ifdef MONGOC_TRACE
193+
gLogTrace = true;
194+
#endif
195+
}
196+
197+
void
198+
mongoc_log_trace_disable (void)
199+
{
200+
#ifdef MONGOC_TRACE
201+
gLogTrace = false;
202+
#endif
203+
}
204+
205+
void
206+
mongoc_log_trace_bytes (const char *domain, const uint8_t *_b, size_t _l)
207+
{
208+
bson_string_t *str, *astr;
209+
int32_t _i;
210+
uint8_t _v;
211+
212+
#ifdef MONGOC_TRACE
213+
if (!gLogTrace) {
214+
return;
215+
}
216+
#endif
217+
218+
str = bson_string_new(NULL);
219+
astr = bson_string_new(NULL);
220+
for (_i = 0; _i < _l; _i++) {
221+
_v = *(_b + _i);
222+
223+
if ((_i % 16) == 0) {
224+
bson_string_append_printf(str, "%05x: ", _i);
225+
}
226+
227+
bson_string_append_printf(str, " %02x", _v);
228+
if (isprint(_v)) {
229+
bson_string_append_printf(astr, " %c", _v);
230+
} else {
231+
bson_string_append(astr, " .");
232+
}
233+
234+
if ((_i % 16) == 15) {
235+
mongoc_log(MONGOC_LOG_LEVEL_TRACE, domain,
236+
"%s %s", str->str, astr->str);
237+
bson_string_truncate(str, 0);
238+
bson_string_truncate(astr, 0);
239+
} else if ((_i % 16) == 7) {
240+
bson_string_append(str, " ");
241+
bson_string_append(astr, " ");
242+
}
243+
}
244+
245+
if (_i != 16) {
246+
mongoc_log(MONGOC_LOG_LEVEL_TRACE, domain,
247+
"%-56s %s", str->str, astr->str);
248+
}
249+
250+
bson_string_free(str, true);
251+
bson_string_free(astr, true);
252+
}
253+
254+
void
255+
mongoc_log_trace_iovec (const char *domain, const mongoc_iovec_t *_iov, size_t _iovcnt)
256+
{
257+
bson_string_t *str, *astr;
258+
const char *_b;
259+
unsigned _i = 0;
260+
unsigned _j = 0;
261+
unsigned _k = 0;
262+
size_t _l = 0;
263+
uint8_t _v;
264+
265+
#ifdef MONGOC_TRACE
266+
if (!gLogTrace) {
267+
return;
268+
}
269+
#endif
270+
271+
for (_i = 0; _i < _iovcnt; _i++) {
272+
_l += _iov[_i].iov_len;
273+
}
274+
275+
_i = 0;
276+
str = bson_string_new(NULL);
277+
astr = bson_string_new(NULL);
278+
279+
for (_j = 0; _j < _iovcnt; _j++) {
280+
_b = (char *)_iov[_j].iov_base;
281+
_l = _iov[_j].iov_len;
282+
283+
for (_k = 0; _k < _l; _k++, _i++) {
284+
_v = *(_b + _k);
285+
if ((_i % 16) == 0) {
286+
bson_string_append_printf(str, "%05x: ", _i);
287+
}
288+
289+
bson_string_append_printf(str, " %02x", _v);
290+
if (isprint(_v)) {
291+
bson_string_append_printf(astr, " %c", _v);
292+
} else {
293+
bson_string_append(astr, " .");
294+
}
295+
296+
if ((_i % 16) == 15) {
297+
mongoc_log(MONGOC_LOG_LEVEL_TRACE, domain,
298+
"%s %s", str->str, astr->str);
299+
bson_string_truncate(str, 0);
300+
bson_string_truncate(astr, 0);
301+
} else if ((_i % 16) == 7) {
302+
bson_string_append(str, " ");
303+
bson_string_append(astr, " ");
304+
}
305+
}
306+
}
307+
308+
if (_i != 16) {
309+
mongoc_log(MONGOC_LOG_LEVEL_TRACE, domain,
310+
"%-56s %s", str->str, astr->str);
311+
}
312+
313+
bson_string_free(str, true);
314+
bson_string_free(astr, true);
315+
}
316+

src/mongoc/mongoc-trace.h

Lines changed: 9 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <ctype.h>
2424

2525
#include "mongoc-log.h"
26+
#include "mongoc-log-private.h"
2627

2728

2829
BSON_BEGIN_DECLS
@@ -35,92 +36,14 @@ BSON_BEGIN_DECLS
3536
#define EXIT do { mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, " EXIT: %s():%d", __FUNCTION__, __LINE__); return; } while (0)
3637
#define RETURN(ret) do { mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, " EXIT: %s():%d", __FUNCTION__, __LINE__); return ret; } while (0)
3738
#define GOTO(label) do { mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, " GOTO: %s():%d %s", __FUNCTION__, __LINE__, #label); goto label; } while (0)
38-
#define DUMP_BYTES(_n, _b, _l) \
39-
do { \
40-
bson_string_t *str, *astr; \
41-
int32_t _i; \
42-
uint8_t _v; \
43-
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, \
44-
" %s = %p [%d]", #_n, _b, (int)_l); \
45-
str = bson_string_new(NULL); \
46-
astr = bson_string_new(NULL); \
47-
for (_i = 0; _i < _l; _i++) { \
48-
_v = *(_b + _i); \
49-
if ((_i % 16) == 0) { \
50-
bson_string_append_printf(str, "%05x: ", _i); \
51-
} \
52-
bson_string_append_printf(str, " %02x", _v); \
53-
if (isprint(_v)) { \
54-
bson_string_append_printf(astr, " %c", _v); \
55-
} else { \
56-
bson_string_append(astr, " ."); \
57-
} \
58-
if ((_i % 16) == 15) { \
59-
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, \
60-
"%s %s", str->str, astr->str); \
61-
bson_string_truncate(str, 0); \
62-
bson_string_truncate(astr, 0); \
63-
} else if ((_i % 16) == 7) { \
64-
bson_string_append(str, " "); \
65-
bson_string_append(astr, " "); \
66-
} \
67-
} \
68-
if (_i != 16) { \
69-
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, \
70-
"%-56s %s", str->str, astr->str); \
71-
} \
72-
bson_string_free(str, true); \
73-
bson_string_free(astr, true); \
74-
} while (0)
75-
#define DUMP_IOVEC(_n, _iov, _iovcnt) \
76-
do { \
77-
bson_string_t *str, *astr; \
78-
const char *_b; \
79-
unsigned _i = 0; \
80-
unsigned _j = 0; \
81-
unsigned _k = 0; \
82-
size_t _l = 0; \
83-
uint8_t _v; \
84-
for (_i = 0; _i < _iovcnt; _i++) { \
85-
_l += _iov[_i].iov_len; \
86-
} \
87-
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, \
88-
" %s = %p [%d]", #_n, _iov, (int)_l); \
89-
_i = 0; \
90-
str = bson_string_new(NULL); \
91-
astr = bson_string_new(NULL); \
92-
for (_j = 0; _j < _iovcnt; _j++) { \
93-
_b = (char *)_iov[_j].iov_base; \
94-
_l = _iov[_j].iov_len; \
95-
for (_k = 0; _k < _l; _k++, _i++) { \
96-
_v = *(_b + _k); \
97-
if ((_i % 16) == 0) { \
98-
bson_string_append_printf(str, "%05x: ", _i); \
99-
} \
100-
bson_string_append_printf(str, " %02x", _v); \
101-
if (isprint(_v)) { \
102-
bson_string_append_printf(astr, " %c", _v); \
103-
} else { \
104-
bson_string_append(astr, " ."); \
105-
} \
106-
if ((_i % 16) == 15) { \
107-
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, \
108-
"%s %s", str->str, astr->str); \
109-
bson_string_truncate(str, 0); \
110-
bson_string_truncate(astr, 0); \
111-
} else if ((_i % 16) == 7) { \
112-
bson_string_append(str, " "); \
113-
bson_string_append(astr, " "); \
114-
} \
115-
} \
116-
} \
117-
if (_i != 16) { \
118-
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, \
119-
"%-56s %s", str->str, astr->str); \
120-
} \
121-
bson_string_free(str, true); \
122-
bson_string_free(astr, true); \
123-
} while (0)
39+
#define DUMP_BYTES(_n, _b, _l) do { \
40+
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "TRACE: %s():%d %s = %p [%d]", __FUNCTION__, __LINE__, #_n, _b, (int)_l); \
41+
mongoc_log_trace_bytes(MONGOC_LOG_DOMAIN, _b, _l); \
42+
} while (0)
43+
#define DUMP_IOVEC(_n, _iov, _iovcnt) do { \
44+
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "TRACE: %s():%d %s = %p [%d]", __FUNCTION__, __LINE__, #_n, _iov, (int)_iovcnt); \
45+
mongoc_log_trace_iovec(MONGOC_LOG_DOMAIN, _iov, _iovcnt); \
46+
} while (0)
12447
#else
12548
#define TRACE(msg,...)
12649
#define ENTRY

tests/TestSuite.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,11 @@ TestSuite_RunTest (TestSuite *suite, /* IN */
383383
* TODO: If not verbose, close()/dup(/dev/null) for stdout.
384384
*/
385385

386+
/* Tracing is superduper slow */
387+
#if MONGOC_TRACE
388+
mongoc_log_trace_disable ();
389+
#endif
390+
386391
#if defined(_WIN32)
387392
srand (test->seed);
388393
test->func (test->ctx);

tests/TestSuite.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ extern "C" {
6363
} while (0)
6464

6565

66+
#define ASSERT_CONTAINS(a, b) \
67+
do { \
68+
if (NULL == strstr ((a), (b))) { \
69+
fprintf(stderr, \
70+
"FAIL\n\nAssert Failure: \"%s\" does not contain \"%s\"\n", \
71+
a, b); \
72+
abort(); \
73+
} \
74+
} while (0)
75+
6676
#define ASSERT_STARTSWITH(a, b) \
6777
do { \
6878
if ((a) != strstr ((a), (b))) { \

0 commit comments

Comments
 (0)