Wireshark  4.3.0
The Wireshark network protocol analyzer
wslog.h
Go to the documentation of this file.
1 
12 #ifndef __WSLOG_H__
13 #define __WSLOG_H__
14 
15 #include <inttypes.h>
16 #include <stdbool.h>
17 #include <stdio.h>
18 #include <stdarg.h>
19 #include <time.h>
20 #include <glib.h>
21 
22 #include <ws_symbol_export.h>
23 #include <ws_attributes.h>
24 #include <ws_log_defs.h>
25 #include <ws_posix_compat.h>
26 
27 #ifdef WS_LOG_DOMAIN
28 #define _LOG_DOMAIN WS_LOG_DOMAIN
29 #else
30 #define _LOG_DOMAIN ""
31 #endif
32 
33 #ifdef WS_DEBUG
34 #define _LOG_DEBUG_ENABLED true
35 #else
36 #define _LOG_DEBUG_ENABLED false
37 #endif
38 
39 /*
40  * Define the macro WS_LOG_DOMAIN *before* including this header,
41  * for example:
42  * #define WS_LOG_DOMAIN LOG_DOMAIN_MAIN
43  */
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif /* __cplusplus */
48 
49 /*
50  * Console open preference is stored in the Windows registry.
51  * HKEY_CURRENT_USER\Software\Wireshark\ConsoleOpen
52  */
53 #define LOG_HKCU_CONSOLE_OPEN "ConsoleOpen"
54 
55 typedef enum {
56  LOG_CONSOLE_OPEN_NEVER,
57  LOG_CONSOLE_OPEN_AUTO, /* On demand. */
58  LOG_CONSOLE_OPEN_ALWAYS, /* Open during startup. */
59 } ws_log_console_open_pref;
60 
61 WSUTIL_EXPORT
62 ws_log_console_open_pref ws_log_console_open;
63 
64 typedef struct {
65  struct tm tstamp_secs;
66  long nanosecs;
67  intmax_t pid;
69 
71 typedef void (ws_log_writer_cb)(const char *domain, enum ws_log_level level,
72  const char *file, long line, const char *func,
73  const char *fatal_msg, ws_log_manifest_t *mft,
74  const char *user_format, va_list user_ap,
75  void *user_data);
76 
77 
79 typedef void (ws_log_writer_free_data_cb)(void *user_data);
80 
81 
82 WS_DLL_PUBLIC
83 void ws_log_file_writer(FILE *fp, const char *domain, enum ws_log_level level,
84  const char *file, long line, const char *func,
85  ws_log_manifest_t *mft,
86  const char *user_format, va_list user_ap);
87 
88 
89 WS_DLL_PUBLIC
90 void ws_log_console_writer(const char *domain, enum ws_log_level level,
91  const char *file, long line, const char *func,
92  ws_log_manifest_t *mft,
93  const char *user_format, va_list user_ap);
94 
95 
102 WS_DLL_PUBLIC
103 void ws_log_console_writer_set_use_stdout(bool use_stdout);
104 
105 
107 WS_DLL_PUBLIC
108 WS_RETNONNULL
109 const char *ws_log_level_to_string(enum ws_log_level level);
110 
111 
116 WS_DLL_PUBLIC
117 bool ws_log_msg_is_active(const char *domain, enum ws_log_level level);
118 
119 
121 WS_DLL_PUBLIC
122 enum ws_log_level ws_log_get_level(void);
123 
124 
127 WS_DLL_PUBLIC
128 enum ws_log_level ws_log_set_level(enum ws_log_level level);
129 
130 
138 WS_DLL_PUBLIC
139 enum ws_log_level ws_log_set_level_str(const char *str_level);
140 
141 
149 WS_DLL_PUBLIC
150 void ws_log_set_domain_filter(const char *domain_filter);
151 
157 WS_DLL_PUBLIC
158 void ws_log_set_fatal_domain_filter(const char *domain_filter);
159 
160 
168 WS_DLL_PUBLIC
169 void ws_log_set_debug_filter(const char *str_filter);
170 
171 
176 WS_DLL_PUBLIC
177 void ws_log_set_noisy_filter(const char *str_filter);
178 
179 
186 WS_DLL_PUBLIC
187 enum ws_log_level ws_log_set_fatal_level(enum ws_log_level level);
188 
189 
195 WS_DLL_PUBLIC
196 enum ws_log_level ws_log_set_fatal_level_str(const char *str_level);
197 
198 
203 WS_DLL_PUBLIC
204 void ws_log_set_writer(ws_log_writer_cb *writer);
205 
206 
213 WS_DLL_PUBLIC
215  void *user_data,
216  ws_log_writer_free_data_cb *free_user_data);
217 
218 
219 #define LOG_ARGS_NOEXIT -1
220 
225 WS_DLL_PUBLIC
226 int ws_log_parse_args(int *argc_ptr, char *argv[],
227  void (*vcmdarg_err)(const char *, va_list ap),
228  int exit_failure);
229 
230 
237 WS_DLL_PUBLIC
238 void ws_log_init(const char *progname,
239  void (*vcmdarg_err)(const char *, va_list ap));
240 
241 
247 WS_DLL_PUBLIC
248 void ws_log_init_with_writer(const char *progname,
249  ws_log_writer_cb *writer,
250  void (*vcmdarg_err)(const char *, va_list ap));
251 
252 
259 WS_DLL_PUBLIC
260 void ws_log_init_with_writer_and_data(const char *progname,
261  ws_log_writer_cb *writer,
262  void *user_data,
263  ws_log_writer_free_data_cb *free_user_data,
264  void (*vcmdarg_err)(const char *, va_list ap));
265 
266 
271 WS_DLL_PUBLIC
272 void ws_log(const char *domain, enum ws_log_level level,
273  const char *format, ...) G_GNUC_PRINTF(3,4);
274 
275 
280 WS_DLL_PUBLIC
281 void ws_logv(const char *domain, enum ws_log_level level,
282  const char *format, va_list ap);
283 
284 
290 WS_DLL_PUBLIC
291 void ws_log_full(const char *domain, enum ws_log_level level,
292  const char *file, long line, const char *func,
293  const char *format, ...) G_GNUC_PRINTF(6,7);
294 
295 
301 WS_DLL_PUBLIC
302 void ws_logv_full(const char *domain, enum ws_log_level level,
303  const char *file, long line, const char *func,
304  const char *format, va_list ap);
305 
306 
307 WS_DLL_PUBLIC
308 WS_NORETURN
309 void ws_log_fatal_full(const char *domain, enum ws_log_level level,
310  const char *file, long line, const char *func,
311  const char *format, ...) G_GNUC_PRINTF(6,7);
312 
313 
314 /*
315  * The if condition avoids -Wunused warnings for variables used only with
316  * WS_DEBUG, typically inside a ws_debug() call. The compiler will
317  * optimize away the dead execution branch.
318  */
319 #define _LOG_IF_ACTIVE(active, level, file, line, func, ...) \
320  do { \
321  if (active) { \
322  ws_log_full(_LOG_DOMAIN, level, \
323  file, line, func, \
324  __VA_ARGS__); \
325  } \
326  } while (0)
327 
328 #define _LOG_FULL(active, level, ...) \
329  _LOG_IF_ACTIVE(active, level, __FILE__, __LINE__, __func__, __VA_ARGS__)
330 
331 #define _LOG_SIMPLE(active, level, ...) \
332  _LOG_IF_ACTIVE(active, level, NULL, -1, NULL, __VA_ARGS__)
333 
340 #define ws_error(...) \
341  ws_log_fatal_full(_LOG_DOMAIN, LOG_LEVEL_ERROR, \
342  __FILE__, __LINE__, __func__, __VA_ARGS__)
343 
348 #define ws_critical(...) \
349  _LOG_FULL(true, LOG_LEVEL_CRITICAL, __VA_ARGS__)
350 
355 #define ws_warning(...) \
356  _LOG_FULL(true, LOG_LEVEL_WARNING, __VA_ARGS__)
357 
363 #define ws_message(...) \
364  _LOG_SIMPLE(true, LOG_LEVEL_MESSAGE, __VA_ARGS__)
365 
371 #define ws_info(...) \
372  _LOG_SIMPLE(true, LOG_LEVEL_INFO, __VA_ARGS__)
373 
378 #define ws_debug(...) \
379  _LOG_FULL(_LOG_DEBUG_ENABLED, LOG_LEVEL_DEBUG, __VA_ARGS__)
380 
385 #define ws_noisy(...) \
386  _LOG_FULL(_LOG_DEBUG_ENABLED, LOG_LEVEL_NOISY, __VA_ARGS__)
387 
389 #define WS_DEBUG_HERE(...) \
390  _LOG_FULL(true, LOG_LEVEL_ECHO, __VA_ARGS__)
391 
392 #define WS_NOT_IMPLEMENTED() \
393  ws_error("Not implemented yet")
394 
395 WS_DLL_PUBLIC
396 void ws_log_utf8_full(const char *domain, enum ws_log_level level,
397  const char *file, long line, const char *func,
398  const char *string, ssize_t length, const char *endptr);
399 
400 
401 #define ws_log_utf8(str, len, endptr) \
402  do { \
403  if (_LOG_DEBUG_ENABLED) { \
404  ws_log_utf8_full(LOG_DOMAIN_UTF_8, LOG_LEVEL_DEBUG, \
405  __FILE__, __LINE__, __func__, \
406  str, len, endptr); \
407  } \
408  } while (0)
409 
410 
415 WS_DLL_PUBLIC
416 void ws_log_buffer_full(const char *domain, enum ws_log_level level,
417  const char *file, long line, const char *func,
418  const uint8_t *buffer, size_t size,
419  size_t max_bytes_len, const char *msg);
420 
421 
422 #define ws_log_buffer(buf, size) \
423  do { \
424  if (_LOG_DEBUG_ENABLED) { \
425  ws_log_buffer_full(_LOG_DOMAIN, LOG_LEVEL_DEBUG, \
426  __FILE__, __LINE__, __func__, \
427  buf, size, 36, #buf); \
428  } \
429  } while (0)
430 
431 
439 WS_DLL_PUBLIC
440 void ws_log_write_always_full(const char *domain, enum ws_log_level level,
441  const char *file, long line, const char *func,
442  const char *format, ...) G_GNUC_PRINTF(6,7);
443 
444 
450 WS_DLL_PUBLIC
451 void ws_log_add_custom_file(FILE *fp);
452 
453 
454 WS_DLL_PUBLIC
455 void ws_log_print_usage(FILE *fp);
456 
457 #ifdef __cplusplus
458 }
459 #endif /* __cplusplus */
460 
461 #endif /* __WSLOG_H__ */
Definition: mcast_stream.h:30
Definition: wslog.h:64
WS_DLL_PUBLIC void ws_log(const char *domain, enum ws_log_level level, const char *format,...) G_GNUC_PRINTF(3
WS_DLL_PUBLIC void ws_log_init_with_writer_and_data(const char *progname, ws_log_writer_cb *writer, void *user_data, ws_log_writer_free_data_cb *free_user_data, void(*vcmdarg_err)(const char *, va_list ap))
Definition: wslog.c:936
WS_DLL_PUBLIC void ws_log_set_writer_with_data(ws_log_writer_cb *writer, void *user_data, ws_log_writer_free_data_cb *free_user_data)
Definition: wslog.c:775
WS_DLL_PUBLIC void ws_log_set_domain_filter(const char *domain_filter)
Definition: wslog.c:713
WS_DLL_PUBLIC void ws_log_set_debug_filter(const char *str_filter)
Definition: wslog.c:727
void() ws_log_writer_free_data_cb(void *user_data)
Definition: wslog.h:79
WS_DLL_PUBLIC void ws_log_set_fatal_domain_filter(const char *domain_filter)
Definition: wslog.c:720
WS_DLL_PUBLIC void ws_log_console_writer_set_use_stdout(bool use_stdout)
Definition: wslog.c:1373
WS_DLL_PUBLIC enum ws_log_level ws_log_set_level(enum ws_log_level level)
Definition: wslog.c:350
WS_DLL_PUBLIC void ws_log_set_noisy_filter(const char *str_filter)
Definition: wslog.c:734
WS_DLL_PUBLIC void WS_DLL_PUBLIC void ws_logv_full(const char *domain, enum ws_log_level level, const char *file, long line, const char *func, const char *format, va_list ap)
Definition: wslog.c:1154
WS_DLL_PUBLIC WS_RETNONNULL const char * ws_log_level_to_string(enum ws_log_level level)
Definition: wslog.c:136
WS_DLL_PUBLIC int ws_log_parse_args(int *argc_ptr, char *argv[], void(*vcmdarg_err)(const char *, va_list ap), int exit_failure)
Definition: wslog.c:502
WS_DLL_PUBLIC void ws_log_write_always_full(const char *domain, enum ws_log_level level, const char *file, long line, const char *func, const char *format,...) G_GNUC_PRINTF(6
void() ws_log_writer_cb(const char *domain, enum ws_log_level level, const char *file, long line, const char *func, const char *fatal_msg, ws_log_manifest_t *mft, const char *user_format, va_list user_ap, void *user_data)
Definition: wslog.h:71
WS_DLL_PUBLIC void ws_log_buffer_full(const char *domain, enum ws_log_level level, const char *file, long line, const char *func, const uint8_t *buffer, size_t size, size_t max_bytes_len, const char *msg)
Definition: wslog.c:1326
WS_DLL_PUBLIC enum ws_log_level ws_log_set_fatal_level(enum ws_log_level level)
Definition: wslog.c:741
WS_DLL_PUBLIC void WS_DLL_PUBLIC void ws_log_add_custom_file(FILE *fp)
Definition: wslog.c:1396
WS_DLL_PUBLIC void ws_log_init(const char *progname, void(*vcmdarg_err)(const char *, va_list ap))
Definition: wslog.c:847
WS_DLL_PUBLIC void ws_log_full(const char *domain, enum ws_log_level level, const char *file, long line, const char *func, const char *format,...) G_GNUC_PRINTF(6
WS_DLL_PUBLIC void WS_DLL_PUBLIC void ws_logv(const char *domain, enum ws_log_level level, const char *format, va_list ap)
Definition: wslog.c:1143
WS_DLL_PUBLIC void ws_log_init_with_writer(const char *progname, ws_log_writer_cb *writer, void(*vcmdarg_err)(const char *, va_list ap))
Definition: wslog.c:927
WS_DLL_PUBLIC enum ws_log_level ws_log_get_level(void)
Definition: wslog.c:344
WS_DLL_PUBLIC void ws_log_set_writer(ws_log_writer_cb *writer)
Definition: wslog.c:764
WS_DLL_PUBLIC bool ws_log_msg_is_active(const char *domain, enum ws_log_level level)
Definition: wslog.c:278
WS_DLL_PUBLIC enum ws_log_level ws_log_set_fatal_level_str(const char *str_level)
Definition: wslog.c:755
WS_DLL_PUBLIC enum ws_log_level ws_log_set_level_str(const char *str_level)
Definition: wslog.c:362