2021-02-27 03:38:15 +00:00
|
|
|
/*
|
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
|
|
|
* Copyright 2021 Gerald Combs
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __WSLOG_H__
|
|
|
|
#define __WSLOG_H__
|
|
|
|
|
2021-06-16 13:11:08 +00:00
|
|
|
#include <ws_log_defs.h>
|
2021-06-06 15:43:27 +00:00
|
|
|
#include <ws_symbol_export.h>
|
|
|
|
#include <glib.h>
|
2021-06-08 01:46:52 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
2021-06-06 15:43:27 +00:00
|
|
|
|
2021-06-08 01:46:52 +00:00
|
|
|
#ifndef WS_LOG_DOMAIN
|
|
|
|
#define WS_LOG_DOMAIN LOG_DOMAIN_DEFAULT
|
|
|
|
#endif
|
|
|
|
|
2021-06-16 13:11:08 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif /* __cplusplus */
|
2021-06-08 01:46:52 +00:00
|
|
|
|
2021-06-10 21:13:11 +00:00
|
|
|
/** Callback for registering a log writer. */
|
2021-06-14 18:53:59 +00:00
|
|
|
typedef void (ws_log_writer_cb)(const char *domain, enum ws_log_level level,
|
|
|
|
const char *timestamp,
|
|
|
|
const char *file, int line, const char *func,
|
|
|
|
const char *user_format, va_list user_ap,
|
2021-06-08 01:46:52 +00:00
|
|
|
void *user_data);
|
|
|
|
|
|
|
|
|
2021-06-10 21:13:11 +00:00
|
|
|
/** Callback for freeing a user data pointer. */
|
|
|
|
typedef void (ws_log_writer_free_data_cb)(void *user_data);
|
2021-06-08 01:46:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
WS_DLL_PUBLIC
|
2021-06-14 18:53:59 +00:00
|
|
|
void ws_log_default_writer(const char *domain, enum ws_log_level level,
|
|
|
|
const char *timestamp,
|
|
|
|
const char *file, int line, const char *func,
|
|
|
|
const char *user_format, va_list user_ap,
|
|
|
|
void *user_data);
|
2021-06-08 01:46:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
/** Convert a numerical level to its string representation. */
|
|
|
|
WS_DLL_PUBLIC
|
|
|
|
const char *ws_log_level_to_string(enum ws_log_level level);
|
|
|
|
|
|
|
|
|
|
|
|
/** Checks if the active log level would discard a message for the given
|
|
|
|
* log domain.
|
|
|
|
*
|
|
|
|
* Returns TRUE if a message will be discarded for the domain/log_level combo.
|
|
|
|
*/
|
|
|
|
WS_DLL_PUBLIC
|
|
|
|
gboolean ws_log_level_is_active(enum ws_log_level level);
|
|
|
|
|
|
|
|
|
|
|
|
/** Return the currently active log level. */
|
2021-06-06 15:43:27 +00:00
|
|
|
WS_DLL_PUBLIC
|
2021-06-08 01:46:52 +00:00
|
|
|
enum ws_log_level ws_log_get_level(void);
|
|
|
|
|
|
|
|
|
|
|
|
/** Set the actice log level. Returns the same value (the active level). */
|
|
|
|
WS_DLL_PUBLIC
|
|
|
|
enum ws_log_level ws_log_set_level(enum ws_log_level log_level);
|
|
|
|
|
|
|
|
|
|
|
|
/** Set the actice log level from a string.
|
|
|
|
*
|
|
|
|
* String levels are "error", "critical", "warning", "message", "info" and
|
|
|
|
* "debug" (case insensitive).
|
|
|
|
* Returns the new log level or WS_LOG_LEVEL NONE if the string representation
|
|
|
|
* is invalid.
|
|
|
|
*/
|
|
|
|
WS_DLL_PUBLIC
|
|
|
|
enum ws_log_level ws_log_set_level_str(const char *str_level);
|
|
|
|
|
|
|
|
|
2021-06-11 12:39:16 +00:00
|
|
|
/** Set a domain filter from a string.
|
|
|
|
*
|
|
|
|
* Domain filter is a case insensitive list separated by ',' or ';'. Only
|
|
|
|
* the domains in the filter will generate output; the others will be muted.
|
2021-06-15 23:43:25 +00:00
|
|
|
* Filter expressions can be preceded by '!' to invert the sense of the match.
|
|
|
|
* In this case only non-matching domains will generate output.
|
2021-06-11 12:39:16 +00:00
|
|
|
*/
|
|
|
|
WS_DLL_PUBLIC
|
2021-06-16 05:54:23 +00:00
|
|
|
void ws_log_set_domain_filter(const char *domain_filter);
|
2021-06-11 12:39:16 +00:00
|
|
|
|
|
|
|
|
2021-06-16 03:20:15 +00:00
|
|
|
/** Set a debug filter from a string.
|
|
|
|
*
|
|
|
|
* A debug filter lists all domains that should have debug level output turned
|
|
|
|
* on, regardless of the global log level and domain filter.
|
|
|
|
*/
|
|
|
|
WS_DLL_PUBLIC
|
2021-06-16 05:54:23 +00:00
|
|
|
void ws_log_set_debug_filter(const char *str_filter);
|
|
|
|
|
|
|
|
|
|
|
|
/** Set a noisy filter from a string.
|
|
|
|
*
|
|
|
|
* A noisy filter lists all domains that should have noisy level output turned
|
|
|
|
* on, regardless of the global log level and domain filter.
|
|
|
|
*/
|
|
|
|
WS_DLL_PUBLIC
|
|
|
|
void ws_log_set_noisy_filter(const char *str_filter);
|
2021-06-16 03:20:15 +00:00
|
|
|
|
|
|
|
|
2021-06-16 02:50:45 +00:00
|
|
|
/** Set the fatal log level.
|
|
|
|
*
|
|
|
|
* Sets the log level at which calls to ws_log() will abort the program. The
|
|
|
|
* argument can be LOG_LEVEL_CRITICAL or LOG_LEVEL_WARNING. Level
|
|
|
|
* LOG_LEVEL_ERROR is always fatal.
|
|
|
|
*/
|
|
|
|
WS_DLL_PUBLIC
|
|
|
|
enum ws_log_level ws_log_set_fatal(enum ws_log_level log_level);
|
|
|
|
|
|
|
|
|
|
|
|
/** Set the fatal log level from a string.
|
|
|
|
*
|
|
|
|
* Same as ws_log_set_fatal(), but accepts the strings "critical" or "warnings"
|
|
|
|
* instead as arguments.
|
|
|
|
*/
|
|
|
|
WS_DLL_PUBLIC
|
|
|
|
enum ws_log_level ws_log_set_fatal_str(const char *str_level);
|
|
|
|
|
|
|
|
|
2021-06-14 15:51:19 +00:00
|
|
|
/** Parses the command line arguments for log options.
|
2021-06-11 12:39:16 +00:00
|
|
|
*
|
2021-06-14 15:51:19 +00:00
|
|
|
* Returns zero for no error, non-zero for a bad option value.
|
2021-06-11 12:39:16 +00:00
|
|
|
*/
|
|
|
|
WS_DLL_PUBLIC
|
2021-06-14 15:51:19 +00:00
|
|
|
int ws_log_parse_args(int *argc_ptr, char *argv[], void (*print_err)(const char *, ...));
|
2021-06-11 12:39:16 +00:00
|
|
|
|
|
|
|
|
2021-06-08 01:46:52 +00:00
|
|
|
/** Initializes the logging code.
|
|
|
|
*
|
|
|
|
* Must be called at startup before using the log API. If provided the
|
|
|
|
* ws_log_writer_t pointer will be used to write every message. If the writer
|
|
|
|
* is NULL the default log writer is used.
|
|
|
|
*/
|
|
|
|
WS_DLL_PUBLIC
|
2021-06-10 21:13:11 +00:00
|
|
|
void ws_log_init(ws_log_writer_cb *writer);
|
2021-06-08 01:46:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
/** Initializes the logging code.
|
|
|
|
*
|
|
|
|
* Can be used instead of wslog_init(). Takes an extra user data pointer. This
|
|
|
|
* pointer is passed to the writer with each invocation. If a free function
|
|
|
|
* is passed it will be called with user_data when the program terminates.
|
|
|
|
*/
|
|
|
|
WS_DLL_PUBLIC
|
2021-06-10 21:13:11 +00:00
|
|
|
void ws_log_init_with_data(ws_log_writer_cb *writer, void *user_data,
|
|
|
|
ws_log_writer_free_data_cb *free_user_data);
|
2021-06-08 01:46:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
/** This function is called to output a message to the log.
|
|
|
|
*
|
|
|
|
* Takes a format string and a variable number of arguments.
|
|
|
|
*/
|
|
|
|
WS_DLL_PUBLIC
|
2021-06-11 12:39:16 +00:00
|
|
|
void ws_log(const char *domain, enum ws_log_level level,
|
2021-06-08 01:46:52 +00:00
|
|
|
const char *format, ...) G_GNUC_PRINTF(3,4);
|
|
|
|
|
2021-06-12 01:27:43 +00:00
|
|
|
|
2021-06-08 01:46:52 +00:00
|
|
|
/** This function is called to output a message to the log.
|
|
|
|
*
|
|
|
|
* Takes a format string and a 'va_list'.
|
|
|
|
*/
|
|
|
|
WS_DLL_PUBLIC
|
2021-06-11 12:39:16 +00:00
|
|
|
void ws_logv(const char *domain, enum ws_log_level level,
|
2021-06-08 01:46:52 +00:00
|
|
|
const char *format, va_list ap);
|
|
|
|
|
|
|
|
|
|
|
|
/** This function is called to output a message to the log.
|
|
|
|
*
|
|
|
|
* In addition to the message this function accepts file/line/function
|
|
|
|
* information. 'func' may be NULL.
|
|
|
|
*/
|
|
|
|
WS_DLL_PUBLIC
|
2021-06-11 12:39:16 +00:00
|
|
|
void ws_log_full(const char *domain, enum ws_log_level level,
|
2021-06-06 15:43:27 +00:00
|
|
|
const char *file, int line, const char *func,
|
|
|
|
const char *format, ...) G_GNUC_PRINTF(6,7);
|
|
|
|
|
2021-06-08 01:46:52 +00:00
|
|
|
|
2021-06-14 23:01:25 +00:00
|
|
|
/** This function is called to output a message to the log.
|
|
|
|
*
|
|
|
|
* In addition to the message this function accepts file/line/function
|
|
|
|
* information. 'func' may be NULL.
|
|
|
|
*/
|
|
|
|
WS_DLL_PUBLIC
|
|
|
|
void ws_logv_full(const char *domain, enum ws_log_level level,
|
|
|
|
const char *file, int line, const char *func,
|
|
|
|
const char *format, va_list ap);
|
|
|
|
|
|
|
|
|
2021-06-08 01:46:52 +00:00
|
|
|
#define _LOG_FULL(level, ...) ws_log_full(WS_LOG_DOMAIN, level, \
|
|
|
|
__FILE__, __LINE__, G_STRFUNC, __VA_ARGS__)
|
|
|
|
|
|
|
|
/** Logs with "error" level.
|
|
|
|
*
|
|
|
|
* Accepts a format string and includes the file and function name.
|
|
|
|
*
|
|
|
|
* "error" is always fatal and terminates the program with a coredump.
|
|
|
|
*/
|
|
|
|
#define ws_error(...) _LOG_FULL(LOG_LEVEL_ERROR, __VA_ARGS__)
|
|
|
|
|
|
|
|
/** Logs with "critical" level.
|
|
|
|
*
|
|
|
|
* Accepts a format string and includes the file and function name.
|
|
|
|
*/
|
|
|
|
#define ws_critical(...) _LOG_FULL(LOG_LEVEL_CRITICAL, __VA_ARGS__)
|
|
|
|
|
|
|
|
/** Logs with "warning" level.
|
|
|
|
*
|
|
|
|
* Accepts a format string and includes the file and function name.
|
|
|
|
*/
|
|
|
|
#define ws_warning(...) _LOG_FULL(LOG_LEVEL_WARNING, __VA_ARGS__)
|
|
|
|
|
|
|
|
/** Logs with "message" level.
|
|
|
|
*
|
|
|
|
* Accepts a format string and includes the file and function name.
|
|
|
|
*/
|
|
|
|
#define ws_message(...) _LOG_FULL(LOG_LEVEL_MESSAGE, __VA_ARGS__)
|
|
|
|
|
|
|
|
/** Logs with "info" level.
|
|
|
|
*
|
|
|
|
* Accepts a format string and includes the file and function name.
|
|
|
|
*/
|
|
|
|
#define ws_info(...) _LOG_FULL(LOG_LEVEL_INFO, __VA_ARGS__)
|
|
|
|
|
2021-06-09 23:40:52 +00:00
|
|
|
#ifndef WS_DISABLE_DEBUG
|
2021-06-16 05:54:23 +00:00
|
|
|
#define _LOG_DEBUG(level, ...) _LOG_FULL(level, __VA_ARGS__)
|
2021-02-27 03:38:15 +00:00
|
|
|
#else
|
2021-06-16 05:54:23 +00:00
|
|
|
/*
|
|
|
|
* This avoids -Wunused warnings for variables used only with
|
|
|
|
* !WS_DISABLE_DEBUG,typically inside a ws_debug() call. The compiler will
|
|
|
|
* optimize away the dead execution branch.
|
|
|
|
*/
|
|
|
|
#define _LOG_DEBUG(level, ...) \
|
2021-06-08 01:46:52 +00:00
|
|
|
G_STMT_START { \
|
2021-06-16 05:54:23 +00:00
|
|
|
if (0) _LOG_FULL(level, __VA_ARGS__); \
|
2021-06-08 01:46:52 +00:00
|
|
|
} G_STMT_END
|
2021-02-27 03:38:15 +00:00
|
|
|
#endif
|
|
|
|
|
2021-06-16 05:54:23 +00:00
|
|
|
/** Logs with "debug" level.
|
|
|
|
*
|
|
|
|
* Accepts a format string and includes the file and function name.
|
|
|
|
*/
|
|
|
|
#define ws_debug(...) _LOG_DEBUG(LOG_LEVEL_DEBUG, __VA_ARGS__)
|
|
|
|
|
|
|
|
/** Logs with "noisy" level.
|
|
|
|
*
|
|
|
|
* Accepts a format string and includes the file and function name.
|
|
|
|
*/
|
|
|
|
#define ws_noisy(...) _LOG_DEBUG(LOG_LEVEL_NOISY, __VA_ARGS__)
|
|
|
|
|
2021-06-08 01:46:52 +00:00
|
|
|
|
|
|
|
/** Define an auxilliary file pointer where messages should be written.
|
|
|
|
*
|
|
|
|
* This file, if set, functions in addition to the registered log writer.
|
|
|
|
*/
|
|
|
|
WS_DLL_PUBLIC
|
|
|
|
void ws_log_add_custom_file(FILE *fp);
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif /* __cplusplus */
|
|
|
|
|
2021-02-27 03:38:15 +00:00
|
|
|
#endif /* __WSLOG_H__ */
|