3 * Copyright 2021, João Valverde <j@v6e.pt>
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
22 #include <ws_symbol_export.h>
23 #include <ws_attributes.h>
24 #include <ws_log_defs.h>
25 #include <ws_posix_compat.h>
28 #define _LOG_DOMAIN WS_LOG_DOMAIN
30 #define _LOG_DOMAIN ""
34 #define _LOG_DEBUG_ENABLED true
36 #define _LOG_DEBUG_ENABLED false
40 * Define the macro WS_LOG_DOMAIN *before* including this header,
42 * #define WS_LOG_DOMAIN LOG_DOMAIN_MAIN
47 #endif /* __cplusplus */
50 * Console open preference is stored in the Windows registry.
51 * HKEY_CURRENT_USER\Software\Wireshark\ConsoleOpen
53 #define LOG_HKCU_CONSOLE_OPEN "ConsoleOpen"
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
;
62 ws_log_console_open_pref ws_log_console_open
;
65 struct tm tstamp_secs
;
70 /** Callback for registering a log writer. */
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
,
78 /** Callback for freeing a user data pointer. */
79 typedef void (ws_log_writer_free_data_cb
)(void *user_data
);
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
);
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
);
96 /** Configure log levels "info" and below to use stdout.
98 * Normally all log messages are written to stderr. For backward compatibility
99 * with GLib calling this function with true configures log levels "info",
100 * "debug" and "noisy" to be written to stdout.
103 void ws_log_console_writer_set_use_stdout(bool use_stdout
);
106 /** Convert a numerical level to its string representation. */
109 const char *ws_log_level_to_string(enum ws_log_level level
);
112 /** Checks if a domain and level combination generate output.
114 * Returns true if a message will be printed for the domain/level combo.
117 bool ws_log_msg_is_active(const char *domain
, enum ws_log_level level
);
120 /** Return the currently active log level. */
122 enum ws_log_level
ws_log_get_level(void);
125 /** Set the active log level. Returns the active level or LOG_LEVEL_NONE
126 * if level is invalid. */
128 enum ws_log_level
ws_log_set_level(enum ws_log_level level
);
131 /** Set the active log level from a string.
133 * String levels are "error", "critical", "warning", "message", "info",
134 * "debug" and "noisy" (case insensitive).
135 * Returns the new log level or LOG_LEVEL NONE if the string representation
139 enum ws_log_level
ws_log_set_level_str(const char *str_level
);
142 /** Set a domain filter from a string.
144 * Domain filter is a case insensitive list separated by ',' or ';'. Only
145 * the domains in the filter will generate output; the others will be muted.
146 * Filter expressions can be preceded by '!' to invert the sense of the match.
147 * In this case only non-matching domains will generate output.
150 void ws_log_set_domain_filter(const char *domain_filter
);
152 /** Set a fatal domain filter from a string.
154 * Domain filter is a case insensitive list separated by ',' or ';'. Domains
155 * in the filter will cause the program to abort.
158 void ws_log_set_fatal_domain_filter(const char *domain_filter
);
161 /** Set a debug filter from a string.
163 * A debug filter lists all domains that should have debug level output turned
164 * on, regardless of the global log level and domain filter. If negated
165 * then debug (and below) will be disabled and the others unaffected by
169 void ws_log_set_debug_filter(const char *str_filter
);
172 /** Set a noisy filter from a string.
174 * Same as ws_log_set_debug_filter() for "noisy" level.
177 void ws_log_set_noisy_filter(const char *str_filter
);
180 /** Set the fatal log level.
182 * Sets the log level at which calls to ws_log() will abort the program. The
183 * argument can be LOG_LEVEL_ERROR, LOG_LEVEL_CRITICAL or LOG_LEVEL_WARNING.
184 * Level LOG_LEVEL_ERROR is always fatal.
187 enum ws_log_level
ws_log_set_fatal_level(enum ws_log_level level
);
190 /** Set the fatal log level from a string.
192 * Same as ws_log_set_fatal(), but accepts the strings "error", critical" or
193 * "warning" instead as arguments.
196 enum ws_log_level
ws_log_set_fatal_level_str(const char *str_level
);
199 /** Set the active log writer.
201 * The parameter 'writer' can be NULL to use the default writer.
204 void ws_log_set_writer(ws_log_writer_cb
*writer
);
207 /** Set the active log writer.
209 * The parameter 'writer' can be NULL to use the default writer.
210 * Accepts an extra user_data parameter that will be passed to
214 void ws_log_set_writer_with_data(ws_log_writer_cb
*writer
,
216 ws_log_writer_free_data_cb
*free_user_data
);
219 #define LOG_ARGS_NOEXIT -1
221 /** Parses the command line arguments for log options.
223 * Returns zero for no error, non-zero for one or more invalid options.
226 int ws_log_parse_args(int *argc_ptr
, char *argv
[],
227 void (*vcmdarg_err
)(const char *, va_list ap
),
231 /** Initializes the logging code.
233 * Must be called at startup before using the log API. If provided
234 * vcmdarg_err is used to print initialization errors. This usually means
235 * a misconfigured environment variable.
238 void ws_log_init(void (*vcmdarg_err
)(const char *, va_list ap
));
241 /** Initializes the logging code.
243 * Can be used instead of wslog_init(). Takes an extra writer argument.
244 * If provided this callback will be used instead of the default writer.
247 void ws_log_init_with_writer(ws_log_writer_cb
*writer
,
248 void (*vcmdarg_err
)(const char *, va_list ap
));
251 /** Initializes the logging code.
253 * Accepts a user data pointer in addition to the writer. This pointer will
254 * be provided to the writer with every invocation. If provided
255 * free_user_data will be called during cleanup.
258 void ws_log_init_with_writer_and_data(ws_log_writer_cb
*writer
,
260 ws_log_writer_free_data_cb
*free_user_data
,
261 void (*vcmdarg_err
)(const char *, va_list ap
));
264 /** This function is called to output a message to the log.
266 * Takes a format string and a variable number of arguments.
269 void ws_log(const char *domain
, enum ws_log_level level
,
270 const char *format
, ...) G_GNUC_PRINTF(3,4);
273 /** This function is called to output a message to the log.
275 * Takes a format string and a 'va_list'.
278 void ws_logv(const char *domain
, enum ws_log_level level
,
279 const char *format
, va_list ap
);
282 /** This function is called to output a message to the log.
284 * In addition to the message this function accepts file/line/function
288 void ws_log_full(const char *domain
, enum ws_log_level level
,
289 const char *file
, long line
, const char *func
,
290 const char *format
, ...) G_GNUC_PRINTF(6,7);
293 /** This function is called to output a message to the log.
295 * In addition to the message this function accepts file/line/function
299 void ws_logv_full(const char *domain
, enum ws_log_level level
,
300 const char *file
, long line
, const char *func
,
301 const char *format
, va_list ap
);
306 void ws_log_fatal_full(const char *domain
, enum ws_log_level level
,
307 const char *file
, long line
, const char *func
,
308 const char *format
, ...) G_GNUC_PRINTF(6,7);
312 * The if condition avoids -Wunused warnings for variables used only with
313 * WS_DEBUG, typically inside a ws_debug() call. The compiler will
314 * optimize away the dead execution branch.
316 #define _LOG_IF_ACTIVE(active, level, file, line, func, ...) \
319 ws_log_full(_LOG_DOMAIN, level, \
325 #define _LOG_FULL(active, level, ...) \
326 _LOG_IF_ACTIVE(active, level, __FILE__, __LINE__, __func__, __VA_ARGS__)
328 #define _LOG_SIMPLE(active, level, ...) \
329 _LOG_IF_ACTIVE(active, level, NULL, -1, NULL, __VA_ARGS__)
331 /** Logs with "error" level.
333 * Accepts a format string and includes the file and function name.
335 * "error" is always fatal and terminates the program with a coredump.
337 #define ws_error(...) \
338 ws_log_fatal_full(_LOG_DOMAIN, LOG_LEVEL_ERROR, \
339 __FILE__, __LINE__, __func__, __VA_ARGS__)
341 /** Logs with "critical" level.
343 * Accepts a format string and includes the file and function name.
345 #define ws_critical(...) \
346 _LOG_FULL(true, LOG_LEVEL_CRITICAL, __VA_ARGS__)
348 /** Logs with "warning" level.
350 * Accepts a format string and includes the file and function name.
352 #define ws_warning(...) \
353 _LOG_FULL(true, LOG_LEVEL_WARNING, __VA_ARGS__)
355 /** Logs with "message" level.
357 * Accepts a format string and does not include the file and function
358 * name (use ws_log_full instead). This is the default log level.
360 #define ws_message(...) \
361 _LOG_SIMPLE(true, LOG_LEVEL_MESSAGE, __VA_ARGS__)
363 /** Logs with "info" level.
365 * Accepts a format string and does not include the file and function
366 * name (use ws_log_full instead).
368 #define ws_info(...) \
369 _LOG_SIMPLE(true, LOG_LEVEL_INFO, __VA_ARGS__)
371 /** Logs with "debug" level.
373 * Accepts a format string and includes the file and function name.
375 #define ws_debug(...) \
376 _LOG_FULL(_LOG_DEBUG_ENABLED, LOG_LEVEL_DEBUG, __VA_ARGS__)
378 /** Logs with "noisy" level.
380 * Accepts a format string and includes the file and function name.
382 #define ws_noisy(...) \
383 _LOG_FULL(_LOG_DEBUG_ENABLED, LOG_LEVEL_NOISY, __VA_ARGS__)
385 /** Used for temporary debug print outs, always active. */
386 #define WS_DEBUG_HERE(...) \
387 _LOG_FULL(true, LOG_LEVEL_ECHO, __VA_ARGS__)
389 #define WS_NOT_IMPLEMENTED() \
390 ws_error("Not implemented yet")
393 void ws_log_utf8_full(const char *domain
, enum ws_log_level level
,
394 const char *file
, long line
, const char *func
,
395 const char *string
, ssize_t length
, const char *endptr
);
398 #define ws_log_utf8(str, len, endptr) \
400 if (_LOG_DEBUG_ENABLED) { \
401 ws_log_utf8_full(LOG_DOMAIN_UTF_8, LOG_LEVEL_DEBUG, \
402 __FILE__, __LINE__, __func__, \
408 /** This function is called to log a buffer (bytes array).
410 * Accepts an optional 'msg' argument to provide a description.
413 void ws_log_buffer_full(const char *domain
, enum ws_log_level level
,
414 const char *file
, long line
, const char *func
,
415 const uint8_t *buffer
, size_t size
,
416 size_t max_bytes_len
, const char *msg
);
419 #define ws_log_buffer(buf, size, msg) \
421 if (_LOG_DEBUG_ENABLED) { \
422 ws_log_buffer_full(_LOG_DOMAIN, LOG_LEVEL_DEBUG, \
423 __FILE__, __LINE__, __func__, \
424 buf, size, 36, msg ? msg : #buf); \
429 /** Auxiliary function to write custom logging functions.
431 * This function is the same as ws_log_full() but does not perform any
432 * domain/level filtering to avoid a useless double activation check.
433 * It should only be used in conjunction with a pre-check using
434 * ws_log_msg_is_active().
437 void ws_log_write_always_full(const char *domain
, enum ws_log_level level
,
438 const char *file
, long line
, const char *func
,
439 const char *format
, ...) G_GNUC_PRINTF(6,7);
442 /** Define an auxiliary file pointer where messages should be written.
444 * This file, if set, functions in addition to the registered or
445 * default log writer.
448 void ws_log_add_custom_file(FILE *fp
);
452 void ws_log_print_usage(FILE *fp
);
456 #endif /* __cplusplus */
458 #endif /* __WSLOG_H__ */