1MONGOC_LOGGING(3) libmongoc MONGOC_LOGGING(3)
2
3
4
6 mongoc_logging - Logging
7
8 MongoDB C driver Logging Abstraction
9
11 typedef enum {
12 MONGOC_LOG_LEVEL_ERROR,
13 MONGOC_LOG_LEVEL_CRITICAL,
14 MONGOC_LOG_LEVEL_WARNING,
15 MONGOC_LOG_LEVEL_MESSAGE,
16 MONGOC_LOG_LEVEL_INFO,
17 MONGOC_LOG_LEVEL_DEBUG,
18 MONGOC_LOG_LEVEL_TRACE,
19 } mongoc_log_level_t;
20
21 #define MONGOC_ERROR(...)
22 #define MONGOC_CRITICAL(...)
23 #define MONGOC_WARNING(...)
24 #define MONGOC_MESSAGE(...)
25 #define MONGOC_INFO(...)
26 #define MONGOC_DEBUG(...)
27
28 typedef void (*mongoc_log_func_t) (mongoc_log_level_t log_level,
29 const char *log_domain,
30 const char *message,
31 void *user_data);
32
33 void
34 mongoc_log_set_handler (mongoc_log_func_t log_func, void *user_data);
35 void
36 mongoc_log (mongoc_log_level_t log_level,
37 const char *log_domain,
38 const char *format,
39 ...) BSON_GNUC_PRINTF (3, 4);
40 const char *
41 mongoc_log_level_str (mongoc_log_level_t log_level);
42 void
43 mongoc_log_default_handler (mongoc_log_level_t log_level,
44 const char *log_domain,
45 const char *message,
46 void *user_data);
47 void
48 mongoc_log_trace_enable (void);
49 void
50 mongoc_log_trace_disable (void);
51
52 The MongoDB C driver comes with an abstraction for logging that you can
53 use in your application, or integrate with an existing logging system.
54
56 To make logging a little less painful, various helper macros are pro‐
57 vided. See the following example.
58
59 #undef MONGOC_LOG_DOMAIN
60 #define MONGOC_LOG_DOMAIN "my-custom-domain"
61
62 MONGOC_WARNING ("An error occurred: %s", strerror (errno));
63
65 The default log handler prints a timestamp and the log message to std‐
66 out, or to stderr for warnings, critical messages, and errors.
67 You can override the handler with mongoc_log_set_handler().
68 Your handler function is called in a mutex for thread safety.
69
70 For example, you could register a custom handler to suppress messages
71 at INFO level and below:
72
73 void
74 my_logger (mongoc_log_level_t log_level,
75 const char *log_domain,
76 const char *message,
77 void *user_data)
78 {
79 /* smaller values are more important */
80 if (log_level < MONGOC_LOG_LEVEL_INFO) {
81 mongoc_log_default_handler (log_level, log_domain, message, user_data);
82 }
83 }
84
85 int
86 main (int argc, char *argv[])
87 {
88 mongoc_log_set_handler (my_logger, NULL);
89 mongoc_init ();
90
91 /* ... your code ... */
92
93 mongoc_cleanup ();
94 return 0;
95 }
96
97 Note that in the example above mongoc_log_set_handler() is called be‐
98 fore mongoc_init(). Otherwise, some log traces could not be processed
99 by the log handler.
100
101 To restore the default handler:
102
103 mongoc_log_set_handler (mongoc_log_default_handler, NULL);
104
106 To disable all logging, including warnings, critical messages and er‐
107 rors, provide an empty log handler:
108
109 mongoc_log_set_handler (NULL, NULL);
110
112 If compiling your own copy of the MongoDB C driver, consider configur‐
113 ing with -DENABLE_TRACING=ON to enable function tracing and hex dumps
114 of network packets to STDERR and STDOUT during development and debug‐
115 ging.
116
117 This is especially useful when debugging what may be going on inter‐
118 nally in the driver.
119
120 Trace messages can be enabled and disabled by calling mon‐
121 goc_log_trace_enable() and mongoc_log_trace_disable()
122
123 NOTE:
124 Compiling the driver with -DENABLE_TRACING=ON will affect its per‐
125 formance. Disabling tracing with mongoc_log_trace_disable() signifi‐
126 cantly reduces the overhead, but cannot remove it completely.
127
129 MongoDB, Inc
130
132 2017-present, MongoDB, Inc
133
134
135
136
1371.24.3 Aug 17, 2023 MONGOC_LOGGING(3)