diff --git a/CMakeLists.txt b/CMakeLists.txt index 7bb04dd0e0..378396c9b6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3374,10 +3374,10 @@ set(SHARK_PUBLIC_HEADERS cli_main.h file.h globals.h - log.h ws_attributes.h ws_compiler_tests.h ws_diag_control.h + ws_log_domains.h ws_symbol_export.h version_info.h ${CMAKE_BINARY_DIR}/ws_version.h diff --git a/capture/capture-pcap-util.c b/capture/capture-pcap-util.c index 3dc6c9d181..f9dda3cf9a 100644 --- a/capture/capture-pcap-util.c +++ b/capture/capture-pcap-util.c @@ -9,6 +9,7 @@ */ #include "config.h" +#define WS_LOG_DOMAIN LOG_DOMAIN_CAPCHILD #ifdef HAVE_LIBPCAP @@ -68,10 +69,9 @@ #include "capture/capture-pcap-util.h" #include "capture/capture-pcap-util-int.h" -#include "log.h" - #include #include +#include #ifndef _WIN32 #include @@ -1275,23 +1275,19 @@ open_capture_device_pcap_create( pcap_t *pcap_h; int status; - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, - "Calling pcap_create() using %s.", interface_opts->name); + ws_debug("Calling pcap_create() using %s.", interface_opts->name); pcap_h = pcap_create(interface_opts->name, *open_err_str); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, - "pcap_create() returned %p.", (void *)pcap_h); + ws_debug("pcap_create() returned %p.", (void *)pcap_h); if (pcap_h == NULL) { *open_err = CAP_DEVICE_OPEN_ERR_NOT_PERMISSIONS; return NULL; } if (interface_opts->has_snaplen) { - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, - "Calling pcap_set_snaplen() with snaplen %d.", + ws_debug("Calling pcap_set_snaplen() with snaplen %d.", interface_opts->snaplen); pcap_set_snaplen(pcap_h, interface_opts->snaplen); } - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, - "Calling pcap_set_promisc() with promisc_mode %d.", + ws_debug("Calling pcap_set_promisc() with promisc_mode %d.", interface_opts->promisc_mode); pcap_set_promisc(pcap_h, interface_opts->promisc_mode); pcap_set_timeout(pcap_h, timeout); @@ -1337,18 +1333,15 @@ open_capture_device_pcap_create( } #endif /* HAVE_PCAP_SET_TSTAMP_PRECISION */ - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, - "buffersize %d.", interface_opts->buffer_size); + ws_debug("buffersize %d.", interface_opts->buffer_size); if (interface_opts->buffer_size != 0) pcap_set_buffer_size(pcap_h, interface_opts->buffer_size * 1024 * 1024); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, - "monitor_mode %d.", interface_opts->monitor_mode); + ws_debug("monitor_mode %d.", interface_opts->monitor_mode); if (interface_opts->monitor_mode) pcap_set_rfmon(pcap_h, 1); status = pcap_activate(pcap_h); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, - "pcap_activate() returned %d.", status); + ws_debug("pcap_activate() returned %d.", status); if (status < 0) { /* Failed to activate, set to NULL */ if (status == PCAP_ERROR) { @@ -1424,13 +1417,11 @@ open_capture_device_pcap_open_live(interface_options *interface_opts, */ snaplen = 256*1024; } - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, - "pcap_open_live() calling using name %s, snaplen %d, promisc_mode %d.", + ws_debug("pcap_open_live() calling using name %s, snaplen %d, promisc_mode %d.", interface_opts->name, snaplen, interface_opts->promisc_mode); pcap_h = pcap_open_live(interface_opts->name, snaplen, interface_opts->promisc_mode, timeout, *open_err_str); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, - "pcap_open_live() returned %p.", (void *)pcap_h); + ws_debug("pcap_open_live() returned %p.", (void *)pcap_h); if (pcap_h == NULL) { *open_err = CAP_DEVICE_OPEN_ERR_GENERIC; return NULL; @@ -1538,7 +1529,7 @@ open_capture_device(capture_options *capture_opts, Some versions of libpcap may put warnings into the error buffer if they succeed; to tell if that's happened, we have to clear the error buffer, and check if it's still a null string. */ - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Entering open_capture_device()."); + ws_debug("Entering open_capture_device()."); *open_err = CAP_DEVICE_OPEN_NO_ERR; (*open_err_str)[0] = '\0'; #if defined(HAVE_PCAP_OPEN) && defined(HAVE_PCAP_REMOTE) @@ -1563,8 +1554,7 @@ open_capture_device(capture_options *capture_opts, */ snaplen = 256*1024; } - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, - "Calling pcap_open() using name %s, snaplen %d, promisc_mode %d, datatx_udp %d, nocap_rpcap %d.", + ws_debug("Calling pcap_open() using name %s, snaplen %d, promisc_mode %d, datatx_udp %d, nocap_rpcap %d.", interface_opts->name, snaplen, interface_opts->promisc_mode, interface_opts->datatx_udp, interface_opts->nocap_rpcap); @@ -1597,16 +1587,15 @@ open_capture_device(capture_options *capture_opts, sizeof *open_err_str); } } - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, - "pcap_open() returned %p.", (void *)pcap_h); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "open_capture_device %s : %s", pcap_h ? "SUCCESS" : "FAILURE", interface_opts->name); + ws_debug("pcap_open() returned %p.", (void *)pcap_h); + ws_debug("open_capture_device %s : %s", pcap_h ? "SUCCESS" : "FAILURE", interface_opts->name); return pcap_h; } #endif pcap_h = open_capture_device_local(capture_opts, interface_opts, timeout, open_err, open_err_str); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "open_capture_device %s : %s", pcap_h ? "SUCCESS" : "FAILURE", interface_opts->name); + ws_debug("open_capture_device %s : %s", pcap_h ? "SUCCESS" : "FAILURE", interface_opts->name); return pcap_h; } diff --git a/capture/capture_ifinfo.c b/capture/capture_ifinfo.c index 396d0733cf..40e0f03944 100644 --- a/capture/capture_ifinfo.c +++ b/capture/capture_ifinfo.c @@ -9,6 +9,7 @@ */ #include "config.h" +#define WS_LOG_DOMAIN LOG_DOMAIN_CAPTURE #ifdef HAVE_LIBPCAP @@ -23,7 +24,6 @@ #include "capture/capture_session.h" #include "capture/capture_sync.h" #include "extcap.h" -#include "log.h" #include #include @@ -90,8 +90,6 @@ capture_interface_list(int *err, char **err_str, void (*update_cb)(void)) if_info_t *if_info; if_addr_t *if_addr; - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Interface List ..."); - *err = 0; if (err_str) { *err_str = NULL; @@ -101,12 +99,12 @@ capture_interface_list(int *err, char **err_str, void (*update_cb)(void)) ret = sync_interface_list_open(&data, &primary_msg, &secondary_msg, update_cb); if (ret != 0) { /* Add the extcap interfaces that can exist, even if no native interfaces have been found */ - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Loading External Capture Interface List ..."); + ws_message("Loading External Capture Interface List ..."); if_list = append_extcap_interface_list(if_list, err_str); /* err_str is ignored, as the error for the interface loading list will take precedence */ if ( g_list_length(if_list) == 0 ) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Interface List failed. Error %d, %s (%s)", + ws_message("Capture Interface List failed. Error %d, %s (%s)", *err, primary_msg ? primary_msg : "no message", secondary_msg ? secondary_msg : "no secondary message"); if (err_str) { @@ -185,7 +183,7 @@ capture_interface_list(int *err, char **err_str, void (*update_cb)(void)) #endif /* Add the extcap interfaces after the native and remote interfaces */ - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Loading External Capture Interface List ..."); + ws_message("Loading External Capture Interface List ..."); if_list = append_extcap_interface_list(if_list, err_str); return if_list; @@ -205,8 +203,6 @@ capture_get_if_capabilities(const gchar *ifname, gboolean monitor_mode, gchar *data, *primary_msg, *secondary_msg; gchar **raw_list; - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Interface Capabilities ..."); - /* see if the interface is from extcap */ caps = extcap_get_if_dlts(ifname, err_primary_msg); if (caps != NULL) @@ -220,7 +216,7 @@ capture_get_if_capabilities(const gchar *ifname, gboolean monitor_mode, err = sync_if_capabilities_open(ifname, monitor_mode, auth_string, &data, &primary_msg, &secondary_msg, update_cb); if (err != 0) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Interface Capabilities failed. Error %d, %s", + ws_message("Capture Interface Capabilities failed. Error %d, %s", err, primary_msg ? primary_msg : "no message"); if (err_primary_msg) *err_primary_msg = primary_msg; @@ -245,7 +241,7 @@ capture_get_if_capabilities(const gchar *ifname, gboolean monitor_mode, * First line is 0 if monitor mode isn't supported, 1 if it is. */ if (raw_list[0] == NULL || *raw_list[0] == '\0') { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Interface Capabilities returned no information."); + ws_message("Capture Interface Capabilities returned no information."); if (err_primary_msg) { *err_primary_msg = g_strdup("Dumpcap returned no interface capability information"); } @@ -268,7 +264,7 @@ capture_get_if_capabilities(const gchar *ifname, gboolean monitor_mode, break; default: - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Interface Capabilities returned bad information."); + ws_message("Capture Interface Capabilities returned bad information."); if (err_primary_msg) { *err_primary_msg = g_strdup_printf("Dumpcap returned \"%s\" for monitor-mode capability", raw_list[0]); diff --git a/capture/capture_sync.c b/capture/capture_sync.c index afd9f47778..88af85b9e6 100644 --- a/capture/capture_sync.c +++ b/capture/capture_sync.c @@ -9,6 +9,7 @@ */ #include "config.h" +#define WS_LOG_DOMAIN LOG_DOMAIN_CAPTURE #ifdef HAVE_LIBPCAP @@ -20,6 +21,7 @@ #include #include +#include #ifdef _WIN32 #include @@ -81,7 +83,6 @@ #include #include #include "extcap.h" -#include "log.h" #ifdef _WIN32 #include /* For spawning child process */ @@ -230,8 +231,8 @@ sync_pipe_start(capture_options *capture_opts, capture_session *cap_session, inf if (capture_opts->ifaces->len > 1) capture_opts->use_pcapng = TRUE; - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_start"); - capture_opts_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, capture_opts); + ws_debug("sync_pipe_start"); + capture_opts_log(LOG_DOMAIN_CAPTURE, LOG_LEVEL_DEBUG, capture_opts); cap_session->fork_child = WS_INVALID_PID; @@ -447,7 +448,7 @@ sync_pipe_start(capture_options *capture_opts, capture_session *cap_session, inf argv = sync_pipe_add_arg(argv, &argc, capture_opts->save_file); } for (i = 0; i < argc; i++) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "argv[%d]: %s", i, argv[i]); + ws_debug("argv[%d]: %s", i, argv[i]); } if (capture_opts->compress_type) { argv = sync_pipe_add_arg(argv, &argc, "--compress-type"); @@ -682,7 +683,7 @@ sync_pipe_open_command(char* const argv[], int *data_read_fd, *fork_child = WS_INVALID_PID; *data_read_fd = -1; *message_read_fd = -1; - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_open_command"); + ws_debug("sync_pipe_open_command"); if (!msg) { /* We can't return anything */ @@ -1096,21 +1097,21 @@ sync_pipe_run_command(char* const argv[], gchar **data, gchar **primary_msg, int logging_enabled; /* check if logging is actually enabled, otherwise don't expend the CPU generating logging */ - logging_enabled=( (G_LOG_LEVEL_DEBUG | G_LOG_LEVEL_INFO) & G_LOG_LEVEL_MASK & prefs.console_log_level); - if(logging_enabled){ + logging_enabled = ws_log_level_is_active(LOG_LEVEL_INFO); + if (logging_enabled) { start_time = g_get_monotonic_time(); - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_INFO, "sync_pipe_run_command() starts"); - for(i=0; argv[i] != 0; i++) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, " argv[%d]: %s", i, argv[i]); + ws_info("sync_pipe_run_command() starts"); + for (i=0; argv[i] != 0; i++) { + ws_debug(" argv[%d]: %s", i, argv[i]); } } /* do the actual sync pipe run command */ - ret=sync_pipe_run_command_actual(argv, data, primary_msg, secondary_msg, update_cb); + ret = sync_pipe_run_command_actual(argv, data, primary_msg, secondary_msg, update_cb); - if(logging_enabled){ + if (logging_enabled) { elapsed = (g_get_monotonic_time() - start_time) / 1e6; - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_INFO, "sync_pipe_run_command() ends, taking %.3fs, result=%d", elapsed, ret); + ws_info("sync_pipe_run_command() ends, taking %.3fs, result=%d", elapsed, ret); } return ret; @@ -1191,7 +1192,7 @@ sync_interface_list_open(gchar **data, gchar **primary_msg, char **argv; int ret; - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_interface_list_open"); + ws_debug("sync_interface_list_open"); argv = init_pipe_args(&argc); @@ -1236,7 +1237,7 @@ sync_if_capabilities_open(const gchar *ifname, gboolean monitor_mode, const gcha char **argv; int ret; - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_if_capabilities_open"); + ws_debug("sync_if_capabilities_open"); argv = init_pipe_args(&argc); @@ -1291,7 +1292,7 @@ sync_interface_stats_open(int *data_read_fd, ws_process_id *fork_child, gchar ** /*char *secondary_msg_text;*/ char *combined_msg; - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_interface_stats_open"); + ws_debug("sync_interface_stats_open"); argv = init_pipe_args(&argc); @@ -1456,17 +1457,14 @@ pipe_read_bytes(int pipe_fd, char *bytes, int required, char **msg) newly = ws_read(pipe_fd, &bytes[offset], required); if (newly == 0) { /* EOF */ - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, - "read from pipe %d: EOF (capture closed?)", pipe_fd); + ws_debug("read from pipe %d: EOF (capture closed?)", pipe_fd); *msg = 0; return offset; } if (newly < 0) { /* error */ error = errno; - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, - "read from pipe %d: error(%u): %s", pipe_fd, error, - g_strerror(error)); + ws_debug("read from pipe %d: error(%u): %s", pipe_fd, error, g_strerror(error)); *msg = g_strdup_printf("Error reading from sync pipe: %s", g_strerror(error)); return newly; @@ -1501,8 +1499,7 @@ sync_pipe_gets_nonblock(int pipe_fd, char *bytes, int max) { break; } else if (newly == -1) { /* error */ - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, - "read from pipe %d: error(%u): %s", pipe_fd, errno, g_strerror(errno)); + ws_debug("read from pipe %d: error(%u): %s", pipe_fd, errno, g_strerror(errno)); return -1; } else if (bytes[offset] == '\n') { break; @@ -1547,12 +1544,10 @@ pipe_read_block(int pipe_fd, char *indicator, int len, char *msg, * is an "I'm done" indication, so don't report it as an * error. */ - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, - "read %d got an EOF", pipe_fd); + ws_debug("read %d got an EOF", pipe_fd); return 0; } - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, - "read %d failed to read header: %lu", pipe_fd, (long)newly); + ws_debug("read %d failed to read header: %lu", pipe_fd, (long)newly); if (newly != -1) { /* * Short read, but not an immediate EOF. @@ -1568,15 +1563,13 @@ pipe_read_block(int pipe_fd, char *indicator, int len, char *msg, /* only indicator with no value? */ if(required == 0) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, - "read %d indicator: %c empty value", pipe_fd, *indicator); + ws_debug("read %d indicator: %c empty value", pipe_fd, *indicator); return 4; } /* does the data fit into the given buffer? */ if(required > len) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, - "read %d length error, required %d > len %d, header: 0x%02x 0x%02x 0x%02x 0x%02x", + ws_debug("read %d length error, required %d > len %d, header: 0x%02x 0x%02x 0x%02x 0x%02x", pipe_fd, required, len, header[0], header[1], header[2], header[3]); @@ -1584,8 +1577,7 @@ pipe_read_block(int pipe_fd, char *indicator, int len, char *msg, memcpy(msg, header, sizeof(header)); newly = ws_read(pipe_fd, &msg[sizeof(header)], len-sizeof(header)); if (newly < 0) { /* error */ - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, - "read from pipe %d: error(%u): %s", pipe_fd, errno, g_strerror(errno)); + ws_debug("read from pipe %d: error(%u): %s", pipe_fd, errno, g_strerror(errno)); } *err_msg = g_strdup_printf("Unknown message from dumpcap reading header, try to show it as a string: %s", msg); @@ -1604,9 +1596,7 @@ pipe_read_block(int pipe_fd, char *indicator, int len, char *msg, } /* XXX If message is "2part", the msg probably won't be sent to debug log correctly */ - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, - "read %d ok indicator: %c len: %u msg: %s", pipe_fd, *indicator, - len, msg); + ws_debug("read %d ok indicator: %c len: %u msg: %s", pipe_fd, *indicator, len, msg); *err_msg = NULL; return newly + 4; } @@ -1672,7 +1662,7 @@ sync_pipe_input_cb(gint source, gpointer user_data) #ifdef _WIN32 ws_close(cap_session->signal_pipe_write_fd); #endif - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_input_cb: cleaning extcap pipe"); + ws_debug("cleaning extcap pipe"); extcap_if_cleanup(cap_session->capture_opts, &primary_msg); cap_session->closed(cap_session, primary_msg); g_free(primary_msg); @@ -1683,7 +1673,7 @@ sync_pipe_input_cb(gint source, gpointer user_data) switch(indicator) { case SP_FILE: if(!cap_session->new_file(cap_session, buffer)) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_input_cb: file failed, closing capture"); + ws_debug("file failed, closing capture"); /* We weren't able to open the new capture file; user has been alerted. Close the sync pipe. */ @@ -1708,9 +1698,9 @@ sync_pipe_input_cb(gint source, gpointer user_data) break; case SP_PACKET_COUNT: if (!ws_strtou32(buffer, NULL, &npackets)) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_WARNING, "Invalid packets number: %s", buffer); + ws_warning("Invalid packets number: %s", buffer); } - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_input_cb: new packets %u", npackets); + ws_debug("new packets %u", npackets); cap_session->count += npackets; cap_session->new_packets(cap_session, npackets); break; @@ -1781,7 +1771,7 @@ sync_pipe_wait_for_child(ws_process_id fork_child, gchar **msgp) start_time = g_get_monotonic_time(); - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_wait_for_child: wait till child closed"); + ws_debug("wait till child closed"); g_assert(fork_child != WS_INVALID_PID); *msgp = NULL; /* assume no error */ @@ -1849,7 +1839,7 @@ sync_pipe_wait_for_child(ws_process_id fork_child, gchar **msgp) * kills dumpcap, so we should eventually see that and * clean up and terminate. */ - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_WARNING, "sync_pipe_wait_for_child: waitpid returned EINTR. retrying."); + ws_warning("waitpid returned EINTR. retrying."); continue; } else if (errno == ECHILD) { /* @@ -1870,7 +1860,7 @@ sync_pipe_wait_for_child(ws_process_id fork_child, gchar **msgp) #endif elapsed = (g_get_monotonic_time() - start_time) / 1e6; - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_wait_for_child: capture child closed after %.3fs", elapsed); + ws_debug("capture child closed after %.3fs", elapsed); return ret; } @@ -1997,15 +1987,14 @@ signal_pipe_capquit_to_child(capture_session *cap_session) const char quit_msg[] = "QUIT"; int ret; - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "signal_pipe_capquit_to_child"); + ws_debug("signal_pipe_capquit_to_child"); /* it doesn't matter *what* we send here, the first byte will stop the capture */ /* simply sending a "QUIT" string */ /*pipe_write_block(cap_session->signal_pipe_write_fd, SP_QUIT, quit_msg);*/ ret = ws_write(cap_session->signal_pipe_write_fd, quit_msg, sizeof quit_msg); if(ret == -1) { - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_WARNING, - "signal_pipe_capquit_to_child: %d header: error %s", cap_session->signal_pipe_write_fd, g_strerror(errno)); + ws_warning("%d header: error %s", cap_session->signal_pipe_write_fd, g_strerror(errno)); } } #endif @@ -2025,8 +2014,7 @@ sync_pipe_stop(capture_session *cap_session) /* send the SIGINT signal to close the capture child gracefully. */ int sts = kill(cap_session->fork_child, SIGINT); if (sts != 0) { - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_WARNING, - "Sending SIGINT to child failed: %s\n", g_strerror(errno)); + ws_warning("Sending SIGINT to child failed: %s\n", g_strerror(errno)); } #else #define STOP_SLEEP_TIME 500 /* ms */ @@ -2048,8 +2036,7 @@ sync_pipe_stop(capture_session *cap_session) /* Force the issue. */ if (terminate) { - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_WARNING, - "sync_pipe_stop: forcing child to exit"); + ws_warning("sync_pipe_stop: forcing child to exit"); sync_pipe_kill(cap_session->fork_child); } #endif @@ -2065,8 +2052,7 @@ sync_pipe_kill(ws_process_id fork_child) #ifndef _WIN32 int sts = kill(fork_child, SIGTERM); /* SIGTERM so it can clean up if necessary */ if (sts != 0) { - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_WARNING, - "Sending SIGTERM to child failed: %s\n", g_strerror(errno)); + ws_warning("Sending SIGTERM to child failed: %s\n", g_strerror(errno)); } #else /* Remark: This is not the preferred method of closing a process! diff --git a/capture/capture_win_ifnames.c b/capture/capture_win_ifnames.c index 90cc1cbffd..f13c4d2699 100644 --- a/capture/capture_win_ifnames.c +++ b/capture/capture_win_ifnames.c @@ -31,8 +31,6 @@ #define NETIO_STATUS DWORD #endif -#include "log.h" - #include "capture/capture_ifinfo.h" #include "capture/capture_win_ifnames.h" diff --git a/capture_opts.c b/capture_opts.c index 0c7b7de36d..6bf735b707 100644 --- a/capture_opts.c +++ b/capture_opts.c @@ -150,115 +150,115 @@ capture_opts_cleanup(capture_options *capture_opts) /* log content of capture_opts */ void -capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_options *capture_opts) { +capture_opts_log(enum ws_log_domain log_domain, enum ws_log_level log_level, capture_options *capture_opts) { guint i; - g_log(log_domain, log_level, "CAPTURE OPTIONS :"); + ws_log(log_domain, log_level, "CAPTURE OPTIONS :"); for (i = 0; i < capture_opts->ifaces->len; i++) { interface_options *interface_opts; interface_opts = &g_array_index(capture_opts->ifaces, interface_options, i); - g_log(log_domain, log_level, "Interface name[%02d] : %s", i, interface_opts->name ? interface_opts->name : "(unspecified)"); - g_log(log_domain, log_level, "Interface description[%02d] : %s", i, interface_opts->descr ? interface_opts->descr : "(unspecified)"); - g_log(log_domain, log_level, "Interface vendor description[%02d] : %s", i, interface_opts->hardware ? interface_opts->hardware : "(unspecified)"); - g_log(log_domain, log_level, "Display name[%02d]: %s", i, interface_opts->display_name ? interface_opts->display_name : "(unspecified)"); - g_log(log_domain, log_level, "Capture filter[%02d] : %s", i, interface_opts->cfilter ? interface_opts->cfilter : "(unspecified)"); - g_log(log_domain, log_level, "Snap length[%02d] (%u) : %d", i, interface_opts->has_snaplen, interface_opts->snaplen); - g_log(log_domain, log_level, "Link Type[%02d] : %d", i, interface_opts->linktype); - g_log(log_domain, log_level, "Promiscuous Mode[%02d]: %s", i, interface_opts->promisc_mode?"TRUE":"FALSE"); - g_log(log_domain, log_level, "Extcap[%02d] : %s", i, interface_opts->extcap ? interface_opts->extcap : "(unspecified)"); - g_log(log_domain, log_level, "Extcap FIFO[%02d] : %s", i, interface_opts->extcap_fifo ? interface_opts->extcap_fifo : "(unspecified)"); - g_log(log_domain, log_level, "Extcap PID[%02d] : %d", i, interface_opts->extcap_pid); + ws_log(log_domain, log_level, "Interface name[%02d] : %s", i, interface_opts->name ? interface_opts->name : "(unspecified)"); + ws_log(log_domain, log_level, "Interface description[%02d] : %s", i, interface_opts->descr ? interface_opts->descr : "(unspecified)"); + ws_log(log_domain, log_level, "Interface vendor description[%02d] : %s", i, interface_opts->hardware ? interface_opts->hardware : "(unspecified)"); + ws_log(log_domain, log_level, "Display name[%02d]: %s", i, interface_opts->display_name ? interface_opts->display_name : "(unspecified)"); + ws_log(log_domain, log_level, "Capture filter[%02d] : %s", i, interface_opts->cfilter ? interface_opts->cfilter : "(unspecified)"); + ws_log(log_domain, log_level, "Snap length[%02d] (%u) : %d", i, interface_opts->has_snaplen, interface_opts->snaplen); + ws_log(log_domain, log_level, "Link Type[%02d] : %d", i, interface_opts->linktype); + ws_log(log_domain, log_level, "Promiscuous Mode[%02d]: %s", i, interface_opts->promisc_mode?"TRUE":"FALSE"); + ws_log(log_domain, log_level, "Extcap[%02d] : %s", i, interface_opts->extcap ? interface_opts->extcap : "(unspecified)"); + ws_log(log_domain, log_level, "Extcap FIFO[%02d] : %s", i, interface_opts->extcap_fifo ? interface_opts->extcap_fifo : "(unspecified)"); + ws_log(log_domain, log_level, "Extcap PID[%02d] : %d", i, interface_opts->extcap_pid); #ifdef CAN_SET_CAPTURE_BUFFER_SIZE - g_log(log_domain, log_level, "Buffer size[%02d] : %d (MB)", i, interface_opts->buffer_size); + ws_log(log_domain, log_level, "Buffer size[%02d] : %d (MB)", i, interface_opts->buffer_size); #endif - g_log(log_domain, log_level, "Monitor Mode[%02d] : %s", i, interface_opts->monitor_mode?"TRUE":"FALSE"); + ws_log(log_domain, log_level, "Monitor Mode[%02d] : %s", i, interface_opts->monitor_mode?"TRUE":"FALSE"); #ifdef HAVE_PCAP_REMOTE - g_log(log_domain, log_level, "Capture source[%02d] : %s", i, + ws_log(log_domain, log_level, "Capture source[%02d] : %s", i, interface_opts->src_type == CAPTURE_IFLOCAL ? "Local interface" : interface_opts->src_type == CAPTURE_IFREMOTE ? "Remote interface" : "Unknown"); if (interface_opts->src_type == CAPTURE_IFREMOTE) { - g_log(log_domain, log_level, "Remote host[%02d] : %s", i, interface_opts->remote_host ? interface_opts->remote_host : "(unspecified)"); - g_log(log_domain, log_level, "Remote port[%02d] : %s", i, interface_opts->remote_port ? interface_opts->remote_port : "(unspecified)"); + ws_log(log_domain, log_level, "Remote host[%02d] : %s", i, interface_opts->remote_host ? interface_opts->remote_host : "(unspecified)"); + ws_log(log_domain, log_level, "Remote port[%02d] : %s", i, interface_opts->remote_port ? interface_opts->remote_port : "(unspecified)"); } - g_log(log_domain, log_level, "Authentication[%02d] : %s", i, + ws_log(log_domain, log_level, "Authentication[%02d] : %s", i, interface_opts->auth_type == CAPTURE_AUTH_NULL ? "Null" : interface_opts->auth_type == CAPTURE_AUTH_PWD ? "By username/password" : "Unknown"); if (interface_opts->auth_type == CAPTURE_AUTH_PWD) { - g_log(log_domain, log_level, "Auth username[%02d] : %s", i, interface_opts->auth_username ? interface_opts->auth_username : "(unspecified)"); - g_log(log_domain, log_level, "Auth password[%02d] : ", i); + ws_log(log_domain, log_level, "Auth username[%02d] : %s", i, interface_opts->auth_username ? interface_opts->auth_username : "(unspecified)"); + ws_log(log_domain, log_level, "Auth password[%02d] : ", i); } - g_log(log_domain, log_level, "UDP data tfer[%02d] : %u", i, interface_opts->datatx_udp); - g_log(log_domain, log_level, "No cap. RPCAP[%02d] : %u", i, interface_opts->nocap_rpcap); - g_log(log_domain, log_level, "No cap. local[%02d] : %u", i, interface_opts->nocap_local); + ws_log(log_domain, log_level, "UDP data tfer[%02d] : %u", i, interface_opts->datatx_udp); + ws_log(log_domain, log_level, "No cap. RPCAP[%02d] : %u", i, interface_opts->nocap_rpcap); + ws_log(log_domain, log_level, "No cap. local[%02d] : %u", i, interface_opts->nocap_local); #endif #ifdef HAVE_PCAP_SETSAMPLING - g_log(log_domain, log_level, "Sampling meth.[%02d] : %d", i, interface_opts->sampling_method); - g_log(log_domain, log_level, "Sampling param.[%02d] : %d", i, interface_opts->sampling_param); + ws_log(log_domain, log_level, "Sampling meth.[%02d] : %d", i, interface_opts->sampling_method); + ws_log(log_domain, log_level, "Sampling param.[%02d] : %d", i, interface_opts->sampling_param); #endif - g_log(log_domain, log_level, "Timestamp type [%02d] : %s", i, interface_opts->timestamp_type); + ws_log(log_domain, log_level, "Timestamp type [%02d] : %s", i, interface_opts->timestamp_type); } - g_log(log_domain, log_level, "Interface name[df] : %s", capture_opts->default_options.name ? capture_opts->default_options.name : "(unspecified)"); - g_log(log_domain, log_level, "Interface Descr[df] : %s", capture_opts->default_options.descr ? capture_opts->default_options.descr : "(unspecified)"); - g_log(log_domain, log_level, "Interface Hardware Descr[df] : %s", capture_opts->default_options.hardware ? capture_opts->default_options.hardware : "(unspecified)"); - g_log(log_domain, log_level, "Interface display name[df] : %s", capture_opts->default_options.display_name ? capture_opts->default_options.display_name : "(unspecified)"); - g_log(log_domain, log_level, "Capture filter[df] : %s", capture_opts->default_options.cfilter ? capture_opts->default_options.cfilter : "(unspecified)"); - g_log(log_domain, log_level, "Snap length[df] (%u) : %d", capture_opts->default_options.has_snaplen, capture_opts->default_options.snaplen); - g_log(log_domain, log_level, "Link Type[df] : %d", capture_opts->default_options.linktype); - g_log(log_domain, log_level, "Promiscuous Mode[df]: %s", capture_opts->default_options.promisc_mode?"TRUE":"FALSE"); - g_log(log_domain, log_level, "Extcap[df] : %s", capture_opts->default_options.extcap ? capture_opts->default_options.extcap : "(unspecified)"); - g_log(log_domain, log_level, "Extcap FIFO[df] : %s", capture_opts->default_options.extcap_fifo ? capture_opts->default_options.extcap_fifo : "(unspecified)"); + ws_log(log_domain, log_level, "Interface name[df] : %s", capture_opts->default_options.name ? capture_opts->default_options.name : "(unspecified)"); + ws_log(log_domain, log_level, "Interface Descr[df] : %s", capture_opts->default_options.descr ? capture_opts->default_options.descr : "(unspecified)"); + ws_log(log_domain, log_level, "Interface Hardware Descr[df] : %s", capture_opts->default_options.hardware ? capture_opts->default_options.hardware : "(unspecified)"); + ws_log(log_domain, log_level, "Interface display name[df] : %s", capture_opts->default_options.display_name ? capture_opts->default_options.display_name : "(unspecified)"); + ws_log(log_domain, log_level, "Capture filter[df] : %s", capture_opts->default_options.cfilter ? capture_opts->default_options.cfilter : "(unspecified)"); + ws_log(log_domain, log_level, "Snap length[df] (%u) : %d", capture_opts->default_options.has_snaplen, capture_opts->default_options.snaplen); + ws_log(log_domain, log_level, "Link Type[df] : %d", capture_opts->default_options.linktype); + ws_log(log_domain, log_level, "Promiscuous Mode[df]: %s", capture_opts->default_options.promisc_mode?"TRUE":"FALSE"); + ws_log(log_domain, log_level, "Extcap[df] : %s", capture_opts->default_options.extcap ? capture_opts->default_options.extcap : "(unspecified)"); + ws_log(log_domain, log_level, "Extcap FIFO[df] : %s", capture_opts->default_options.extcap_fifo ? capture_opts->default_options.extcap_fifo : "(unspecified)"); #ifdef CAN_SET_CAPTURE_BUFFER_SIZE - g_log(log_domain, log_level, "Buffer size[df] : %d (MB)", capture_opts->default_options.buffer_size); + ws_log(log_domain, log_level, "Buffer size[df] : %d (MB)", capture_opts->default_options.buffer_size); #endif - g_log(log_domain, log_level, "Monitor Mode[df] : %s", capture_opts->default_options.monitor_mode?"TRUE":"FALSE"); + ws_log(log_domain, log_level, "Monitor Mode[df] : %s", capture_opts->default_options.monitor_mode?"TRUE":"FALSE"); #ifdef HAVE_PCAP_REMOTE - g_log(log_domain, log_level, "Capture source[df] : %s", + ws_log(log_domain, log_level, "Capture source[df] : %s", capture_opts->default_options.src_type == CAPTURE_IFLOCAL ? "Local interface" : capture_opts->default_options.src_type == CAPTURE_IFREMOTE ? "Remote interface" : "Unknown"); if (capture_opts->default_options.src_type == CAPTURE_IFREMOTE) { - g_log(log_domain, log_level, "Remote host[df] : %s", capture_opts->default_options.remote_host ? capture_opts->default_options.remote_host : "(unspecified)"); - g_log(log_domain, log_level, "Remote port[df] : %s", capture_opts->default_options.remote_port ? capture_opts->default_options.remote_port : "(unspecified)"); + ws_log(log_domain, log_level, "Remote host[df] : %s", capture_opts->default_options.remote_host ? capture_opts->default_options.remote_host : "(unspecified)"); + ws_log(log_domain, log_level, "Remote port[df] : %s", capture_opts->default_options.remote_port ? capture_opts->default_options.remote_port : "(unspecified)"); } - g_log(log_domain, log_level, "Authentication[df] : %s", + ws_log(log_domain, log_level, "Authentication[df] : %s", capture_opts->default_options.auth_type == CAPTURE_AUTH_NULL ? "Null" : capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD ? "By username/password" : "Unknown"); if (capture_opts->default_options.auth_type == CAPTURE_AUTH_PWD) { - g_log(log_domain, log_level, "Auth username[df] : %s", capture_opts->default_options.auth_username ? capture_opts->default_options.auth_username : "(unspecified)"); - g_log(log_domain, log_level, "Auth password[df] : "); + ws_log(log_domain, log_level, "Auth username[df] : %s", capture_opts->default_options.auth_username ? capture_opts->default_options.auth_username : "(unspecified)"); + ws_log(log_domain, log_level, "Auth password[df] : "); } - g_log(log_domain, log_level, "UDP data tfer[df] : %u", capture_opts->default_options.datatx_udp); - g_log(log_domain, log_level, "No cap. RPCAP[df] : %u", capture_opts->default_options.nocap_rpcap); - g_log(log_domain, log_level, "No cap. local[df] : %u", capture_opts->default_options.nocap_local); + ws_log(log_domain, log_level, "UDP data tfer[df] : %u", capture_opts->default_options.datatx_udp); + ws_log(log_domain, log_level, "No cap. RPCAP[df] : %u", capture_opts->default_options.nocap_rpcap); + ws_log(log_domain, log_level, "No cap. local[df] : %u", capture_opts->default_options.nocap_local); #endif #ifdef HAVE_PCAP_SETSAMPLING - g_log(log_domain, log_level, "Sampling meth. [df] : %d", capture_opts->default_options.sampling_method); - g_log(log_domain, log_level, "Sampling param.[df] : %d", capture_opts->default_options.sampling_param); + ws_log(log_domain, log_level, "Sampling meth. [df] : %d", capture_opts->default_options.sampling_method); + ws_log(log_domain, log_level, "Sampling param.[df] : %d", capture_opts->default_options.sampling_param); #endif - g_log(log_domain, log_level, "Timestamp type [df] : %s", capture_opts->default_options.timestamp_type ? capture_opts->default_options.timestamp_type : "(unspecified)"); - g_log(log_domain, log_level, "SavingToFile : %u", capture_opts->saving_to_file); - g_log(log_domain, log_level, "SaveFile : %s", (capture_opts->save_file) ? capture_opts->save_file : ""); - g_log(log_domain, log_level, "GroupReadAccess : %u", capture_opts->group_read_access); - g_log(log_domain, log_level, "Fileformat : %s", (capture_opts->use_pcapng) ? "PCAPNG" : "PCAP"); - g_log(log_domain, log_level, "RealTimeMode : %u", capture_opts->real_time_mode); - g_log(log_domain, log_level, "ShowInfo : %u", capture_opts->show_info); + ws_log(log_domain, log_level, "Timestamp type [df] : %s", capture_opts->default_options.timestamp_type ? capture_opts->default_options.timestamp_type : "(unspecified)"); + ws_log(log_domain, log_level, "SavingToFile : %u", capture_opts->saving_to_file); + ws_log(log_domain, log_level, "SaveFile : %s", (capture_opts->save_file) ? capture_opts->save_file : ""); + ws_log(log_domain, log_level, "GroupReadAccess : %u", capture_opts->group_read_access); + ws_log(log_domain, log_level, "Fileformat : %s", (capture_opts->use_pcapng) ? "PCAPNG" : "PCAP"); + ws_log(log_domain, log_level, "RealTimeMode : %u", capture_opts->real_time_mode); + ws_log(log_domain, log_level, "ShowInfo : %u", capture_opts->show_info); - g_log(log_domain, log_level, "MultiFilesOn : %u", capture_opts->multi_files_on); - g_log(log_domain, log_level, "FileDuration (%u) : %.3f", capture_opts->has_file_duration, capture_opts->file_duration); - g_log(log_domain, log_level, "FileInterval (%u) : %u", capture_opts->has_file_interval, capture_opts->file_interval); - g_log(log_domain, log_level, "FilePackets (%u) : %u", capture_opts->has_file_packets, capture_opts->file_packets); - g_log(log_domain, log_level, "RingNumFiles (%u) : %u", capture_opts->has_ring_num_files, capture_opts->ring_num_files); - g_log(log_domain, log_level, "RingPrintFiles (%u) : %s", capture_opts->print_file_names, (capture_opts->print_file_names ? capture_opts->print_name_to : "")); + ws_log(log_domain, log_level, "MultiFilesOn : %u", capture_opts->multi_files_on); + ws_log(log_domain, log_level, "FileDuration (%u) : %.3f", capture_opts->has_file_duration, capture_opts->file_duration); + ws_log(log_domain, log_level, "FileInterval (%u) : %u", capture_opts->has_file_interval, capture_opts->file_interval); + ws_log(log_domain, log_level, "FilePackets (%u) : %u", capture_opts->has_file_packets, capture_opts->file_packets); + ws_log(log_domain, log_level, "RingNumFiles (%u) : %u", capture_opts->has_ring_num_files, capture_opts->ring_num_files); + ws_log(log_domain, log_level, "RingPrintFiles (%u) : %s", capture_opts->print_file_names, (capture_opts->print_file_names ? capture_opts->print_name_to : "")); - g_log(log_domain, log_level, "AutostopFiles (%u) : %u", capture_opts->has_autostop_files, capture_opts->autostop_files); - g_log(log_domain, log_level, "AutostopPackets (%u) : %u", capture_opts->has_autostop_packets, capture_opts->autostop_packets); - g_log(log_domain, log_level, "AutostopFilesize(%u) : %u (KB)", capture_opts->has_autostop_filesize, capture_opts->autostop_filesize); - g_log(log_domain, log_level, "AutostopDuration(%u) : %.3f", capture_opts->has_autostop_duration, capture_opts->autostop_duration); + ws_log(log_domain, log_level, "AutostopFiles (%u) : %u", capture_opts->has_autostop_files, capture_opts->autostop_files); + ws_log(log_domain, log_level, "AutostopPackets (%u) : %u", capture_opts->has_autostop_packets, capture_opts->autostop_packets); + ws_log(log_domain, log_level, "AutostopFilesize(%u) : %u (KB)", capture_opts->has_autostop_filesize, capture_opts->autostop_filesize); + ws_log(log_domain, log_level, "AutostopDuration(%u) : %.3f", capture_opts->has_autostop_duration, capture_opts->autostop_duration); } /* diff --git a/capture_opts.h b/capture_opts.h index c815c53586..2c6fffc8a3 100644 --- a/capture_opts.h +++ b/capture_opts.h @@ -22,6 +22,7 @@ #include #include "ringbuffer.h" +#include #ifdef _WIN32 #include @@ -338,7 +339,7 @@ capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg) /* log content of capture_opts */ extern void -capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_options *capture_opts); +capture_opts_log(enum ws_log_domain domain, enum ws_log_level level, capture_options *capture_opts); enum caps_query { CAPS_QUERY_LINK_TYPES = 0x1, diff --git a/cmakeconfig.h.in b/cmakeconfig.h.in index d03badad62..cfe20395c6 100644 --- a/cmakeconfig.h.in +++ b/cmakeconfig.h.in @@ -380,5 +380,6 @@ #endif #include +#include #endif /* __CONFIG_H__ */ diff --git a/debian/libwsutil0.symbols b/debian/libwsutil0.symbols index 6dcc95d870..3449ab932b 100644 --- a/debian/libwsutil0.symbols +++ b/debian/libwsutil0.symbols @@ -225,7 +225,20 @@ libwsutil.so.0 libwsutil0 #MINVER# ws_inet_pton4@Base 2.1.2 ws_inet_pton6@Base 2.1.2 ws_init_sockets@Base 3.1.0 + ws_log@Base 3.5.0 + ws_log_add_custom_file@Base 3.5.0 + ws_log_default_writer@Base 3.5.0 + ws_log_domain_to_string@Base 3.5.0 ws_log_full@Base 3.5.0 + ws_log_get_level@Base 3.5.0 + ws_log_init@Base 3.5.0 + ws_log_init_with_data@Base 3.5.0 + ws_log_level_is_active@Base 3.5.0 + ws_log_level_to_string@Base 3.5.0 + ws_log_set_level@Base 3.5.0 + ws_log_set_level_args@Base 3.5.0 + ws_log_set_level_str@Base 3.5.0 + ws_logv@Base 3.5.0 ws_mempbrk_compile@Base 1.99.4 ws_mempbrk_exec@Base 1.99.4 ws_pipe_close@Base 2.6.5 diff --git a/dumpcap.c b/dumpcap.c index 163f9c69c3..6036dae003 100644 --- a/dumpcap.c +++ b/dumpcap.c @@ -8,6 +8,7 @@ */ #include +#define WS_LOG_DOMAIN LOG_DOMAIN_CAPCHILD #include #include /* for exit() */ @@ -47,6 +48,7 @@ #include #include +#include #ifdef HAVE_LIBCAP # include @@ -78,7 +80,6 @@ #include #include "wsutil/tempfile.h" -#include "log.h" #include "wsutil/file_util.h" #include "wsutil/cpu_info.h" #include "wsutil/os_version_info.h" @@ -326,8 +327,8 @@ static gboolean need_timeout_workaround; #define WRITER_THREAD_TIMEOUT 100000 /* usecs */ static void -console_log_handler(const char *log_domain, GLogLevelFlags log_level, - const char *message, gpointer user_data _U_); +dumpcap_log_writer(const char *message, enum ws_log_domain domain, + enum ws_log_level level, void *ptr); /* capture related options */ static capture_options global_capture_opts; @@ -510,9 +511,7 @@ static void /* .... */ print_caps(const char *pfx) { cap_t caps = cap_get_proc(); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, - "%s: EUID: %d Capabilities: %s", pfx, - geteuid(), cap_to_text(caps, NULL)); + ws_debug("%s: EUID: %d Capabilities: %s", pfx, geteuid(), cap_to_text(caps, NULL)); cap_free(caps); } #else @@ -931,8 +930,7 @@ print_statistics_loop(gboolean machine_readable) * connections. We avoid collecting stats on them. */ if (!strncmp(if_info->name, "nf", 2)) { - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Skipping interface %s for stats", - if_info->name); + ws_debug("Skipping interface %s for stats", if_info->name); continue; } #endif @@ -1025,10 +1023,8 @@ capture_cleanup_handler(DWORD dwCtrlType) user logs out. (XXX - can we explicitly check whether we're running as a service?) */ - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, - "Console: Control signal"); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, - "Console: Control signal, CtrlType: %lu", dwCtrlType); + ws_info("Console: Control signal"); + ws_debug("Console: Control signal, CtrlType: %lu", dwCtrlType); /* Keep capture running if we're a service and a user logs off */ if (capture_child || (dwCtrlType != CTRL_LOGOFF_EVENT)) { @@ -1046,11 +1042,6 @@ capture_cleanup_handler(int signum _U_) SIGTERM. We assume that if the user wanted it to keep running after they logged out, they'd have nohupped it. */ - /* Note: don't call g_log() in the signal handler: if we happened to be in - * g_log() in process context when the signal came in, g_log will detect - * the "recursion" and abort. - */ - capture_loop_stop(); } #endif @@ -1452,7 +1443,7 @@ cap_pipe_read_data_bytes(capture_src *pcap_src, char *errmsg, size_t errmsgl) ssize_t b; #ifdef LOG_CAPTURE_VERBOSE - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "cap_pipe_read_data_bytes read %lu of %lu", + ws_debug("cap_pipe_read_data_bytes read %lu of %lu", pcap_src->cap_pipe_bytes_read, pcap_src->cap_pipe_bytes_to_read); #endif sz = pcap_src->cap_pipe_bytes_to_read - pcap_src->cap_pipe_bytes_read; @@ -1501,7 +1492,7 @@ cap_pipe_read_data_bytes(capture_src *pcap_src, char *errmsg, size_t errmsgl) } pcap_src->cap_pipe_bytes_read += bytes_read; #ifdef LOG_CAPTURE_VERBOSE - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "cap_pipe_read_data_bytes read %lu of %lu", + ws_debug("cap_pipe_read_data_bytes read %lu of %lu", pcap_src->cap_pipe_bytes_read, pcap_src->cap_pipe_bytes_to_read); #endif return bytes_read; @@ -1552,7 +1543,7 @@ cap_pipe_open_live(char *pipename, pcap_src->cap_pipe_h = INVALID_HANDLE_VALUE; #endif - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "cap_pipe_open_live: %s", pipename); + ws_debug("cap_pipe_open_live: %s", pipename); /* * XXX - this blocks until a pcap per-file header has been written to @@ -1868,7 +1859,7 @@ cap_pipe_open_live(char *pipename, return; error: - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "cap_pipe_open_live: error %s", errmsg); + ws_debug("cap_pipe_open_live: error %s", errmsg); pcap_src->cap_pipe_err = PIPERR; cap_pipe_close(fd, pcap_src->from_cap_socket); pcap_src->cap_pipe_fd = -1; @@ -1992,7 +1983,7 @@ pcap_pipe_open_live(int fd, return; error: - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "pcap_pipe_open_live: error %s", errmsg); + ws_debug("pcap_pipe_open_live: error %s", errmsg); pcap_src->cap_pipe_err = PIPERR; cap_pipe_close(fd, pcap_src->from_cap_socket); pcap_src->cap_pipe_fd = -1; @@ -2043,10 +2034,10 @@ pcapng_read_shb(capture_src *pcap_src, switch (shb.magic) { case PCAPNG_MAGIC: - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "pcapng SHB MAGIC"); + ws_debug("pcapng SHB MAGIC"); break; case PCAPNG_SWAPPED_MAGIC: - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "pcapng SHB SWAPPED MAGIC"); + ws_debug("pcapng SHB SWAPPED MAGIC"); /* * pcapng sources can contain all sorts of block types. * Rather than add a bunch of complexity to this code (which is @@ -2141,7 +2132,7 @@ pcapng_adjust_block(capture_src *pcap_src, const pcapng_block_header_t *bh, u_ch g_free(idb_source->idb); memset(idb_source, 0, sizeof(saved_idb_t)); idb_source->deleted = TRUE; - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "%s: deleted pcapng IDB %u", G_STRFUNC, iface_id); + ws_debug("%s: deleted pcapng IDB %u", G_STRFUNC, iface_id); } } g_array_set_size(pcap_src->cap_pipe_info.pcapng.src_iface_to_global, 0); @@ -2160,7 +2151,7 @@ pcapng_adjust_block(capture_src *pcap_src, const pcapng_block_header_t *bh, u_ch g_array_append_val(global_ld.saved_idbs, idb_source); guint32 iface_id = global_ld.saved_idbs->len - 1; g_array_append_val(pcap_src->cap_pipe_info.pcapng.src_iface_to_global, iface_id); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "%s: mapped pcapng IDB %u -> %u from source %u", + ws_debug("%s: mapped pcapng IDB %u -> %u from source %u", G_STRFUNC, pcap_src->cap_pipe_info.pcapng.src_iface_to_global->len - 1, iface_id, pcap_src->interface_id); } break; @@ -2178,7 +2169,7 @@ pcapng_adjust_block(capture_src *pcap_src, const pcapng_block_header_t *bh, u_ch memcpy(pd + sizeof(pcapng_block_header_t), &g_array_index(pcap_src->cap_pipe_info.pcapng.src_iface_to_global, guint32, iface_id), 4); } else { - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "%s: pcapng EPB or ISB interface id %u > max %u", G_STRFUNC, iface_id, pcap_src->cap_pipe_info.pcapng.src_iface_to_global->len); + ws_debug("%s: pcapng EPB or ISB interface id %u > max %u", G_STRFUNC, iface_id, pcap_src->cap_pipe_info.pcapng.src_iface_to_global->len); return FALSE; } } @@ -2203,7 +2194,7 @@ pcapng_pipe_open_live(int fd, guint32 type = BLOCK_TYPE_SHB; pcapng_block_header_t *bh = &pcap_src->cap_pipe_info.pcapng.bh; - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "pcapng_pipe_open_live: fd %d", fd); + ws_debug("pcapng_pipe_open_live: fd %d", fd); /* * A pcapng block begins with the block type followed by the block @@ -2266,7 +2257,7 @@ pcapng_pipe_open_live(int fd, return; error: - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "pcapng_pipe_open_live: error %s", errmsg); + ws_debug("pcapng_pipe_open_live: error %s", errmsg); pcap_src->cap_pipe_err = PIPERR; cap_pipe_close(fd, pcap_src->from_cap_socket); pcap_src->cap_pipe_fd = -1; @@ -2291,7 +2282,7 @@ pcap_pipe_dispatch(loop_data *ld, capture_src *pcap_src, char *errmsg, size_t er pcap_pipe_info_t *pcap_info = &pcap_src->cap_pipe_info.pcap; #ifdef LOG_CAPTURE_VERBOSE - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "pcap_pipe_dispatch"); + ws_debug("pcap_pipe_dispatch"); #endif switch (pcap_src->cap_pipe_state) { @@ -2531,14 +2522,14 @@ pcapng_pipe_dispatch(loop_data *ld, capture_src *pcap_src, char *errmsg, size_t pcapng_block_header_t *bh = &pcap_src->cap_pipe_info.pcapng.bh; #ifdef LOG_CAPTURE_VERBOSE - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "pcapng_pipe_dispatch"); + ws_debug("pcapng_pipe_dispatch"); #endif switch (pcap_src->cap_pipe_state) { case STATE_EXPECT_REC_HDR: #ifdef LOG_CAPTURE_VERBOSE - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "pcapng_pipe_dispatch STATE_EXPECT_REC_HDR"); + ws_debug("pcapng_pipe_dispatch STATE_EXPECT_REC_HDR"); #endif #ifdef _WIN32 if (g_mutex_trylock(pcap_src->cap_pipe_read_mtx)) { @@ -2560,7 +2551,7 @@ pcapng_pipe_dispatch(loop_data *ld, capture_src *pcap_src, char *errmsg, size_t case STATE_READ_REC_HDR: #ifdef LOG_CAPTURE_VERBOSE - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "pcapng_pipe_dispatch STATE_READ_REC_HDR"); + ws_debug("pcapng_pipe_dispatch STATE_READ_REC_HDR"); #endif #ifdef _WIN32 if (pcap_src->from_cap_socket) { @@ -2593,7 +2584,7 @@ pcapng_pipe_dispatch(loop_data *ld, capture_src *pcap_src, char *errmsg, size_t case STATE_EXPECT_DATA: #ifdef LOG_CAPTURE_VERBOSE - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "pcapng_pipe_dispatch STATE_EXPECT_DATA"); + ws_debug("pcapng_pipe_dispatch STATE_EXPECT_DATA"); #endif #ifdef _WIN32 if (g_mutex_trylock(pcap_src->cap_pipe_read_mtx)) { @@ -2615,7 +2606,7 @@ pcapng_pipe_dispatch(loop_data *ld, capture_src *pcap_src, char *errmsg, size_t case STATE_READ_DATA: #ifdef LOG_CAPTURE_VERBOSE - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "pcapng_pipe_dispatch STATE_READ_DATA"); + ws_debug("pcapng_pipe_dispatch STATE_READ_DATA"); #endif #ifdef _WIN32 if (pcap_src->from_cap_socket) { @@ -2808,7 +2799,7 @@ capture_loop_open_input(capture_options *capture_opts, loop_data *ld, g_rw_lock_writer_lock (&ld->saved_shb_idb_lock); g_array_append_val(global_ld.saved_idbs, idb_source); g_rw_lock_writer_unlock (&ld->saved_shb_idb_lock); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "%s: saved capture_opts IDB %u", + ws_debug("%s: saved capture_opts IDB %u", G_STRFUNC, i); #ifdef MUST_DO_SELECT @@ -2831,7 +2822,7 @@ capture_loop_open_input(capture_options *capture_opts, loop_data *ld, #endif g_array_append_val(ld->pcaps, pcap_src); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_open_input : %s", interface_opts->name); + ws_debug("capture_loop_open_input : %s", interface_opts->name); pcap_src->pcap_h = open_capture_device(capture_opts, interface_opts, CAP_READ_TIMEOUT, &open_err, &open_err_str); @@ -2963,7 +2954,7 @@ capture_loop_open_input(capture_options *capture_opts, loop_data *ld, */ ld->pcapng_passthrough = TRUE; g_rw_lock_writer_lock (&ld->saved_shb_idb_lock); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "%s: Clearing %u interfaces for passthrough", + ws_debug("%s: Clearing %u interfaces for passthrough", G_STRFUNC, global_ld.saved_idbs->len); g_array_set_size(global_ld.saved_idbs, 0); g_rw_lock_writer_unlock (&ld->saved_shb_idb_lock); @@ -2988,7 +2979,7 @@ static void capture_loop_close_input(loop_data *ld) guint i; capture_src *pcap_src; - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_close_input"); + ws_debug("capture_loop_close_input"); for (i = 0; i < ld->pcaps->len; i++) { pcap_src = g_array_index(ld->pcaps, capture_src *, i); @@ -3017,7 +3008,7 @@ static void capture_loop_close_input(loop_data *ld) } else { /* Capture device. If open, close the pcap_t. */ if (pcap_src->pcap_h != NULL) { - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_close_input: closing %p", (void *)pcap_src->pcap_h); + ws_debug("capture_loop_close_input: closing %p", (void *)pcap_src->pcap_h); pcap_close(pcap_src->pcap_h); pcap_src->pcap_h = NULL; } @@ -3035,7 +3026,7 @@ capture_loop_init_filter(pcap_t *pcap_h, gboolean from_cap_pipe, { struct bpf_program fcode; - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_init_filter: %s", cfilter); + ws_debug("capture_loop_init_filter: %s", cfilter); /* capture filters only work on real interfaces */ if (cfilter && !from_cap_pipe) { @@ -3072,7 +3063,7 @@ capture_loop_init_pcapng_output(capture_options *capture_opts, loop_data *ld, if (ld->pcapng_passthrough && !ld->saved_shb) { /* We have a single pcapng capture interface and this is the first or only output file. */ - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "%s: skipping dumpcap SHB and IDBs in favor of source", G_STRFUNC); + ws_debug("%s: skipping dumpcap SHB and IDBs in favor of source", G_STRFUNC); g_rw_lock_reader_unlock (&ld->saved_shb_idb_lock); return TRUE; } @@ -3092,7 +3083,7 @@ capture_loop_init_pcapng_output(capture_options *capture_opts, loop_data *ld, successful = pcapng_write_block(ld->pdh, ld->saved_shb, bh.block_total_length, &ld->bytes_written, err); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "%s: wrote saved passthrough SHB %d", G_STRFUNC, successful); + ws_debug("%s: wrote saved passthrough SHB %d", G_STRFUNC, successful); } else { GString *cpu_info_str = g_string_new(""); get_cpu_info(cpu_info_str); @@ -3105,7 +3096,7 @@ capture_loop_init_pcapng_output(capture_options *capture_opts, loop_data *ld, -1, /* section_length */ &ld->bytes_written, err); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "%s: wrote dumpcap SHB %d", G_STRFUNC, successful); + ws_debug("%s: wrote dumpcap SHB %d", G_STRFUNC, successful); g_string_free(cpu_info_str, TRUE); } @@ -3138,10 +3129,10 @@ capture_loop_init_pcapng_output(capture_options *capture_opts, loop_data *ld, 0, /* IDB_IF_SPEED 8 */ 6, /* IDB_TSRESOL 9 */ &global_ld.err); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "%s: skipping deleted pcapng IDB %u", G_STRFUNC, i); + ws_debug("%s: skipping deleted pcapng IDB %u", G_STRFUNC, i); } else if (idb_source.idb && idb_source.idb_len) { successful = pcapng_write_block(global_ld.pdh, idb_source.idb, idb_source.idb_len, &ld->bytes_written, err); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "%s: wrote pcapng IDB %d", G_STRFUNC, successful); + ws_debug("%s: wrote pcapng IDB %d", G_STRFUNC, successful); } else if (idb_source.interface_id < capture_opts->ifaces->len) { unsigned if_id = idb_source.interface_id; interface_options *interface_opts = &g_array_index(capture_opts->ifaces, interface_options, if_id); @@ -3164,7 +3155,7 @@ capture_loop_init_pcapng_output(capture_options *capture_opts, loop_data *ld, 0, /* IDB_IF_SPEED 8 */ pcap_src->ts_nsec ? 9 : 6, /* IDB_TSRESOL 9 */ &global_ld.err); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "%s: wrote capture_opts IDB %d: %d", G_STRFUNC, if_id, successful); + ws_debug("%s: wrote capture_opts IDB %d: %d", G_STRFUNC, if_id, successful); } } g_rw_lock_reader_unlock (&ld->saved_shb_idb_lock); @@ -3180,7 +3171,7 @@ capture_loop_init_output(capture_options *capture_opts, loop_data *ld, char *err { int err = 0; - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_init_output"); + ws_debug("capture_loop_init_output"); if ((capture_opts->use_pcapng == FALSE) && (capture_opts->ifaces->len > 1)) { @@ -3210,7 +3201,7 @@ capture_loop_init_output(capture_options *capture_opts, loop_data *ld, char *err /* Increase the size of the IO buffer */ ld->io_buffer = (char *)g_malloc(buffsize); setvbuf(ld->pdh, ld->io_buffer, _IOFBF, buffsize); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_init_output: buffsize %zu", buffsize); + ws_debug("capture_loop_init_output: buffsize %zu", buffsize); } } if (ld->pdh) { @@ -3265,7 +3256,7 @@ capture_loop_close_output(capture_options *capture_opts, loop_data *ld, int *err guint64 end_time = create_timestamp(); gboolean success; - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_close_output"); + ws_debug("capture_loop_close_output"); if (capture_opts->multi_files_on) { return ringbuf_libpcap_dump_close(&capture_opts->save_file, err_close); @@ -3333,7 +3324,7 @@ capture_loop_dispatch(loop_data *ld, if (pcap_src->from_cap_pipe) { /* dispatch from capture pipe */ #ifdef LOG_CAPTURE_VERBOSE - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_dispatch: from capture pipe"); + ws_debug("capture_loop_dispatch: from capture pipe"); #endif #ifdef _WIN32 if (pcap_src->from_cap_socket) { @@ -3362,7 +3353,7 @@ capture_loop_dispatch(loop_data *ld, */ inpkts = pcap_src->cap_pipe_dispatch(ld, pcap_src, errmsg, errmsg_len); if (inpkts < 0) { - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "%s: src %u pipe reached EOF or err, rcv: %u drop: %u flush: %u", + ws_debug("%s: src %u pipe reached EOF or err, rcv: %u drop: %u flush: %u", G_STRFUNC, pcap_src->interface_id, pcap_src->received, pcap_src->dropped, pcap_src->flushed); g_assert(pcap_src->cap_pipe_err != PIPOK); } @@ -3386,7 +3377,7 @@ capture_loop_dispatch(loop_data *ld, * later, so it can use pcap_breakloop(). */ #ifdef LOG_CAPTURE_VERBOSE - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_dispatch: from pcap_dispatch with select"); + ws_debug("capture_loop_dispatch: from pcap_dispatch with select"); #endif if (pcap_src->pcap_fd != -1) { sel_ret = cap_pipe_select(pcap_src->pcap_fd); @@ -3427,7 +3418,7 @@ capture_loop_dispatch(loop_data *ld, /* dispatch from pcap without select */ #if 1 #ifdef LOG_CAPTURE_VERBOSE - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_dispatch: from pcap_dispatch"); + ws_debug("capture_loop_dispatch: from pcap_dispatch"); #endif #ifdef _WIN32 /* @@ -3457,7 +3448,7 @@ capture_loop_dispatch(loop_data *ld, } #else /* pcap_next_ex */ #ifdef LOG_CAPTURE_VERBOSE - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_dispatch: from pcap_next_ex"); + ws_debug("capture_loop_dispatch: from pcap_next_ex"); #endif /* XXX - this is currently unused, as there is some confusion with pcap_next_ex() vs. pcap_dispatch() */ @@ -3496,7 +3487,7 @@ capture_loop_dispatch(loop_data *ld, } #ifdef LOG_CAPTURE_VERBOSE - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_dispatch: %d new packet%s", inpkts, plurality(inpkts, "", "s")); + ws_debug("capture_loop_dispatch: %d new packet%s", inpkts, plurality(inpkts, "", "s")); #endif return ld->packets_captured - packet_count_before; @@ -3538,7 +3529,7 @@ capture_loop_open_output(capture_options *capture_opts, int *save_file_fd, gboolean is_tempfile; GError *err_tempfile = NULL; - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_open_output: %s", + ws_debug("capture_loop_open_output: %s", (capture_opts->save_file) ? capture_opts->save_file : "(not specified)"); if (capture_opts->save_file != NULL) { @@ -3790,8 +3781,7 @@ pcap_read_handler(void* arg) capture_src *pcap_src = (capture_src *)arg; char errmsg[MSG_MAX_LENGTH+1]; - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Started thread for interface %d.", - pcap_src->interface_id); + ws_info("Started thread for interface %d.", pcap_src->interface_id); /* If this is a pipe input it might finish early. */ while (global_ld.go && pcap_src->cap_pipe_err == PIPOK) { @@ -3799,8 +3789,7 @@ pcap_read_handler(void* arg) capture_loop_dispatch(&global_ld, errmsg, sizeof(errmsg), pcap_src); } - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Stopped thread for interface %d.", - pcap_src->interface_id); + ws_info("Stopped thread for interface %d.", pcap_src->interface_id); g_thread_exit(NULL); return (NULL); } @@ -3823,8 +3812,7 @@ capture_loop_dequeue_packet(void) { g_async_queue_unlock(pcap_queue); if (queue_element) { if (queue_element->pcap_src->from_pcapng) { - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, - "Dequeued a block of type 0x%08x of length %d captured on interface %d.", + ws_info("Dequeued a block of type 0x%08x of length %d captured on interface %d.", queue_element->u.bh.block_type, queue_element->u.bh.block_total_length, queue_element->pcap_src->interface_id); @@ -3832,8 +3820,7 @@ capture_loop_dequeue_packet(void) { &queue_element->u.bh, queue_element->pd); } else { - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, - "Dequeued a packet of length %d captured on interface %d.", + ws_info("Dequeued a packet of length %d captured on interface %d.", queue_element->u.phdr.caplen, queue_element->pcap_src->interface_id); capture_loop_write_packet_cb((u_char *) queue_element->pcap_src, @@ -3935,8 +3922,8 @@ capture_loop_start(capture_options *capture_opts, gboolean *stats_known, struct /* We haven't yet gotten the capture statistics. */ *stats_known = FALSE; - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Capture loop starting ..."); - capture_opts_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, capture_opts); + ws_info("Capture loop starting ..."); + capture_opts_log(LOG_DOMAIN_CAPCHILD, LOG_LEVEL_DEBUG, capture_opts); /* open the "input file" from network interface or capture pipe */ if (!capture_loop_open_input(capture_opts, &global_ld, errmsg, sizeof(errmsg), @@ -4031,8 +4018,8 @@ capture_loop_start(capture_options *capture_opts, gboolean *stats_known, struct gettimeofday(&upd_time, NULL); #endif start_time = create_timestamp(); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Capture loop running."); - capture_opts_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, capture_opts); + ws_info("Capture loop running."); + capture_opts_log(LOG_DOMAIN_CAPCHILD, LOG_LEVEL_DEBUG, capture_opts); /* WOW, everything is prepared! */ /* please fasten your seat belts, we will enter now the actual capture loop */ @@ -4158,16 +4145,14 @@ capture_loop_start(capture_options *capture_opts, gboolean *stats_known, struct } } - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Capture loop stopping ..."); + ws_info("Capture loop stopping ..."); if (use_threads) { for (i = 0; i < global_ld.pcaps->len; i++) { pcap_src = g_array_index(global_ld.pcaps, capture_src *, i); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Waiting for thread of interface %u...", - pcap_src->interface_id); + ws_info("Waiting for thread of interface %u...", pcap_src->interface_id); g_thread_join(pcap_src->tid); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Thread of interface %u terminated.", - pcap_src->interface_id); + ws_info("Thread of interface %u terminated.", pcap_src->interface_id); } while (1) { gboolean dequeued = capture_loop_dequeue_packet(); @@ -4374,7 +4359,7 @@ capture_loop_start(capture_options *capture_opts, gboolean *stats_known, struct /* close the input file (pcap or capture pipe) */ capture_loop_close_input(&global_ld); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Capture loop stopped."); + ws_info("Capture loop stopped."); /* ok, if the write and the close were successful. */ return write_ok && close_ok; @@ -4404,7 +4389,7 @@ error: /* close the input file (pcap or cap_pipe) */ capture_loop_close_input(&global_ld); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Capture loop stopped with error"); + ws_info("Capture loop stopped with error"); return FALSE; } @@ -4531,7 +4516,7 @@ capture_loop_write_pcapng_cb(capture_src *pcap_src, const pcapng_block_header_t } if (!pcapng_adjust_block(pcap_src, bh, pd)) { - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "%s failed to adjust pcapng block.", G_STRFUNC); + ws_info("%s failed to adjust pcapng block.", G_STRFUNC); g_assert_not_reached(); return; } @@ -4564,8 +4549,7 @@ capture_loop_write_pcapng_cb(capture_src *pcap_src, const pcapng_block_header_t } else if (bh->block_type == BLOCK_TYPE_EPB || bh->block_type == BLOCK_TYPE_SPB || bh->block_type == BLOCK_TYPE_SYSTEMD_JOURNAL) { /* count packet only if we actually have an EPB or SPB */ #if defined(DEBUG_DUMPCAP) || defined(DEBUG_CHILD_DUMPCAP) - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, - "Wrote a pcapng block type %u of length %d captured on interface %u.", + ws_info("Wrote a pcapng block type %u of length %d captured on interface %u.", bh->block_type, bh->block_total_length, pcap_src->interface_id); #endif capture_loop_wrote_one_packet(pcap_src); @@ -4582,7 +4566,7 @@ capture_loop_write_packet_cb(u_char *pcap_src_p, const struct pcap_pkthdr *phdr, int err; guint ts_mul = pcap_src->ts_nsec ? 1000000000 : 1000000; - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_write_packet_cb"); + ws_debug("capture_loop_write_packet_cb"); /* We may be called multiple times from pcap_dispatch(); if we've set the "stop capturing" flag, ignore this packet, as we're not @@ -4620,8 +4604,7 @@ capture_loop_write_packet_cb(u_char *pcap_src_p, const struct pcap_pkthdr *phdr, pcap_src->dropped++; } else { #if defined(DEBUG_DUMPCAP) || defined(DEBUG_CHILD_DUMPCAP) - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, - "Wrote a pcap packet of length %d captured on interface %u.", + ws_info("Wrote a pcap packet of length %d captured on interface %u.", phdr->caplen, pcap_src->interface_id); #endif capture_loop_wrote_one_packet(pcap_src); @@ -4675,19 +4658,16 @@ capture_loop_queue_packet_cb(u_char *pcap_src_p, const struct pcap_pkthdr *phdr, pcap_src->dropped++; g_free(queue_element->pd); g_free(queue_element); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, - "Dropped a packet of length %d captured on interface %u.", + ws_info("Dropped a packet of length %d captured on interface %u.", phdr->caplen, pcap_src->interface_id); } else { pcap_src->received++; - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, - "Queued a packet of length %d captured on interface %u.", + ws_info("Queued a packet of length %d captured on interface %u.", phdr->caplen, pcap_src->interface_id); } /* I don't want to hold the mutex over the debug output. So the output may be wrong */ - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, - "Queue size is now %" G_GINT64_MODIFIER "d bytes (%" G_GINT64_MODIFIER "d packets)", + ws_info("Queue size is now %" G_GINT64_MODIFIER "d bytes (%" G_GINT64_MODIFIER "d packets)", pcap_queue_bytes, pcap_queue_packets); } @@ -4735,19 +4715,16 @@ capture_loop_queue_pcapng_cb(capture_src *pcap_src, const pcapng_block_header_t pcap_src->dropped++; g_free(queue_element->pd); g_free(queue_element); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, - "Dropped a packet of length %d captured on interface %u.", + ws_info("Dropped a packet of length %d captured on interface %u.", bh->block_total_length, pcap_src->interface_id); } else { pcap_src->received++; - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, - "Queued a block of type 0x%08x of length %d captured on interface %u.", + ws_info("Queued a block of type 0x%08x of length %d captured on interface %u.", bh->block_type, bh->block_total_length, pcap_src->interface_id); } /* I don't want to hold the mutex over the debug output. So the output may be wrong */ - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, - "Queue size is now %" G_GINT64_MODIFIER "d bytes (%" G_GINT64_MODIFIER "d packets)", + ws_info("Queue size is now %" G_GINT64_MODIFIER "d bytes (%" G_GINT64_MODIFIER "d packets)", pcap_queue_bytes, pcap_queue_packets); } @@ -4852,7 +4829,6 @@ main(int argc, char *argv[]) gboolean stats_known; struct pcap_stat stats = {0}; - GLogLevelFlags log_flags; gboolean list_interfaces = FALSE; int caps_queries = 0; gboolean print_bpf_code = FALSE; @@ -4868,8 +4844,19 @@ main(int argc, char *argv[]) #endif GString *str; + /* Initialize log handler early so we can have proper logging during startup. */ + ws_log_init(dumpcap_log_writer); + cmdarg_err_init(dumpcap_cmdarg_err, dumpcap_cmdarg_err_cont); + /* Command line options are parsed too late to configure logging, do it + manually. */ + const char *opt_err_val; + if ((opt_err_val = ws_log_set_level_args(&argc, argv)) != NULL) { + cmdarg_err("Invalid log level \"%s\"", opt_err_val); + exit (1); + } + #ifdef _WIN32 create_app_running_mutex(); @@ -4969,37 +4956,6 @@ main(int argc, char *argv[]) } } - /* The default_log_handler will use stdout, which makes trouble in */ - /* capture child mode, as it uses stdout for its sync_pipe. */ - /* So: the filtering is done in the console_log_handler and not here.*/ - /* We set the log handlers right up front to make sure that any log */ - /* messages when running as child will be sent back to the parent */ - /* with the correct format. */ - - log_flags = - (GLogLevelFlags)( - G_LOG_LEVEL_ERROR| - G_LOG_LEVEL_CRITICAL| - G_LOG_LEVEL_WARNING| - G_LOG_LEVEL_MESSAGE| - G_LOG_LEVEL_INFO| - G_LOG_LEVEL_DEBUG| - G_LOG_FLAG_FATAL| - G_LOG_FLAG_RECURSION); - - g_log_set_handler(NULL, - log_flags, - console_log_handler, NULL /* user_data */); - g_log_set_handler(LOG_DOMAIN_MAIN, - log_flags, - console_log_handler, NULL /* user_data */); - g_log_set_handler(LOG_DOMAIN_CAPTURE, - log_flags, - console_log_handler, NULL /* user_data */); - g_log_set_handler(LOG_DOMAIN_CAPTURE_CHILD, - log_flags, - console_log_handler, NULL /* user_data */); - /* Initialize the pcaps list and IDBs */ global_ld.pcaps = g_array_new(FALSE, FALSE, sizeof(capture_src *)); global_ld.pcapng_passthrough = FALSE; @@ -5009,10 +4965,10 @@ main(int argc, char *argv[]) err_msg = ws_init_sockets(); if (err_msg != NULL) { - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_ERROR, + ws_log(LOG_DOMAIN_CAPCHILD, LOG_LEVEL_ERROR, "ERROR: %s", err_msg); g_free(err_msg); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_ERROR, + ws_log(LOG_DOMAIN_CAPCHILD, LOG_LEVEL_ERROR, "%s", please_report_bug()); exit_main(1); } @@ -5237,8 +5193,7 @@ main(int argc, char *argv[]) GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL); if (sig_pipe_handle == INVALID_HANDLE_VALUE) { - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, - "Signal pipe: Unable to open %s. Dead parent?", + ws_info("Signal pipe: Unable to open %s. Dead parent?", sig_pipe_name); exit_main(1); } @@ -5535,8 +5490,7 @@ main(int argc, char *argv[]) interface_options *interface_opts; interface_opts = &g_array_index(global_capture_opts.ifaces, interface_options, j); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Interface: %s\n", - interface_opts->name); + ws_debug("Interface: %s\n", interface_opts->name); } } else { str = g_string_new(""); @@ -5613,92 +5567,29 @@ main(int argc, char *argv[]) } static void -console_log_handler(const char *log_domain, GLogLevelFlags log_level, - const char *message, gpointer user_data _U_) +dumpcap_log_writer(const char *message, enum ws_log_domain domain _U_, + enum ws_log_level level _U_, void *ptr _U_) { - time_t curr; - struct tm *today; - const char *level; - gchar *msg; - - /* ignore log message, if log_level isn't interesting */ - if ( !(log_level & G_LOG_LEVEL_MASK & ~(G_LOG_LEVEL_DEBUG|G_LOG_LEVEL_INFO))) { -#if !defined(DEBUG_DUMPCAP) && !defined(DEBUG_CHILD_DUMPCAP) - return; -#endif - } - - switch(log_level & G_LOG_LEVEL_MASK) { - case G_LOG_LEVEL_ERROR: - level = "Err "; - break; - case G_LOG_LEVEL_CRITICAL: - level = "Crit"; - break; - case G_LOG_LEVEL_WARNING: - level = "Warn"; - break; - case G_LOG_LEVEL_MESSAGE: - level = "Msg "; - break; - case G_LOG_LEVEL_INFO: - level = "Info"; - break; - case G_LOG_LEVEL_DEBUG: - level = "Dbg "; - break; - default: - fprintf(stderr, "unknown log_level %d\n", log_level); - level = NULL; - g_assert_not_reached(); - } - - /* Generate the output message */ - if (log_level & G_LOG_LEVEL_MESSAGE) { - /* normal user messages without additional infos */ - msg = g_strdup_printf("%s\n", message); - } else { - /* create a "timestamp" */ - time(&curr); - today = localtime(&curr); - - /* info/debug messages with additional infos */ - if (today != NULL) - msg = g_strdup_printf("%02u:%02u:%02u %8s %s %s\n", - today->tm_hour, today->tm_min, today->tm_sec, - log_domain != NULL ? log_domain : "", - level, message); - else - msg = g_strdup_printf("Time not representable %8s %s %s\n", - log_domain != NULL ? log_domain : "", - level, message); - } - - /* DEBUG & INFO msgs (if we're debugging today) */ #if defined(DEBUG_DUMPCAP) || defined(DEBUG_CHILD_DUMPCAP) - if ( !(log_level & G_LOG_LEVEL_MASK & ~(G_LOG_LEVEL_DEBUG|G_LOG_LEVEL_INFO))) { #ifdef DEBUG_DUMPCAP - fprintf(stderr, "%s", msg); + fprintf(stderr, "%s\n", message); fflush(stderr); #endif #ifdef DEBUG_CHILD_DUMPCAP - fprintf(debug_log, "%s", msg); + fprintf(debug_log, "%s\n", message); fflush(debug_log); #endif - g_free(msg); - return; - } + return; #endif - /* ERROR, CRITICAL, WARNING, MESSAGE messages goto stderr or */ + /* Messages goto stderr or */ /* to parent especially formatted if dumpcap running as child. */ if (capture_child) { - sync_pipe_errmsg_to_parent(2, msg, ""); + sync_pipe_errmsg_to_parent(2, message, ""); } else { - fprintf(stderr, "%s", msg); + fprintf(stderr, "%s", message); fflush(stderr); } - g_free(msg); } @@ -5714,7 +5605,7 @@ report_packet_count(unsigned int packet_count) if (capture_child) { g_snprintf(count_str, sizeof(count_str), "%u", packet_count); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Packets: %s", count_str); + ws_debug("Packets: %s", count_str); pipe_write_block(2, SP_PACKET_COUNT, count_str); } else { count += packet_count; @@ -5728,7 +5619,7 @@ static void report_new_capture_file(const char *filename) { if (capture_child) { - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "File: %s", filename); + ws_debug("File: %s", filename); pipe_write_block(2, SP_FILE, filename); } else { #ifdef SIGINFO @@ -5767,7 +5658,7 @@ report_cfilter_error(capture_options *capture_opts, guint i, const char *errmsg) if (i < capture_opts->ifaces->len) { if (capture_child) { g_snprintf(tmp, sizeof(tmp), "%u:%s", i, errmsg); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Capture filter error: %s", errmsg); + ws_debug("Capture filter error: %s", errmsg); pipe_write_block(2, SP_BAD_FILTER, tmp); } else { /* @@ -5789,10 +5680,8 @@ static void report_capture_error(const char *error_msg, const char *secondary_error_msg) { if (capture_child) { - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, - "Primary Error: %s", error_msg); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, - "Secondary Error: %s", secondary_error_msg); + ws_debug("Primary Error: %s", error_msg); + ws_debug("Secondary Error: %s", secondary_error_msg); sync_pipe_errmsg_to_parent(2, error_msg, secondary_error_msg); } else { cmdarg_err("%s", error_msg); @@ -5809,8 +5698,7 @@ report_packet_drops(guint32 received, guint32 pcap_drops, guint32 drops, guint32 if (capture_child) { char* tmp = g_strdup_printf("%u:%s", total_drops, name); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, - "Packets received/dropped on interface '%s': %u/%u (pcap:%u/dumpcap:%u/flushed:%u/ps_ifdrop:%u)", + ws_debug("Packets received/dropped on interface '%s': %u/%u (pcap:%u/dumpcap:%u/flushed:%u/ps_ifdrop:%u)", name, received, total_drops, pcap_drops, drops, flushed, ps_ifdrop); pipe_write_block(2, SP_DROPS, tmp); g_free(tmp); @@ -5844,8 +5732,7 @@ signal_pipe_check_running(void) if (!sig_pipe_name || !sig_pipe_handle) { /* This shouldn't happen */ - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, - "Signal pipe: No name or handle"); + ws_info("Signal pipe: No name or handle"); return FALSE; } @@ -5860,10 +5747,8 @@ signal_pipe_check_running(void) if (!result || avail > 0) { /* peek failed or some bytes really available */ /* (if not piping from stdin this would fail) */ - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, - "Signal pipe: Stop capture: %s", sig_pipe_name); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, - "Signal pipe: %s (%p) result: %u avail: %lu", sig_pipe_name, + ws_info("Signal pipe: Stop capture: %s", sig_pipe_name); + ws_debug("Signal pipe: %s (%p) result: %u avail: %lu", sig_pipe_name, sig_pipe_handle, result, avail); return FALSE; } else { diff --git a/epan/dissectors/packet-frame.c b/epan/dissectors/packet-frame.c index 020c32a410..e724ce1f94 100644 --- a/epan/dissectors/packet-frame.c +++ b/epan/dissectors/packet-frame.c @@ -29,13 +29,13 @@ #include #include #include +#include #include #include #include #include "packet-frame.h" #include "packet-icmp.h" -#include "log.h" #include #include @@ -1012,7 +1012,7 @@ dissect_frame(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, void* field_info* fi = proto_find_field_from_offset(tree, i, tvb); if (fi && fi->hfinfo->id != proto_frame) { if (prefs.incomplete_dissectors_check_debug) - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_WARNING, + ws_log(LOG_DOMAIN_CAPTURE, LOG_LEVEL_WARNING, "Dissector %s incomplete in frame %u: undecoded byte number %u " "(0x%.4X+%u)", fi->hfinfo->abbrev, diff --git a/epan/epan.c b/epan/epan.c index aaa404ba9f..461276b0c9 100644 --- a/epan/epan.c +++ b/epan/epan.c @@ -31,6 +31,7 @@ #include "epan_dissect.h" #include +#include #include "conversation.h" #include "except.h" @@ -137,26 +138,28 @@ epan_get_version_number(int *major, int *minor, int *micro) static void quiet_gcrypt_logger (void *dummy _U_, int level, const char *format, va_list args) { - GLogLevelFlags log_level = G_LOG_LEVEL_WARNING; + enum ws_log_level log_level; switch (level) { case GCRY_LOG_CONT: // Continuation. Ignore for now. case GCRY_LOG_DEBUG: case GCRY_LOG_INFO: - default: return; + break; case GCRY_LOG_WARN: case GCRY_LOG_BUG: - log_level = G_LOG_LEVEL_WARNING; + log_level = LOG_LEVEL_WARNING; break; case GCRY_LOG_ERROR: - log_level = G_LOG_LEVEL_ERROR; + log_level = LOG_LEVEL_ERROR; break; case GCRY_LOG_FATAL: - log_level = G_LOG_LEVEL_CRITICAL; + log_level = LOG_LEVEL_CRITICAL; break; + default: + return; } - g_logv(NULL, log_level, format, args); + ws_logv(LOG_DOMAIN_EPAN, log_level, format, args); } #endif // _WIN32 diff --git a/epan/prefs.c b/epan/prefs.c index 9af1984267..9707f27da4 100644 --- a/epan/prefs.c +++ b/epan/prefs.c @@ -2424,73 +2424,6 @@ gui_layout_callback(void) ******************************************************/ static void custom_pref_no_cb(pref_t* pref _U_) {} - -/* - * Console log level custom preference functions - */ -static void -console_log_level_reset_cb(pref_t* pref) -{ - *pref->varp.uint = pref->default_val.uint; -} - -static prefs_set_pref_e -console_log_level_set_cb(pref_t* pref, const gchar* value, unsigned int* changed_flags) -{ - guint uval; - - if (!ws_strtou32(value, NULL, &uval)) - return PREFS_SET_SYNTAX_ERR; /* number was bad */ - - if (*pref->varp.uint != uval) { - *changed_flags = prefs_get_effect_flags(pref); - *pref->varp.uint = uval; - } - - if (*pref->varp.uint & (G_LOG_LEVEL_INFO|G_LOG_LEVEL_DEBUG)) { - /* - * GLib drops INFO and DEBUG messages by default. If the user - * hasn't set G_MESSAGES_DEBUG, possibly to a specific set of - * domains, tell it not to do that. - */ - const char *s = g_getenv("G_MESSAGES_DEBUG"); - if(s != NULL) { - g_message("prefs: Skip overwriting environment variable " - "G_MESSAGES_DEBUG=\"%s\"", s); - } - else { - g_info("prefs: Set environment variable G_MESSAGES_DEBUG=\"all\""); - g_setenv("G_MESSAGES_DEBUG", "all", FALSE); - } - } - - return PREFS_SET_OK; -} - -static const char * console_log_level_type_name_cb(void) { - return "Log level"; -} - -static char * console_log_level_type_description_cb(void) { - return g_strdup_printf( - "Console log level (for debugging)\n" - "A bitmask of log levels:\n" - "ERROR = 4\n" - "CRITICAL = 8\n" - "WARNING = 16\n" - "MESSAGE = 32\n" - "INFO = 64\n" - "DEBUG = 128"); -} - -static gboolean console_log_level_is_default_cb(pref_t* pref) { - return *pref->varp.uint == pref->default_val.uint; -} - -static char * console_log_level_to_str_cb(pref_t* pref, gboolean default_val) { - return g_strdup_printf("%u", default_val ? pref->default_val.uint : *pref->varp.uint); -} - /* * Column preference functions */ @@ -3547,15 +3480,7 @@ prefs_register_modules(void) console_module = prefs_register_module(NULL, "console", "Console", "Console logging and debugging output", NULL, FALSE); - custom_cbs.free_cb = custom_pref_no_cb; - custom_cbs.reset_cb = console_log_level_reset_cb; - custom_cbs.set_cb = console_log_level_set_cb; - custom_cbs.type_name_cb = console_log_level_type_name_cb; - custom_cbs.type_description_cb = console_log_level_type_description_cb; - custom_cbs.is_default_cb = console_log_level_is_default_cb; - custom_cbs.to_str_cb = console_log_level_to_str_cb; - prefs_register_uint_custom_preference(console_module, "log.level", "logging level", - "A bitmask of GLib log levels", &custom_cbs, &prefs.console_log_level); + prefs_register_obsolete_preference(console_module, "log.level"); prefs_register_bool_preference(console_module, "incomplete_dissectors_check_debug", "Print debug line for incomplete dissectors", @@ -4271,9 +4196,6 @@ pre_init_prefs(void) } } - prefs.console_log_level = - G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_ERROR; - /* set the default values for the tap/statistics dialog box */ prefs.tap_update_interval = TAP_UPDATE_DEFAULT_INTERVAL; prefs.st_enable_burstinfo = TRUE; diff --git a/epan/prefs.h b/epan/prefs.h index 91733c65da..5866e0a728 100644 --- a/epan/prefs.h +++ b/epan/prefs.h @@ -185,7 +185,6 @@ typedef struct _e_prefs { gchar *gui_interfaces_hide_types; gboolean gui_interfaces_show_hidden; gboolean gui_interfaces_remote_display; - gint console_log_level; gchar *capture_device; gchar *capture_devices_linktypes; gchar *capture_devices_descr; diff --git a/epan/reedsolomon.c b/epan/reedsolomon.c index 0250c00495..5c810aa154 100644 --- a/epan/reedsolomon.c +++ b/epan/reedsolomon.c @@ -19,7 +19,7 @@ #include "config.h" -#define G_LOG_DOMAIN "epan" +#define WS_LOG_DOMAIN LOG_DOMAIN_EPAN #include #include "reedsolomon.h" diff --git a/epan/secrets.c b/epan/secrets.c index 13717d7761..8d61ad42df 100644 --- a/epan/secrets.c +++ b/epan/secrets.c @@ -11,8 +11,7 @@ #include "config.h" -/* Start with G_MESSAGES_DEBUG=secrets to see messages. */ -#define G_LOG_DOMAIN "secrets" +#define WS_LOG_DOMAIN LOG_DOMAIN_EPAN #include "secrets.h" #include diff --git a/epan/tvbuff_zlib.c b/epan/tvbuff_zlib.c index befd05c7ce..2264d5dc1d 100644 --- a/epan/tvbuff_zlib.c +++ b/epan/tvbuff_zlib.c @@ -9,9 +9,8 @@ * SPDX-License-Identifier: GPL-2.0-or-later */ -#define G_LOG_DOMAIN "epan" - #include +#define WS_LOG_DOMAIN LOG_DOMAIN_EPAN #include diff --git a/extcap.c b/extcap.c index 70ebed0374..bdbf234c06 100644 --- a/extcap.c +++ b/extcap.c @@ -11,6 +11,7 @@ */ #include +#define WS_LOG_DOMAIN LOG_DOMAIN_CAPCHILD #include #include @@ -31,7 +32,6 @@ #endif #include -#include #include @@ -41,6 +41,7 @@ #include #include #include +#include #include "capture_opts.h" @@ -498,7 +499,7 @@ extcap_run_all(const char *argv[], extcap_run_cb_t output_cb, gsize data_size, g g_cond_clear(&pool.cond); g_thread_pool_free(pool.pool, FALSE, TRUE); - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "extcap: completed discovery of %d tools in %.3fms", + ws_debug("extcap: completed discovery of %d tools in %.3fms", paths_count, (g_get_monotonic_time() - start_time) / 1000.0); *count = paths_count; return infos; @@ -534,7 +535,7 @@ static gboolean cb_dlt(extcap_callback_info_t cb_info) dlts = extcap_parse_dlts(cb_info.output); temp = dlts; - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap pipe %s ", cb_info.extcap); + ws_debug("Extcap pipe %s ", cb_info.extcap); /* * Allocate the interface capabilities structure. @@ -548,8 +549,7 @@ static gboolean cb_dlt(extcap_callback_info_t cb_info) dlt_item = (extcap_dlt *)dlts->data; if (dlt_item) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, - " DLT %d name=\"%s\" display=\"%s\" ", dlt_item->number, + ws_debug(" DLT %d name=\"%s\" display=\"%s\" ", dlt_item->number, dlt_item->name, dlt_item->display); data_link_info = g_new(data_link_info_t, 1); @@ -572,7 +572,7 @@ static gboolean cb_dlt(extcap_callback_info_t cb_info) { if (cb_info.err_str) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, " returned no DLTs"); + ws_debug(" returned no DLTs"); *(cb_info.err_str) = g_strdup("Extcap returned no DLTs"); } g_free(caps); @@ -930,8 +930,7 @@ extcap_get_if_configuration(const char *ifname) extcap_interface *interface = extcap_find_interface_for_ifname(ifname); if (interface) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap path %s", - get_extcap_dir()); + ws_debug("Extcap path %s", get_extcap_dir()); arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_CONFIG)); arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_INTERFACE)); @@ -976,8 +975,7 @@ extcap_get_if_configuration_values(const char * ifname, const char * argname, GH extcap_interface *interface = extcap_find_interface_for_ifname(ifname); if (interface) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap path %s", - get_extcap_dir()); + ws_debug("Extcap path %s", get_extcap_dir()); args = g_list_append(args, g_strdup(EXTCAP_ARGUMENT_CONFIG)); args = g_list_append(args, g_strdup(EXTCAP_ARGUMENT_INTERFACE)); @@ -1113,8 +1111,7 @@ extcap_verify_capture_filter(const char *ifname, const char *filter, gchar **err extcap_interface *interface = extcap_find_interface_for_ifname(ifname); if (interface) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap path %s", - get_extcap_dir()); + ws_debug("Extcap path %s", get_extcap_dir()); arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_CAPTURE_FILTER)); arguments = g_list_append(arguments, g_strdup(filter)); @@ -1175,14 +1172,12 @@ void extcap_if_cleanup(capture_options *capture_opts, gchar **errormsg) overwrite_exitcode = FALSE; - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, - "Extcap [%s] - Cleaning up fifo: %s; PID: %d", interface_opts->name, + ws_debug("Extcap [%s] - Cleaning up fifo: %s; PID: %d", interface_opts->name, interface_opts->extcap_fifo, interface_opts->extcap_pid); #ifdef _WIN32 if (interface_opts->extcap_pipe_h != INVALID_HANDLE_VALUE) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, - "Extcap [%s] - Closing pipe", interface_opts->name); + ws_debug("Extcap [%s] - Closing pipe", interface_opts->name); FlushFileBuffers(interface_opts->extcap_pipe_h); DisconnectNamedPipe(interface_opts->extcap_pipe_h); CloseHandle(interface_opts->extcap_pipe_h); @@ -1190,8 +1185,7 @@ void extcap_if_cleanup(capture_options *capture_opts, gchar **errormsg) } if (interface_opts->extcap_control_in_h != INVALID_HANDLE_VALUE) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, - "Extcap [%s] - Closing control_in pipe", interface_opts->name); + ws_debug("Extcap [%s] - Closing control_in pipe", interface_opts->name); FlushFileBuffers(interface_opts->extcap_control_in_h); DisconnectNamedPipe(interface_opts->extcap_control_in_h); CloseHandle(interface_opts->extcap_control_in_h); @@ -1199,8 +1193,7 @@ void extcap_if_cleanup(capture_options *capture_opts, gchar **errormsg) } if (interface_opts->extcap_control_out_h != INVALID_HANDLE_VALUE) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, - "Extcap [%s] - Closing control_out pipe", interface_opts->name); + ws_debug("Extcap [%s] - Closing control_out pipe", interface_opts->name); FlushFileBuffers(interface_opts->extcap_control_out_h); DisconnectNamedPipe(interface_opts->extcap_control_out_h); CloseHandle(interface_opts->extcap_control_out_h); @@ -1233,8 +1226,7 @@ void extcap_if_cleanup(capture_options *capture_opts, gchar **errormsg) #endif /* Maybe the client closed and removed fifo, but ws should check if * pid should be closed */ - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, - "Extcap [%s] - Closing spawned PID: %d", interface_opts->name, + ws_debug("Extcap [%s] - Closing spawned PID: %d", interface_opts->name, interface_opts->extcap_pid); pipedata = (ws_pipe_t *) interface_opts->extcap_pipedata; @@ -1535,13 +1527,13 @@ static gboolean extcap_create_pipe(const gchar *ifname, gchar **fifo, HANDLE *ha if (*handle_out == INVALID_HANDLE_VALUE) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "\nError creating pipe => (%d)", GetLastError()); + ws_debug("Error creating pipe => (%d)", GetLastError()); g_free (pipename); return FALSE; } else { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "\nWireshark Created pipe =>(%s) handle (%" G_GUINTPTR_FORMAT ")", pipename, *handle_out); + ws_debug("Wireshark Created pipe =>(%s) handle (%" G_GUINTPTR_FORMAT ")", pipename, *handle_out); *fifo = g_strdup(pipename); } @@ -1563,8 +1555,7 @@ static gboolean extcap_create_pipe(const gchar *ifname, gchar **fifo, const gcha ws_close(fd); - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, - "Extcap - Creating fifo: %s", temp_name); + ws_debug("Extcap - Creating fifo: %s", temp_name); if (file_exists(temp_name)) { @@ -1780,12 +1771,12 @@ process_new_extcap(const char *extcap, char *output) /* Load interfaces from utility */ interfaces = extcap_parse_interfaces(output, &control_items); - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Loading interface list for %s ", extcap); + ws_debug("Loading interface list for %s ", extcap); /* Seems, that there where no interfaces to be loaded */ if ( ! interfaces || g_list_length(interfaces) == 0 ) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Cannot load interfaces for %s", extcap ); + ws_debug("Cannot load interfaces for %s", extcap ); g_list_free(interface_keys); g_free(toolname); return; @@ -1795,8 +1786,7 @@ process_new_extcap(const char *extcap, char *output) element = extcap_ensure_interface(toolname, TRUE); if ( element == NULL ) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_WARNING, - "Cannot store interface %s, already loaded as personal plugin", extcap ); + ws_warning("Cannot store interface %s, already loaded as personal plugin", extcap ); g_list_foreach(interfaces, remove_extcap_entry, NULL); g_list_free(interfaces); g_list_free(interface_keys); @@ -1817,7 +1807,7 @@ process_new_extcap(const char *extcap, char *output) int_iter = (extcap_interface *)walker->data; if (int_iter->call != NULL) - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Interface found %s\n", int_iter->call); + ws_debug("Interface found %s\n", int_iter->call); /* Help is not necessarily stored with the interface, but rather with the version string. * As the version string allways comes in front of the interfaces, this ensures, that it get's @@ -1825,7 +1815,7 @@ process_new_extcap(const char *extcap, char *output) if (int_iter->if_type == EXTCAP_SENTENCE_EXTCAP) { if (int_iter->call != NULL) - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, " Extcap [%s] ", int_iter->call); + ws_debug(" Extcap [%s] ", int_iter->call); /* Only initialize values if none are set. Need to check only one element here */ if ( ! element->version ) @@ -1854,14 +1844,14 @@ process_new_extcap(const char *extcap, char *output) { if ( g_list_find(interface_keys, int_iter->call) ) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_WARNING, "Extcap interface \"%s\" is already provided by \"%s\" ", + ws_warning("Extcap interface \"%s\" is already provided by \"%s\" ", int_iter->call, extcap_if_executable(int_iter->call)); walker = g_list_next(walker); continue; } if ((int_iter->call != NULL) && (int_iter->display)) - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, " Interface [%s] \"%s\" ", int_iter->call, int_iter->display); + ws_debug(" Interface [%s] \"%s\" ", int_iter->call, int_iter->display); int_iter->extcap_path = g_strdup(extcap); diff --git a/extcap/androiddump.c b/extcap/androiddump.c index 6011345385..1f0f21eab4 100644 --- a/extcap/androiddump.c +++ b/extcap/androiddump.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -362,7 +363,7 @@ static const char* interface_to_logbuf(char* interface) static void androiddump_cmdarg_err(const char *msg_format, va_list ap) { - g_logv(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, msg_format, ap); + ws_logv(LOG_DOMAIN_CAPCHILD, LOG_LEVEL_WARNING, msg_format, ap); } static void useSndTimeout(socket_handle_t sock) { @@ -2527,8 +2528,19 @@ int main(int argc, char *argv[]) { char *help_url; char *help_header = NULL; + /* Initialize log handler early so we can have proper logging during startup. */ + ws_log_init(NULL); + cmdarg_err_init(androiddump_cmdarg_err, androiddump_cmdarg_err); + /* Command line options are parsed too late to configure logging, do it + manually. */ + const char *opt_err_val; + if ((opt_err_val = ws_log_set_level_args(&argc, argv)) != NULL) { + cmdarg_err("Invalid log level \"%s\"", opt_err_val); + return EXIT_FAILURE; + } + /* * Get credential information for later use. */ diff --git a/extcap/extcap-base.c b/extcap/extcap-base.c index bc09aa6c54..0f8a5e6a51 100644 --- a/extcap/extcap-base.c +++ b/extcap/extcap-base.c @@ -18,6 +18,7 @@ #include #include #include +#include /* * If we have getopt_long() in the system library, include . @@ -53,8 +54,6 @@ typedef struct _extcap_option { char * optdesc; } extcap_option_t; -FILE* custom_log = NULL; - void extcap_base_register_interface(extcap_parameters * extcap, const char * interface, const char * ifdescription, uint16_t dlt, const char * dltdescription ) { extcap_base_register_interface_ext(extcap, interface, ifdescription, dlt, NULL, dltdescription ); @@ -116,37 +115,16 @@ void extcap_base_set_running_with(extcap_parameters * extcap, const char *fmt, . va_end(ap); } -static void extcap_custom_log(const gchar *log_domain, - GLogLevelFlags log_level, - const gchar *message, - gpointer user_data) -{ - if (log_level & G_LOG_LEVEL_DEBUG) { - if (!custom_log) - return; - fprintf(custom_log, "%s\n", message); - fflush(custom_log); - } else { - g_log_default_handler(log_domain, log_level, message, user_data); - } -} - uint8_t extcap_base_parse_options(extcap_parameters * extcap, int result, char * optargument) { uint8_t ret = 1; switch (result) { case EXTCAP_OPT_DEBUG: -#ifdef _WIN32 - _putenv_s("G_MESSAGES_DEBUG", "all"); -#else - setenv("G_MESSAGES_DEBUG", "all", 1); -#endif extcap->debug = TRUE; break; case EXTCAP_OPT_DEBUG_FILE: extcap_init_custom_log(optargument); - g_log_set_default_handler(extcap_custom_log, NULL); break; case EXTCAP_OPT_LIST_INTERFACES: extcap->do_list_interfaces = 1; @@ -337,11 +315,13 @@ void extcap_help_add_header(extcap_parameters * extcap, char * help_header) void extcap_init_custom_log(const char* filename) { + FILE *custom_log; if (!filename || strlen(filename) == 0) return; custom_log = fopen(filename, "w"); if (!custom_log) g_error("Can't open custom log file: %s (%s)", filename, strerror(errno)); + ws_log_add_custom_file(custom_log); } void extcap_config_debug(unsigned* count) diff --git a/extcap/randpktdump.c b/extcap/randpktdump.c index be7dc52810..a3336f8e7a 100644 --- a/extcap/randpktdump.c +++ b/extcap/randpktdump.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -127,7 +128,7 @@ static int list_config(char *interface) static void randpktdump_cmdarg_err(const char *msg_format, va_list ap) { - g_logv(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, msg_format, ap); + ws_logv(LOG_DOMAIN_CAPCHILD, LOG_LEVEL_WARNING, msg_format, ap); } int main(int argc, char *argv[]) @@ -150,8 +151,19 @@ int main(int argc, char *argv[]) char* help_url; char* help_header = NULL; + /* Initialize log handler early so we can have proper logging during startup. */ + ws_log_init(NULL); + cmdarg_err_init(randpktdump_cmdarg_err, randpktdump_cmdarg_err); + /* Command line options are parsed too late to configure logging, do it + manually. */ + const char *opt_err_val; + if ((opt_err_val = ws_log_set_level_args(&argc, argv)) != NULL) { + cmdarg_err("Invalid log level \"%s\"", opt_err_val); + return EXIT_FAILURE; + } + /* * Get credential information for later use. */ diff --git a/extcap/ssh-base.c b/extcap/ssh-base.c index 342cf6d578..98f968f657 100644 --- a/extcap/ssh-base.c +++ b/extcap/ssh-base.c @@ -15,10 +15,10 @@ #include "ssh-base.h" #include -#include #include #include #include +#include static void extcap_log(int priority _U_, const char *function, const char *buffer, void *userdata _U_) { @@ -92,7 +92,7 @@ ssh_session create_ssh_connection(const ssh_params_t* ssh_params, char** err_inf ssh_options_get(sshs, SSH_OPTIONS_USER, &username); ssh_options_get_port(sshs, &port); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Opening ssh connection to %s@%s:%u", username, + ws_log(LOG_DOMAIN_CAPCHILD, LOG_LEVEL_INFO, "Opening ssh connection to %s@%s:%u", username, ssh_params->host, port); ssh_string_free_char(username); @@ -108,36 +108,36 @@ ssh_session create_ssh_connection(const ssh_params_t* ssh_params, char** err_inf ssh_key pkey = ssh_key_new(); int ret; - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Connecting using public key in %s...", ssh_params->sshkey_path); + ws_info("Connecting using public key in %s...", ssh_params->sshkey_path); ret = ssh_pki_import_privkey_file(ssh_params->sshkey_path, ssh_params->sshkey_passphrase, NULL, NULL, &pkey); if (ret == SSH_OK) { if (ssh_userauth_publickey(sshs, NULL, pkey) == SSH_AUTH_SUCCESS) { - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "done"); + ws_info("done"); ssh_key_free(pkey); return sshs; } } ssh_key_free(pkey); - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "failed (%s)", ssh_get_error(sshs)); + ws_info("failed (%s)", ssh_get_error(sshs)); } /* Try to authenticate using standard public key */ - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Connecting using standard public key..."); + ws_info("Connecting using standard public key..."); if (ssh_userauth_publickey_auto(sshs, NULL, NULL) == SSH_AUTH_SUCCESS) { - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "done"); + ws_info("done"); return sshs; } - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "failed"); + ws_info("failed"); /* If a password has been provided and all previous attempts failed, try to use it */ if (ssh_params->password) { - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Connecting using password..."); + ws_info("Connecting using password..."); if (ssh_userauth_password(sshs, ssh_params->username, ssh_params->password) == SSH_AUTH_SUCCESS) { - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "done"); + ws_info("done"); return sshs; } - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "failed"); + ws_info("failed"); } *err_info = g_strdup_printf("Can't find a valid authentication. Disconnecting."); diff --git a/file.c b/file.c index 17a01ce0d0..846880b003 100644 --- a/file.c +++ b/file.c @@ -813,8 +813,6 @@ cf_continue_tail(capture_file *cf, volatile int to_read, wtap_rec *rec, /* Don't freeze/thaw the list when doing live capture */ /*packet_list_freeze();*/ - /*g_log(NULL, G_LOG_LEVEL_MESSAGE, "cf_continue_tail: %u new: %u", cf->count, to_read);*/ - epan_dissect_init(&edt, cf->epan, create_proto_tree, FALSE); TRY { @@ -866,9 +864,6 @@ cf_continue_tail(capture_file *cf, volatile int to_read, wtap_rec *rec, epan_dissect_cleanup(&edt); - /*g_log(NULL, G_LOG_LEVEL_MESSAGE, "cf_continue_tail: count %u state: %u err: %u", - cf->count, cf->state, *err);*/ - /* Don't freeze/thaw the list when doing live capture */ /*packet_list_thaw();*/ /* With the new packet list the first packet diff --git a/log.h b/log.h deleted file mode 100644 index 8eb9df96dd..0000000000 --- a/log.h +++ /dev/null @@ -1,40 +0,0 @@ -/* log.h - * log output definitions - * - * Wireshark - Network traffic analyzer - * By Gerald Combs - * Copyright 1998 Gerald Combs - * - * SPDX-License-Identifier: GPL-2.0-or-later - */ - -#ifndef __LOG_H__ -#define __LOG_H__ - -/* capture domain (except for capture child, see below) */ -#define LOG_DOMAIN_CAPTURE "Capture" - -/* capture child domain (the capture child might also contain file domain messages!) */ -#define LOG_DOMAIN_CAPTURE_CHILD "CaptureChild" - -/* main domain */ -#define LOG_DOMAIN_MAIN "Main" - -/* enable very verbose capture log debug output */ -/* (might slightly degrade performance) */ -/*#define LOG_CAPTURE_VERBOSE*/ - -#endif - -/* - * Editor modelines - https://www.wireshark.org/tools/modelines.html - * - * Local variables: - * c-basic-offset: 4 - * tab-width: 8 - * indent-tabs-mode: nil - * End: - * - * vi: set shiftwidth=4 tabstop=8 expandtab: - * :indentSize=4:tabSize=8:noTabs=true: - */ diff --git a/rawshark.c b/rawshark.c index 77f85eed66..28593cd263 100644 --- a/rawshark.c +++ b/rawshark.c @@ -60,6 +60,7 @@ #include #include #include +#include #include #include "globals.h" @@ -102,7 +103,6 @@ #include "capture/capture-wpcap.h" #endif /* _WIN32 */ #endif /* HAVE_LIBPCAP */ -#include "log.h" #if 0 /* @@ -208,12 +208,6 @@ print_usage(FILE *output) fprintf(output, " -v display version info and exit\n"); } -static void -log_func_ignore (const gchar *log_domain _U_, GLogLevelFlags log_level _U_, - const gchar *message _U_, gpointer user_data _U_) -{ -} - /** * Open a pipe for raw input. This is a stripped-down version of * pcap_loop.c:cap_pipe_open_live(). @@ -234,7 +228,7 @@ raw_pipe_open(const char *pipe_name) #endif int rfd; - g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "open_raw_pipe: %s", pipe_name); + ws_log(LOG_DOMAIN_CAPCHILD, LOG_LEVEL_DEBUG, "open_raw_pipe: %s", pipe_name); /* * XXX Rawshark blocks until we return @@ -417,7 +411,6 @@ main(int argc, char *argv[]) gchar *rfilters[64]; e_prefs *prefs_p; char badopt; - int log_flags; GPtrArray *disp_fields = g_ptr_array_new(); guint fc; gboolean skip_pcap_header = FALSE; @@ -454,8 +447,19 @@ main(int argc, char *argv[]) setlocale(LC_ALL, ""); #endif + /* Initialize log handler early so we can have proper logging during startup. */ + ws_log_init(NULL); + cmdarg_err_init(rawshark_cmdarg_err, rawshark_cmdarg_err_cont); + /* Command line options are parsed too late to configure logging, do it + manually. */ + const char *opt_err_val; + if ((opt_err_val = ws_log_set_level_args(&argc, argv)) != NULL) { + cmdarg_err("Invalid log level \"%s\"", opt_err_val); + return INVALID_OPTION; + } + /* Initialize the version information. */ ws_init_version_info("Rawshark (Wireshark)", NULL, epan_get_compiled_version_info, @@ -493,20 +497,6 @@ main(int argc, char *argv[]) err_msg); } - /* nothing more than the standard GLib handler, but without a warning */ - log_flags = - G_LOG_LEVEL_WARNING | - G_LOG_LEVEL_MESSAGE | - G_LOG_LEVEL_INFO | - G_LOG_LEVEL_DEBUG; - - g_log_set_handler(NULL, - (GLogLevelFlags)log_flags, - log_func_ignore, NULL /* user_data */); - g_log_set_handler(LOG_DOMAIN_CAPTURE_CHILD, - (GLogLevelFlags)log_flags, - log_func_ignore, NULL /* user_data */); - init_report_message("rawshark", &rawshark_report_routines); timestamp_set_type(TS_RELATIVE); diff --git a/sharkd.c b/sharkd.c index 3456e0fcf8..d3c770d71d 100644 --- a/sharkd.c +++ b/sharkd.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -54,8 +55,6 @@ #include -#include "log.h" - #include #include @@ -115,8 +114,19 @@ main(int argc, char *argv[]) cfile_close_failure_message }; + /* Initialize log handler early so we can have proper logging during startup. */ + ws_log_init(NULL); + cmdarg_err_init(sharkd_cmdarg_err, sharkd_cmdarg_err_cont); + /* Command line options are parsed too late to configure logging, do it + manually. */ + const char *opt_err_val; + if ((opt_err_val = ws_log_set_level_args(&argc, argv)) != NULL) { + cmdarg_err("Invalid log level \"%s\"", opt_err_val); + return INIT_FAILED; + } + /* * Get credential information for later use, and drop privileges * before doing anything else. diff --git a/tfshark.c b/tfshark.c index cd15617620..c9192d4dc1 100644 --- a/tfshark.c +++ b/tfshark.c @@ -11,6 +11,8 @@ #include +#define WS_LOG_DOMAIN LOG_DOMAIN_MAIN + #include #include #include @@ -45,6 +47,7 @@ #include #include #include +#include #include #include @@ -74,7 +77,6 @@ #include #include -#include "log.h" #include #ifdef HAVE_PLUGINS @@ -250,31 +252,6 @@ glossary_option_help(void) fprintf(output, "\n"); } -static void -tfshark_log_handler (const gchar *log_domain, GLogLevelFlags log_level, - const gchar *message, gpointer user_data) -{ - /* ignore log message, if log_level isn't interesting based - upon the console log preferences. - If the preferences haven't been loaded yet, display the - message anyway. - - The default console_log_level preference value is such that only - ERROR, CRITICAL and WARNING level messages are processed; - MESSAGE, INFO and DEBUG level messages are ignored. - - XXX: Aug 07, 2009: Prior tshark g_log code was hardwired to process only - ERROR and CRITICAL level messages so the current code is a behavioral - change. The current behavior is the same as in Wireshark. - */ - if (prefs_loaded && (log_level & G_LOG_LEVEL_MASK & prefs.console_log_level) == 0) { - return; - } - - g_log_default_handler(log_domain, log_level, message, user_data); - -} - static void print_current_user(void) { gchar *cur_user, *cur_group; @@ -323,7 +300,6 @@ main(int argc, char *argv[]) dfilter_t *dfcode = NULL; gchar *err_msg; e_prefs *prefs_p; - int log_flags; gchar *output_only = NULL; /* @@ -371,8 +347,19 @@ main(int argc, char *argv[]) setlocale(LC_ALL, ""); #endif + /* Initialize log handler early so we can have proper logging during startup. */ + ws_log_init(NULL); + cmdarg_err_init(tfshark_cmdarg_err, tfshark_cmdarg_err_cont); + /* Command line options are parsed too late to configure logging, do it + manually. */ + const char *opt_err_val; + if ((opt_err_val = ws_log_set_level_args(&argc, argv)) != NULL) { + cmdarg_err("Invalid log level \"%s\"", opt_err_val); + return INVALID_OPTION; + } + #ifdef _WIN32 create_app_running_mutex(); #endif /* _WIN32 */ @@ -460,24 +447,6 @@ main(int argc, char *argv[]) if (print_summary == -1) print_summary = (print_details || print_hex) ? FALSE : TRUE; -/** Send All g_log messages to our own handler **/ - - log_flags = - G_LOG_LEVEL_ERROR| - G_LOG_LEVEL_CRITICAL| - G_LOG_LEVEL_WARNING| - G_LOG_LEVEL_MESSAGE| - G_LOG_LEVEL_INFO| - G_LOG_LEVEL_DEBUG| - G_LOG_FLAG_FATAL|G_LOG_FLAG_RECURSION; - - g_log_set_handler(NULL, - (GLogLevelFlags)log_flags, - tfshark_log_handler, NULL /* user_data */); - g_log_set_handler(LOG_DOMAIN_MAIN, - (GLogLevelFlags)log_flags, - tfshark_log_handler, NULL /* user_data */); - init_report_message("tfshark", &tfshark_report_routines); timestamp_set_type(TS_RELATIVE); diff --git a/tshark.c b/tshark.c index fddb346c65..a224c5eaf2 100644 --- a/tshark.c +++ b/tshark.c @@ -12,7 +12,7 @@ #include -#define G_LOG_DOMAIN "tshark" +#define WS_LOG_DOMAIN LOG_DOMAIN_MAIN #include #include @@ -117,12 +117,12 @@ #include #include #endif /* HAVE_LIBPCAP */ -#include "log.h" #include #include #include #include +#include #ifdef _WIN32 #include #endif @@ -530,31 +530,6 @@ glossary_option_help(void) fprintf(output, "\n"); } -static void -tshark_log_handler (const gchar *log_domain, GLogLevelFlags log_level, - const gchar *message, gpointer user_data) -{ - /* ignore log message, if log_level isn't interesting based - upon the console log preferences. - If the preferences haven't been loaded yet, display the - message anyway. - - The default console_log_level preference value is such that only - ERROR, CRITICAL and WARNING level messages are processed; - MESSAGE, INFO and DEBUG level messages are ignored. - - XXX: Aug 07, 2009: Prior tshark g_log code was hardwired to process only - ERROR and CRITICAL level messages so the current code is a behavioral - change. The current behavior is the same as in Wireshark. - */ - if (prefs_loaded && (log_level & G_LOG_LEVEL_MASK & prefs.console_log_level) == 0) { - return; - } - - g_log_default_handler(log_domain, log_level, message, user_data); - -} - static void print_current_user(void) { gchar *cur_user, *cur_group; @@ -757,7 +732,6 @@ main(int argc, char *argv[]) dfilter_t *rfcode = NULL; dfilter_t *dfcode = NULL; e_prefs *prefs_p; - int log_flags; gchar *output_only = NULL; gchar *volatile pdu_export_arg = NULL; char *volatile exp_pdu_filename = NULL; @@ -798,10 +772,21 @@ main(int argc, char *argv[]) setlocale(LC_ALL, ""); #endif - ws_debug("tshark started with %d args", argc); + /* Initialize log handler early so we can have proper logging during startup. */ + ws_log_init(NULL); cmdarg_err_init(tshark_cmdarg_err, tshark_cmdarg_err_cont); + /* Command line options are parsed too late to configure logging, do it + manually. */ + const char *opt_err_val; + if ((opt_err_val = ws_log_set_level_args(&argc, argv)) != NULL) { + cmdarg_err("Invalid log level \"%s\"", opt_err_val); + return INVALID_OPTION; + } + + ws_debug("tshark started with %d args", argc); + #ifdef _WIN32 create_app_running_mutex(); #endif /* _WIN32 */ @@ -922,33 +907,6 @@ main(int argc, char *argv[]) } } -/** Send All g_log messages to our own handler **/ - - log_flags = - G_LOG_LEVEL_ERROR| - G_LOG_LEVEL_CRITICAL| - G_LOG_LEVEL_WARNING| - G_LOG_LEVEL_MESSAGE| - G_LOG_LEVEL_INFO| - G_LOG_LEVEL_DEBUG| - G_LOG_FLAG_FATAL|G_LOG_FLAG_RECURSION; - - g_log_set_handler(NULL, - (GLogLevelFlags)log_flags, - tshark_log_handler, NULL /* user_data */); - g_log_set_handler(LOG_DOMAIN_MAIN, - (GLogLevelFlags)log_flags, - tshark_log_handler, NULL /* user_data */); - -#ifdef HAVE_LIBPCAP - g_log_set_handler(LOG_DOMAIN_CAPTURE, - (GLogLevelFlags)log_flags, - tshark_log_handler, NULL /* user_data */); - g_log_set_handler(LOG_DOMAIN_CAPTURE_CHILD, - (GLogLevelFlags)log_flags, - tshark_log_handler, NULL /* user_data */); -#endif - init_report_message("TShark", &tshark_report_routines); #ifdef HAVE_LIBPCAP @@ -2403,8 +2361,6 @@ pipe_timer_cb(gpointer data) /* try to read data from the pipe only 5 times, to avoid blocking */ while(iterations < 5) { - /*g_log(NULL, G_LOG_LEVEL_DEBUG, "pipe_timer_cb: new iteration");*/ - /* Oddly enough although Named pipes don't work on win9x, PeekNamedPipe does !!! */ handle = (HANDLE) _get_osfhandle (pipe_input_p->source); @@ -2419,18 +2375,15 @@ pipe_timer_cb(gpointer data) callback */ if (!result || avail > 0 || childstatus != STILL_ACTIVE) { - /*g_log(NULL, G_LOG_LEVEL_DEBUG, "pipe_timer_cb: data avail");*/ - /* And call the real handler */ if (!pipe_input_p->input_cb(pipe_input_p->source, pipe_input_p->user_data)) { - g_log(NULL, G_LOG_LEVEL_DEBUG, "pipe_timer_cb: input pipe closed, iterations: %u", iterations); + ws_debug("input pipe closed, iterations: %u", iterations); /* pipe closed, return false so that the timer is stopped */ g_mutex_unlock (pipe_input_p->callback_running); return FALSE; } } else { - /*g_log(NULL, G_LOG_LEVEL_DEBUG, "pipe_timer_cb: no data avail");*/ /* No data, stop now */ break; } @@ -2438,8 +2391,6 @@ pipe_timer_cb(gpointer data) iterations++; } - /*g_log(NULL, G_LOG_LEVEL_DEBUG, "pipe_timer_cb: finished with iterations: %u, new timer", iterations);*/ - g_mutex_unlock (pipe_input_p->callback_running); /* we didn't stopped the timer, so let it run */ @@ -2465,7 +2416,6 @@ pipe_input_set_handler(gint source, gpointer user_data, ws_process_id *child_pro this but doesn't seem to work over processes. Attempt to do something similar here, start a timer and check for data on every timeout. */ - /*g_log(NULL, G_LOG_LEVEL_DEBUG, "pipe_input_set_handler: new");*/ pipe_input.pipe_input_id = g_timeout_add(200, pipe_timer_cb, &pipe_input); #endif } @@ -2627,7 +2577,7 @@ capture(void) #endif /* Call the real handler */ if (!pipe_input.input_cb(pipe_input.source, pipe_input.user_data)) { - g_log(NULL, G_LOG_LEVEL_DEBUG, "input pipe closed"); + ws_debug("input pipe closed"); ret = FALSE; loop_running = FALSE; } @@ -2704,9 +2654,9 @@ capture_input_new_file(capture_session *cap_session, gchar *new_file) int err; if (cap_session->state == CAPTURE_PREPARING) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture started."); + ws_message("Capture started."); } - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "File: \"%s\"", new_file); + ws_message("File: \"%s\"", new_file); g_assert(cap_session->state == CAPTURE_PREPARING || cap_session->state == CAPTURE_RUNNING); diff --git a/ui/capture.c b/ui/capture.c index 8fcf3d4123..5cf1eb6129 100644 --- a/ui/capture.c +++ b/ui/capture.c @@ -9,6 +9,7 @@ */ #include "config.h" +#define WS_LOG_DOMAIN LOG_DOMAIN_CAPTURE #ifdef HAVE_LIBPCAP @@ -29,7 +30,6 @@ #include "ui/util.h" #include "ui/urls.h" #include "capture/capture-pcap-util.h" -#include #ifdef _WIN32 #include "capture/capture-wpcap.h" @@ -41,7 +41,7 @@ #include "wsutil/file_util.h" #include "wsutil/str_util.h" #include -#include "log.h" +#include typedef struct if_stat_cache_item_s { char *name; @@ -122,7 +122,7 @@ capture_start(capture_options *capture_opts, capture_session *cap_session, info_ cap_session->state = CAPTURE_PREPARING; cap_session->count = 0; - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Start ..."); + ws_message("Capture Start ..."); source = get_iface_list_string(capture_opts, IFLIST_SHOW_FILTER); cf_set_tempfile_source((capture_file *)cap_session->cf, source->str); g_string_free(source, TRUE); @@ -134,7 +134,7 @@ capture_start(capture_options *capture_opts, capture_session *cap_session, info_ capture_opts->save_file = NULL; } - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Start failed."); + ws_message("Capture Start failed."); cap_session->state = CAPTURE_STOPPED; return FALSE; } @@ -174,7 +174,7 @@ capture_start(capture_options *capture_opts, capture_session *cap_session, info_ void capture_stop(capture_session *cap_session) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Stop ..."); + ws_message("Capture Stop ..."); capture_callback_invoke(capture_cb_capture_stopping, cap_session); @@ -186,7 +186,7 @@ capture_stop(capture_session *cap_session) void capture_kill_child(capture_session *cap_session) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_INFO, "Capture Kill"); + ws_info("Capture Kill"); /* kill the capture child */ sync_pipe_kill(cap_session->fork_child); @@ -379,9 +379,9 @@ capture_input_new_file(capture_session *cap_session, gchar *new_file) gchar *err_msg; if(cap_session->state == CAPTURE_PREPARING) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture started"); + ws_message("Capture started"); } - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "File: \"%s\"", new_file); + ws_message("File: \"%s\"", new_file); g_assert(cap_session->state == CAPTURE_PREPARING || cap_session->state == CAPTURE_RUNNING); @@ -555,9 +555,9 @@ static void capture_input_drops(capture_session *cap_session, guint32 dropped, const char* interface_name) { if (interface_name != NULL) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_INFO, "%u packet%s dropped from %s", dropped, plurality(dropped, "", "s"), interface_name); + ws_info("%u packet%s dropped from %s", dropped, plurality(dropped, "", "s"), interface_name); } else { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_INFO, "%u packet%s dropped", dropped, plurality(dropped, "", "s")); + ws_info("%u packet%s dropped", dropped, plurality(dropped, "", "s")); } g_assert(cap_session->state == CAPTURE_RUNNING); @@ -580,8 +580,7 @@ capture_input_error(capture_session *cap_session, char *error_msg, gchar *safe_error_msg; gchar *safe_secondary_error_msg; - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Error message from child: \"%s\", \"%s\"", - error_msg, secondary_error_msg); + ws_message("Error message from child: \"%s\", \"%s\"", error_msg, secondary_error_msg); g_assert(cap_session->state == CAPTURE_PREPARING || cap_session->state == CAPTURE_RUNNING); @@ -618,7 +617,7 @@ capture_input_cfilter_error(capture_session *cap_session, guint i, gchar *safe_cfilter_error_msg; interface_options *interface_opts; - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture filter error message from child: \"%s\"", error_message); + ws_message("Capture filter error message from child: \"%s\"", error_message); g_assert(cap_session->state == CAPTURE_PREPARING || cap_session->state == CAPTURE_RUNNING); g_assert(i < capture_opts->ifaces->len); @@ -665,7 +664,7 @@ capture_input_closed(capture_session *cap_session, gchar *msg) capture_options *capture_opts = cap_session->capture_opts; int err; - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture stopped."); + ws_message("Capture stopped."); g_assert(cap_session->state == CAPTURE_PREPARING || cap_session->state == CAPTURE_RUNNING); if (msg != NULL) diff --git a/ui/console.c b/ui/console.c index 71cd3b8ebf..1a1af2ebd7 100644 --- a/ui/console.c +++ b/ui/console.c @@ -18,127 +18,33 @@ #include "console.h" -#include "log.h" - -static void -console_log_handler(const char *log_domain, GLogLevelFlags log_level, - const char *message, gpointer user_data _U_) +void +console_log_writer(const char *message, enum ws_log_domain domain _U_, + enum ws_log_level level, void *ptr _U_) { - /* ignore log message, if log_level isn't interesting based - upon the console log preferences. - If the preferences haven't been loaded loaded yet, display the - message anyway. - - The default console_log_level preference value is such that only - ERROR, CRITICAL and WARNING level messages are processed; - MESSAGE, INFO and DEBUG level messages are ignored. */ - if((log_level & G_LOG_LEVEL_MASK & prefs.console_log_level) == 0 && - prefs.console_log_level != 0) { - return; - } - + gboolean fatal = level == LOG_LEVEL_ERROR; #ifdef _WIN32 - time_t curr; - struct tm *today; - const char *level; - FILE *stream = stderr; - - if (prefs.gui_console_open != console_open_never || log_level & G_LOG_LEVEL_ERROR) { + if (prefs.gui_console_open != console_open_never || fatal) { /* the user wants a console or the application will terminate immediately */ create_console(); } +#else + (void)fatal; +#endif /* _WIN32 */ - switch(log_level & G_LOG_LEVEL_MASK) { - case G_LOG_LEVEL_ERROR: - level = "Err "; - break; - case G_LOG_LEVEL_CRITICAL: - level = "Crit"; - break; - case G_LOG_LEVEL_WARNING: - level = "Warn"; - break; - case G_LOG_LEVEL_MESSAGE: - level = "Msg "; - break; - case G_LOG_LEVEL_INFO: - level = "Info"; - stream = stdout; - break; - case G_LOG_LEVEL_DEBUG: - level = "Dbg "; - stream = stdout; - break; - default: - fprintf(stderr, "unknown log_level %d\n", log_level); - level = NULL; - g_assert_not_reached(); - } + FILE *fp = stderr; + g_assert(message); - /* create a "timestamp" */ - time(&curr); - today = localtime(&curr); - guint64 microseconds = create_timestamp(); - if (today != NULL) { - fprintf(stream, "%02d:%02d:%02d.%03" G_GUINT64_FORMAT " %8s %s %s\n", - today->tm_hour, today->tm_min, today->tm_sec, - microseconds % 1000000 / 1000, - log_domain != NULL ? log_domain : "", - level, message); - } else { - fprintf(stream, "Time not representable %8s %s %s\n", - log_domain != NULL ? log_domain : "", - level, message); - } - fflush(stream); + fputs(message, fp); + fputc('\n', fp); + fflush(fp); - if(log_level & G_LOG_LEVEL_ERROR) { +#ifdef _WIN32 + if (fatal) { /* wait for a key press before the following error handler will terminate the program this way the user at least can read the error message */ printf("\n\nPress any key to exit\n"); _getch(); } -#else /* _WIN32 */ - g_log_default_handler(log_domain, log_level, message, NULL); #endif /* _WIN32 */ } - -void set_console_log_handler(void) -{ - GLogLevelFlags log_flags; - /* Arrange that if we have no console window, and a GLib message logging - routine is called to log a message, we pop up a console window. - - We do that by inserting our own handler for all messages logged - to the default domain; that handler pops up a console if necessary, - and then calls the default handler. */ - - /* We might want to have component specific log levels later ... */ - - log_flags = (GLogLevelFlags) - (G_LOG_LEVEL_ERROR| - G_LOG_LEVEL_CRITICAL| - G_LOG_LEVEL_WARNING| - G_LOG_LEVEL_MESSAGE| - G_LOG_LEVEL_INFO| - G_LOG_LEVEL_DEBUG| - G_LOG_FLAG_FATAL| - G_LOG_FLAG_RECURSION); - - g_log_set_handler(NULL, - log_flags, - console_log_handler, NULL /* user_data */); - g_log_set_handler(LOG_DOMAIN_MAIN, - log_flags, - console_log_handler, NULL /* user_data */); - -#ifdef HAVE_LIBPCAP - g_log_set_handler(LOG_DOMAIN_CAPTURE, - log_flags, - console_log_handler, NULL /* user_data */); - g_log_set_handler(LOG_DOMAIN_CAPTURE_CHILD, - log_flags, - console_log_handler, NULL /* user_data */); - -#endif -} diff --git a/ui/console.h b/ui/console.h index 1506fa0936..196ef231f9 100644 --- a/ui/console.h +++ b/ui/console.h @@ -21,9 +21,13 @@ extern "C" { #endif /* __cplusplus */ -/** Set the console log handler. +#include + +/** The GUI log writer. */ -void set_console_log_handler(void); +void +console_log_writer(const char *message, enum ws_log_domain domain, + enum ws_log_level level, void *ptr); #ifdef __cplusplus } diff --git a/ui/iface_lists.c b/ui/iface_lists.c index 26610bc38e..408a111ccb 100644 --- a/ui/iface_lists.c +++ b/ui/iface_lists.c @@ -19,11 +19,11 @@ #include #include +#include #include "ui/capture_ui_utils.h" #include "ui/capture_globals.h" #include "ui/iface_lists.h" -#include "../log.h" /* * Try to populate the given device with options (like capture filter) from @@ -433,7 +433,7 @@ fill_in_local_interfaces(void(*update_cb)(void)) /* record the time we started, so we can log total time later */ start_time = g_get_monotonic_time(); - g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_INFO, "fill_in_local_interfaces() starts"); + ws_log(LOG_DOMAIN_MAIN, LOG_LEVEL_INFO, "fill_in_local_interfaces() starts"); if (!initialized) { /* do the actual work */ @@ -443,7 +443,7 @@ fill_in_local_interfaces(void(*update_cb)(void)) /* log how long it took */ elapsed = (g_get_monotonic_time() - start_time) / 1e6; - g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_INFO, "fill_in_local_interfaces() ends, taking %.3fs", elapsed); + ws_log(LOG_DOMAIN_MAIN, LOG_LEVEL_INFO, "fill_in_local_interfaces() ends, taking %.3fs", elapsed); } void diff --git a/ui/qt/about_dialog.cpp b/ui/qt/about_dialog.cpp index e60ef8a327..14535baa60 100644 --- a/ui/qt/about_dialog.cpp +++ b/ui/qt/about_dialog.cpp @@ -28,8 +28,6 @@ #include #endif -#include "log.h" - #include "ui/alert_box.h" #include "ui/last_open_dir.h" #include "ui/help_url.h" diff --git a/ui/qt/capture_file.cpp b/ui/qt/capture_file.cpp index 522012482c..7b6cb97051 100644 --- a/ui/qt/capture_file.cpp +++ b/ui/qt/capture_file.cpp @@ -19,7 +19,6 @@ capture_file cfile; #include "file.h" -#include "log.h" #include "epan/epan_dissect.h" diff --git a/ui/qt/extcap_argument.cpp b/ui/qt/extcap_argument.cpp index a8d677fb52..9b0db2021c 100644 --- a/ui/qt/extcap_argument.cpp +++ b/ui/qt/extcap_argument.cpp @@ -32,11 +32,11 @@ #include #include -#include #include #include #include +#include #include #include @@ -542,7 +542,7 @@ QWidget * ExtArgNumber::createEditor(QWidget * parent) guint tmp = extcap_complex_get_uint(_argument->range_start); if (tmp > G_MAXINT) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Defined value for range_start of %s exceeds valid integer range", _argument->call); + ws_log(LOG_DOMAIN_CAPTURE, LOG_LEVEL_DEBUG, "Defined value for range_start of %s exceeds valid integer range", _argument->call); val = G_MAXINT; } else @@ -553,7 +553,7 @@ QWidget * ExtArgNumber::createEditor(QWidget * parent) } if (_argument->arg_type == EXTCAP_ARG_UNSIGNED && textValidator->bottom() < 0) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "%s sets negative bottom range for unsigned value, setting to 0", _argument->call); + ws_log(LOG_DOMAIN_CAPTURE, LOG_LEVEL_DEBUG, "%s sets negative bottom range for unsigned value, setting to 0", _argument->call); textValidator->setBottom(0); } @@ -567,7 +567,7 @@ QWidget * ExtArgNumber::createEditor(QWidget * parent) guint tmp = extcap_complex_get_uint(_argument->range_end); if (tmp > G_MAXINT) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Defined value for range_end of %s exceeds valid integer range", _argument->call); + ws_log(LOG_DOMAIN_CAPTURE, LOG_LEVEL_DEBUG, "Defined value for range_end of %s exceeds valid integer range", _argument->call); val = G_MAXINT; } else diff --git a/ui/qt/interface_toolbar.cpp b/ui/qt/interface_toolbar.cpp index f8680866a1..601b741949 100644 --- a/ui/qt/interface_toolbar.cpp +++ b/ui/qt/interface_toolbar.cpp @@ -22,7 +22,6 @@ #include "sync_pipe.h" #include "wsutil/file_util.h" -#include "log.h" #ifdef _WIN32 #include #endif diff --git a/ui/qt/main.cpp b/ui/qt/main.cpp index 73d855c021..53baad6b0f 100644 --- a/ui/qt/main.cpp +++ b/ui/qt/main.cpp @@ -41,6 +41,7 @@ #include #include #include +#include #ifdef HAVE_PLUGINS #include #endif @@ -70,7 +71,6 @@ /* general (not Qt specific) */ #include "file.h" #include "epan/color_filters.h" -#include "log.h" #include "epan/rtd_table.h" #include "epan/srt_table.h" @@ -129,19 +129,6 @@ #include //#define DEBUG_STARTUP_TIME 1 -/* -# Log level -# Console log level (for debugging) -# A bitmask of log levels: -# ERROR = 4 -# CRITICAL = 8 -# WARNING = 16 -# MESSAGE = 32 -# INFO = 64 -# DEBUG = 128 - -#define DEBUG_STARTUP_TIME_LOGLEVEL 252 -*/ /* update the main window */ void main_window_update(void) @@ -335,29 +322,29 @@ get_wireshark_runtime_info(GString *str) } static void -g_log_message_handler(QtMsgType type, const QMessageLogContext &, const QString &msg) +qt_log_message_handler(QtMsgType type, const QMessageLogContext &, const QString &msg) { - GLogLevelFlags log_level = G_LOG_LEVEL_DEBUG; + enum ws_log_level log_level = LOG_LEVEL_DEBUG; switch (type) { case QtInfoMsg: - log_level = G_LOG_LEVEL_INFO; + log_level = LOG_LEVEL_INFO; break; // We want qDebug() messages to show up at our default log level. case QtDebugMsg: case QtWarningMsg: - log_level = G_LOG_LEVEL_WARNING; + log_level = LOG_LEVEL_WARNING; break; case QtCriticalMsg: - log_level = G_LOG_LEVEL_CRITICAL; + log_level = LOG_LEVEL_CRITICAL; break; case QtFatalMsg: - log_level = G_LOG_FLAG_FATAL; + log_level = LOG_LEVEL_ERROR; break; default: break; } - g_log(LOG_DOMAIN_MAIN, log_level, "%s", qUtf8Printable(msg)); + ws_log(LOG_DOMAIN_MAIN, log_level, "%s", qUtf8Printable(msg)); } #ifdef HAVE_LIBPCAP @@ -542,18 +529,16 @@ int main(int argc, char *qt_argv[]) macos_enable_layer_backing(); #endif - /* Enable destinations for logging earlier in startup */ - set_console_log_handler(); - qInstallMessageHandler(g_log_message_handler); + /* Initialize log handler early so we can have proper logging during startup. */ + ws_log_init(console_log_writer); + + qInstallMessageHandler(qt_log_message_handler); + #ifdef _WIN32 restore_pipes(); #endif #ifdef DEBUG_STARTUP_TIME - /* At least on Windows there is a problem with the logging as the preferences is taken - * into account and the preferences are loaded pretty late in the startup process. - */ - prefs.console_log_level = DEBUG_STARTUP_TIME_LOGLEVEL; prefs.gui_console_open = console_open_always; #endif /* DEBUG_STARTUP_TIME */ cmdarg_err_init(wireshark_cmdarg_err, wireshark_cmdarg_err_cont); @@ -595,6 +580,14 @@ int main(int argc, char *qt_argv[]) create_app_running_mutex(); #endif /* _WIN32 */ + /* Command line options are parsed too late to configure logging, do it + manually. */ + const char *opt_err_val; + if ((opt_err_val = ws_log_set_level_args(&argc, argv)) != NULL) { + cmdarg_err("Invalid log level \"%s\"", opt_err_val); + exit_application(INVALID_OPTION); + } + /* * Get credential information for later use, and drop privileges * before doing anything else. @@ -608,7 +601,7 @@ int main(int argc, char *qt_argv[]) * executable file. */ /* init_progfile_dir_error = */ init_progfile_dir(argv[0]); - /* g_log(NULL, G_LOG_LEVEL_DEBUG, "progfile_dir: %s", get_progfile_dir()); */ + /* ws_log(NULL, LOG_LEVEL_DEBUG, "progfile_dir: %s", get_progfile_dir()); */ #ifdef _WIN32 ws_init_dll_search_path(); @@ -741,7 +734,7 @@ int main(int argc, char *qt_argv[]) read_language_prefs(); wsApp->loadLanguage(language); - /* g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "Translator %s", language); */ + /* ws_log(LOG_DOMAIN_MAIN, LOG_LEVEL_DEBUG, "Translator %s", language); */ // Init the main window (and splash) main_w = new(MainWindow); @@ -763,7 +756,7 @@ int main(int argc, char *qt_argv[]) } #ifdef DEBUG_STARTUP_TIME - g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_INFO, "set_console_log_handler, elapsed time %" G_GUINT64_FORMAT " us \n", g_get_monotonic_time() - start_time); + ws_log(LOG_DOMAIN_MAIN, LOG_LEVEL_INFO, "set_console_log_handler, elapsed time %" G_GUINT64_FORMAT " us \n", g_get_monotonic_time() - start_time); #endif #ifdef HAVE_LIBPCAP @@ -783,7 +776,7 @@ int main(int argc, char *qt_argv[]) splash_update(RA_DISSECTORS, NULL, NULL); #ifdef DEBUG_STARTUP_TIME - g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_INFO, "Calling epan init, elapsed time %" G_GUINT64_FORMAT " us \n", g_get_monotonic_time() - start_time); + ws_log(LOG_DOMAIN_MAIN, LOG_LEVEL_INFO, "Calling epan init, elapsed time %" G_GUINT64_FORMAT " us \n", g_get_monotonic_time() - start_time); #endif /* Register all dissectors; we must do this before checking for the "-G" flag, as the "-G" flag dumps information registered by the @@ -796,9 +789,8 @@ int main(int argc, char *qt_argv[]) } #ifdef DEBUG_STARTUP_TIME /* epan_init resets the preferences */ - prefs.console_log_level = DEBUG_STARTUP_TIME_LOGLEVEL; prefs.gui_console_open = console_open_always; - g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_INFO, "epan done, elapsed time %" G_GUINT64_FORMAT " us \n", g_get_monotonic_time() - start_time); + ws_log(LOG_DOMAIN_MAIN, LOG_LEVEL_INFO, "epan done, elapsed time %" G_GUINT64_FORMAT " us \n", g_get_monotonic_time() - start_time); #endif /* Register all audio codecs. */ @@ -817,7 +809,7 @@ int main(int argc, char *qt_argv[]) splash_update(RA_LISTENERS, NULL, NULL); #ifdef DEBUG_STARTUP_TIME - g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_INFO, "Register all tap listeners, elapsed time %" G_GUINT64_FORMAT " us \n", g_get_monotonic_time() - start_time); + ws_log(LOG_DOMAIN_MAIN, LOG_LEVEL_INFO, "Register all tap listeners, elapsed time %" G_GUINT64_FORMAT " us \n", g_get_monotonic_time() - start_time); #endif /* Register all tap listeners; we do this before we parse the arguments, as the "-z" argument can specify a registered tap. */ @@ -835,13 +827,13 @@ int main(int argc, char *qt_argv[]) } #ifdef DEBUG_STARTUP_TIME - g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_INFO, "Calling extcap_register_preferences, elapsed time %" G_GUINT64_FORMAT " us \n", g_get_monotonic_time() - start_time); + ws_log(LOG_DOMAIN_MAIN, LOG_LEVEL_INFO, "Calling extcap_register_preferences, elapsed time %" G_GUINT64_FORMAT " us \n", g_get_monotonic_time() - start_time); #endif splash_update(RA_EXTCAP, NULL, NULL); extcap_register_preferences(); splash_update(RA_PREFERENCES, NULL, NULL); #ifdef DEBUG_STARTUP_TIME - g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_INFO, "Calling module preferences, elapsed time %" G_GUINT64_FORMAT " us \n", g_get_monotonic_time() - start_time); + ws_log(LOG_DOMAIN_MAIN, LOG_LEVEL_INFO, "Calling module preferences, elapsed time %" G_GUINT64_FORMAT " us \n", g_get_monotonic_time() - start_time); #endif global_commandline_info.prefs_p = ws_app.readConfigurationFiles(false); @@ -863,7 +855,7 @@ int main(int argc, char *qt_argv[]) #ifdef HAVE_LIBPCAP #ifdef DEBUG_STARTUP_TIME - g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_INFO, "Calling fill_in_local_interfaces, elapsed time %" G_GUINT64_FORMAT " us \n", g_get_monotonic_time() - start_time); + ws_log(LOG_DOMAIN_MAIN, LOG_LEVEL_INFO, "Calling fill_in_local_interfaces, elapsed time %" G_GUINT64_FORMAT " us \n", g_get_monotonic_time() - start_time); #endif splash_update(RA_INTERFACES, NULL, NULL); @@ -940,7 +932,7 @@ int main(int argc, char *qt_argv[]) changed either from one of the preferences file or from the command line that their preferences have changed. */ #ifdef DEBUG_STARTUP_TIME - g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_INFO, "Calling prefs_apply_all, elapsed time %" G_GUINT64_FORMAT " us \n", g_get_monotonic_time() - start_time); + ws_log(LOG_DOMAIN_MAIN, LOG_LEVEL_INFO, "Calling prefs_apply_all, elapsed time %" G_GUINT64_FORMAT " us \n", g_get_monotonic_time() - start_time); #endif prefs_apply_all(); prefs_to_capture_opts(); @@ -986,7 +978,7 @@ int main(int argc, char *qt_argv[]) } wsApp->allSystemsGo(); - g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_INFO, "Wireshark is up and ready to go, elapsed time %.3fs\n", (float) (g_get_monotonic_time() - start_time) / 1000000); + ws_log(LOG_DOMAIN_MAIN, LOG_LEVEL_INFO, "Wireshark is up and ready to go, elapsed time %.3fs\n", (float) (g_get_monotonic_time() - start_time) / 1000000); SimpleDialog::displayQueuedMessages(main_w); /* User could specify filename, or display filter, or both */ diff --git a/ui/qt/main_window.cpp b/ui/qt/main_window.cpp index 56fbd3d1b4..119950cb86 100644 --- a/ui/qt/main_window.cpp +++ b/ui/qt/main_window.cpp @@ -21,6 +21,7 @@ DIAG_ON(frame-larger-than=) #include "epan/conversation_filter.h" #include #include +#include #include #include #include @@ -843,7 +844,7 @@ void MainWindow::setPipeInputHandler(gint source, gpointer user_data, ws_process this but doesn't seem to work over processes. Attempt to do something similar here, start a timer and check for data on every timeout. */ - /*g_log(NULL, G_LOG_LEVEL_DEBUG, "pipe_input_set_handler: new");*/ + /*ws_log(NULL, LOG_LEVEL_DEBUG, "pipe_input_set_handler: new");*/ if (pipe_timer_) { disconnect(pipe_timer_, SIGNAL(timeout()), this, SLOT(pipeTimeout())); diff --git a/ui/qt/main_window_slots.cpp b/ui/qt/main_window_slots.cpp index 947914411c..d810d44e5c 100644 --- a/ui/qt/main_window_slots.cpp +++ b/ui/qt/main_window_slots.cpp @@ -44,6 +44,7 @@ DIAG_ON(frame-larger-than=) #include "wsutil/file_util.h" #include "wsutil/filesystem.h" +#include #include "epan/addr_resolv.h" #include "epan/column.h" @@ -927,8 +928,6 @@ void MainWindow::pipeTimeout() { /* try to read data from the pipe only 5 times, to avoid blocking */ while (iterations < 5) { - /*g_log(NULL, G_LOG_LEVEL_DEBUG, "pipe_timer_cb: new iteration");*/ - /* Oddly enough although Named pipes don't work on win9x, PeekNamedPipe does !!! */ handle = (HANDLE)_get_osfhandle(pipe_source_); @@ -943,17 +942,14 @@ void MainWindow::pipeTimeout() { callback */ if (!result || avail > 0 || childstatus != STILL_ACTIVE) { - /*g_log(NULL, G_LOG_LEVEL_DEBUG, "pipe_timer_cb: data avail");*/ - /* And call the real handler */ if (!pipe_input_cb_(pipe_source_, pipe_user_data_)) { - g_log(NULL, G_LOG_LEVEL_DEBUG, "pipe_timer_cb: input pipe closed, iterations: %u", iterations); + ws_log(LOG_DOMAIN_DEFAULT, LOG_LEVEL_DEBUG, "pipe_timer_cb: input pipe closed, iterations: %u", iterations); /* pipe closed, return false so that the old timer is not run again */ delete pipe_timer_; return; } } else { - /*g_log(NULL, G_LOG_LEVEL_DEBUG, "pipe_timer_cb: no data avail");*/ /* No data, stop now */ break; } diff --git a/ui/qt/models/credentials_model.cpp b/ui/qt/models/credentials_model.cpp index 5530514a5e..747213b9b0 100644 --- a/ui/qt/models/credentials_model.cpp +++ b/ui/qt/models/credentials_model.cpp @@ -13,7 +13,6 @@ #include "credentials_model.h" #include -#include #include CredentialsModel::CredentialsModel(QObject *parent) diff --git a/ui/qt/sctp_chunk_statistics_dialog.cpp b/ui/qt/sctp_chunk_statistics_dialog.cpp index 6caa180212..291a0fba7c 100644 --- a/ui/qt/sctp_chunk_statistics_dialog.cpp +++ b/ui/qt/sctp_chunk_statistics_dialog.cpp @@ -13,6 +13,7 @@ #include "uat_dialog.h" #include +#include #include "ui/tap-sctp-analysis.h" #include @@ -89,7 +90,7 @@ void SCTPChunkStatisticsDialog::fillTable(bool all, const sctp_assoc_info_t *sel pref_t *pref = prefs_find_preference(prefs_find_module("sctp"),"statistics_chunk_types"); if (!pref) { - g_log(NULL, G_LOG_LEVEL_ERROR, "Can't find preference sctp/statistics_chunk_types"); + ws_log(LOG_DOMAIN_QTUI, LOG_LEVEL_ERROR, "Can't find preference sctp/statistics_chunk_types"); return; } uat_t *uat = prefs_get_uat_value(pref); @@ -105,7 +106,7 @@ void SCTPChunkStatisticsDialog::fillTable(bool all, const sctp_assoc_info_t *sel if (errno == ENOENT) { init = true; } else { - g_log(NULL, G_LOG_LEVEL_ERROR, "Can't open %s: %s", fname, g_strerror(errno)); + ws_log(LOG_DOMAIN_QTUI, LOG_LEVEL_ERROR, "Can't open %s: %s", fname, g_strerror(errno)); return; } } @@ -213,7 +214,7 @@ void SCTPChunkStatisticsDialog::on_pushButton_clicked() pref_t *pref = prefs_find_preference(prefs_find_module("sctp"),"statistics_chunk_types"); if (!pref) { - g_log(NULL, G_LOG_LEVEL_ERROR, "Can't find preference sctp/statistics_chunk_types"); + ws_log(LOG_DOMAIN_QTUI, LOG_LEVEL_ERROR, "Can't find preference sctp/statistics_chunk_types"); return; } @@ -290,7 +291,7 @@ void SCTPChunkStatisticsDialog::on_actionChunkTypePreferences_triggered() pref_t *pref = prefs_find_preference(prefs_find_module("sctp"),"statistics_chunk_types"); if (!pref) { - g_log(NULL, G_LOG_LEVEL_ERROR, "Can't find preference sctp/statistics_chunk_types"); + ws_log(LOG_DOMAIN_QTUI, LOG_LEVEL_ERROR, "Can't find preference sctp/statistics_chunk_types"); return; } @@ -299,7 +300,7 @@ void SCTPChunkStatisticsDialog::on_actionChunkTypePreferences_triggered() if (!uat_load(uat, NULL, &err)) { /* XXX - report this through the GUI */ - g_log(NULL, G_LOG_LEVEL_WARNING, "Error loading table '%s': %s", uat->name, err); + ws_log(LOG_DOMAIN_QTUI, LOG_LEVEL_WARNING, "Error loading table '%s': %s", uat->name, err); g_free(err); } diff --git a/ui/qt/simple_dialog.cpp b/ui/qt/simple_dialog.cpp index b1a08bc791..5e57a7bc03 100644 --- a/ui/qt/simple_dialog.cpp +++ b/ui/qt/simple_dialog.cpp @@ -9,7 +9,6 @@ #include "simple_dialog.h" -#include "log.h" #include "file.h" #include "epan/strutil.h" @@ -18,6 +17,7 @@ #include "ui/commandline.h" #include +#include #include #include "wireshark_application.h" @@ -63,7 +63,7 @@ static void visible_message_finished(QMessageBox *box, int result _U_) visible_messages_mutex.lock(); for (int i = 0; i < visible_messages.size(); i++) { if (visible_messages[i].box == box) { - g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_WARNING, "%d duplicates of \"%s\" were suppressed", + ws_log(LOG_DOMAIN_MAIN, LOG_LEVEL_WARNING, "%d duplicates of \"%s\" were suppressed", visible_messages[i].counter, box->text().toStdString().c_str()); visible_messages.removeAt(i); break; diff --git a/ui/qt/traffic_table_dialog.cpp b/ui/qt/traffic_table_dialog.cpp index c1d7883036..0f6ab018aa 100644 --- a/ui/qt/traffic_table_dialog.cpp +++ b/ui/qt/traffic_table_dialog.cpp @@ -7,7 +7,6 @@ * SPDX-License-Identifier: GPL-2.0-or-later */ -#include "log.h" #include "traffic_table_dialog.h" #include diff --git a/ui/qt/widgets/wireless_timeline.cpp b/ui/qt/widgets/wireless_timeline.cpp index 732568b2bc..eef8b8562e 100644 --- a/ui/qt/widgets/wireless_timeline.cpp +++ b/ui/qt/widgets/wireless_timeline.cpp @@ -23,7 +23,6 @@ #include #include "globals.h" -#include "../../log.h" #include #include diff --git a/ui/qt/wireshark_application.cpp b/ui/qt/wireshark_application.cpp index 6c5662ced1..676b598a23 100644 --- a/ui/qt/wireshark_application.cpp +++ b/ui/qt/wireshark_application.cpp @@ -43,7 +43,6 @@ #include "coloring_rules_dialog.h" #include "epan/color_filters.h" -#include "log.h" #include "recent_file_status.h" #include "extcap.h" diff --git a/ui/tap-tcp-stream.c b/ui/tap-tcp-stream.c index e9d0bef7a1..d838c3a8de 100644 --- a/ui/tap-tcp-stream.c +++ b/ui/tap-tcp-stream.c @@ -105,8 +105,6 @@ graph_segment_list_get(capture_file *cf, struct tcp_graph *tg) GString *error_string; tcp_scan_t ts; - g_log(NULL, G_LOG_LEVEL_DEBUG, "graph_segment_list_get()"); - if (!cf || !tg) { return; } diff --git a/ui/win32/console_win32.c b/ui/win32/console_win32.c index 22b7d10a7d..744791fe7d 100644 --- a/ui/win32/console_win32.c +++ b/ui/win32/console_win32.c @@ -93,18 +93,6 @@ needs_redirection(int std_handle) return FALSE; } -/* The code to create and desstroy console windows should not be necessary, - at least as I read the GLib source code, as it looks as if GLib is, on - Win32, *supposed* to create a console window into which to display its - output. - - That doesn't happen, however. I suspect there's something completely - broken about that code in GLib-for-Win32, and that it may be related - to the breakage that forces us to just call "printf()" on the message - rather than passing the message on to "g_log_default_handler()" - (which is the routine that does the aforementioned non-functional - console window creation). */ - /* * If this application has no console window to which its standard output * would go, create one. diff --git a/wiretap/ipfix.c b/wiretap/ipfix.c index 64740f4cd0..020e9895f4 100644 --- a/wiretap/ipfix.c +++ b/wiretap/ipfix.c @@ -48,7 +48,7 @@ #include "config.h" -#define G_LOG_DOMAIN "wiretap" +#define WS_LOG_DOMAIN LOG_DOMAIN_WIRETAP #include #include diff --git a/wiretap/merge.c b/wiretap/merge.c index 7bca49627e..824c7cf6b2 100644 --- a/wiretap/merge.c +++ b/wiretap/merge.c @@ -14,7 +14,7 @@ #include "config.h" -#define G_LOG_DOMAIN "wiretap" +#define WS_LOG_DOMAIN LOG_DOMAIN_WIRETAP #include #include diff --git a/wiretap/pcapng.c b/wiretap/pcapng.c index 923ad242fe..4918bdf65f 100644 --- a/wiretap/pcapng.c +++ b/wiretap/pcapng.c @@ -17,7 +17,7 @@ #include "config.h" -#define G_LOG_DOMAIN "wiretap" +#define WS_LOG_DOMAIN LOG_DOMAIN_WIRETAP #include #include diff --git a/ws_log_domains.h b/ws_log_domains.h new file mode 100644 index 0000000000..29ad446402 --- /dev/null +++ b/ws_log_domains.h @@ -0,0 +1,44 @@ +/* ws_log_domains.h + * log domain definitions + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef __WS_LOG_DOMAINS_H__ +#define __WS_LOG_DOMAINS_H__ + +enum ws_log_domain { + /* Default domain */ + LOG_DOMAIN_DEFAULT, + /* Main execution domain (wireshark, tshark, etc) */ + LOG_DOMAIN_MAIN, + /* Capture domain (except for capture child, see below) */ + LOG_DOMAIN_CAPTURE, + /* Capture child domain (the capture child might also contain + * file domain messages!) */ + LOG_DOMAIN_CAPCHILD, + LOG_DOMAIN_WIRETAP, + LOG_DOMAIN_EPAN, + LOG_DOMAIN_WSUTIL, + LOG_DOMAIN_QTUI, + _LOG_DOMAIN_LAST +}; + +#endif /* __WS_LOG_DOMAINS_H__ */ + +/* + * Editor modelines - https://www.wireshark.org/tools/modelines.html + * + * Local variables: + * c-basic-offset: 4 + * tab-width: 8 + * indent-tabs-mode: nil + * End: + * + * vi: set shiftwidth=4 tabstop=8 expandtab: + * :indentSize=4:tabSize=8:noTabs=true: + */ diff --git a/wsutil/rsa.c b/wsutil/rsa.c index 740173db59..632d36c908 100644 --- a/wsutil/rsa.c +++ b/wsutil/rsa.c @@ -10,13 +10,14 @@ */ #include "config.h" +#define WS_LOG_DOMAIN LOG_DOMAIN_WSUTIL #include "rsa.h" #include #include "filesystem.h" #include "file_util.h" -#include "log.h" #include +#include #ifdef HAVE_LIBGNUTLS @@ -229,7 +230,7 @@ rsa_load_pkcs12(FILE *fp, const gchar *cert_passwd, char **err) return NULL; } - g_log(NULL, G_LOG_LEVEL_INFO, "rsa_privkey_to_sexp: PKCS#12 imported\n"); + ws_debug("grsa_privkey_to_sexp: PKCS#12 imported"); /* TODO: Use gnutls_pkcs12_simple_parse, since 3.1.0 (August 2012) */ for (i=0; ; i++) { @@ -263,7 +264,7 @@ rsa_load_pkcs12(FILE *fp, const gchar *cert_passwd, char **err) ret); goto done; } - g_log(NULL, G_LOG_LEVEL_INFO, "Bag %d/%d: %s\n", i, j, BAGTYPE(bag_type)); + ws_debug("Bag %d/%d: %s", i, j, BAGTYPE(bag_type)); if (bag_type == GNUTLS_BAG_ENCRYPTED) { ret = gnutls_pkcs12_bag_decrypt(bag, cert_passwd); if (ret == 0) { @@ -279,7 +280,7 @@ rsa_load_pkcs12(FILE *fp, const gchar *cert_passwd, char **err) ret); goto done; } - g_log(NULL, G_LOG_LEVEL_INFO, "Bag %d/%d decrypted: %s\n", i, j, BAGTYPE(bag_type)); + ws_debug("Bag %d/%d decrypted: %s", i, j, BAGTYPE(bag_type)); } } diff --git a/wsutil/win32-utils.c b/wsutil/win32-utils.c index c8917c694e..5973682a7b 100644 --- a/wsutil/win32-utils.c +++ b/wsutil/win32-utils.c @@ -12,11 +12,11 @@ #include "win32-utils.h" -#include - #include #include +#include + /* Quote the argument element if necessary, so that it will get * reconstructed correctly in the C runtime startup code. Note that * the unquoting algorithm in the C runtime is really weird, and @@ -199,7 +199,7 @@ static void win32_kill_child_on_exit(HANDLE child_handle) { cjo_handle = CreateJobObject(NULL, NULL); if (!cjo_handle) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Could not create child cleanup job object: %s", + ws_log(LOG_DOMAIN_CAPTURE, LOG_LEVEL_DEBUG, "Could not create child cleanup job object: %s", win32strerror(GetLastError())); return; } @@ -209,14 +209,14 @@ static void win32_kill_child_on_exit(HANDLE child_handle) { BOOL sijo_ret = SetInformationJobObject(cjo_handle, JobObjectExtendedLimitInformation, &cjo_jel_info, sizeof(cjo_jel_info)); if (!sijo_ret) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Could not set child cleanup limits: %s", + ws_log(LOG_DOMAIN_CAPTURE, LOG_LEVEL_DEBUG, "Could not set child cleanup limits: %s", win32strerror(GetLastError())); } } BOOL aptjo_ret = AssignProcessToJobObject(cjo_handle, child_handle); if (!aptjo_ret) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Could not assign child cleanup process: %s", + ws_log(LOG_DOMAIN_CAPTURE, LOG_LEVEL_DEBUG, "Could not assign child cleanup process: %s", win32strerror(GetLastError())); } } diff --git a/wsutil/ws_pipe.c b/wsutil/ws_pipe.c index 0a7d26f231..500859e2a4 100644 --- a/wsutil/ws_pipe.c +++ b/wsutil/ws_pipe.c @@ -10,6 +10,7 @@ */ #include +#define WS_LOG_DOMAIN LOG_DOMAIN_CAPTURE #include #include @@ -28,7 +29,6 @@ #endif #include -#include #ifdef __linux__ #define HAS_G_SPAWN_LINUX_THREAD_SAFETY_BUG @@ -39,6 +39,7 @@ #include "wsutil/filesystem.h" #include "wsutil/ws_pipe.h" +#include "wsutil/wslog.h" #ifdef HAS_G_SPAWN_LINUX_THREAD_SAFETY_BUG struct linux_dirent64 { @@ -175,7 +176,7 @@ convert_to_argv(const char *command, int args_count, char *const *args) // arguments would silently be ignored because protect_arg returns an // empty string, therefore we print a warning here. if (!*args[i]) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_WARNING, "Empty argument %d in arguments list", i); + ws_warning("Empty argument %d in arguments list", i); } argv[1 + i] = g_strdup(args[i]); } @@ -252,7 +253,7 @@ gboolean ws_pipe_spawn_sync(const gchar *working_directory, const gchar *command gchar **argv = convert_to_argv(command, argc, args); gchar *command_line = convert_to_command_line(argv); - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "spawn_sync: %s", command_line); + ws_debug("command line: %s", command_line); guint64 start_time = g_get_monotonic_time(); @@ -265,7 +266,7 @@ gboolean ws_pipe_spawn_sync(const gchar *working_directory, const gchar *command { g_free(command_line); g_strfreev(argv); - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Could not create stdout overlapped event"); + ws_debug("Could not create stdout overlapped event"); return FALSE; } stderr_overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); @@ -274,7 +275,7 @@ gboolean ws_pipe_spawn_sync(const gchar *working_directory, const gchar *command CloseHandle(stdout_overlapped.hEvent); g_free(command_line); g_strfreev(argv); - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Could not create stderr overlapped event"); + ws_debug("Could not create stderr overlapped event"); return FALSE; } @@ -289,7 +290,7 @@ gboolean ws_pipe_spawn_sync(const gchar *working_directory, const gchar *command CloseHandle(stderr_overlapped.hEvent); g_free(command_line); g_strfreev(argv); - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Could not create stdout handle"); + ws_debug("Could not create stdout handle"); return FALSE; } @@ -301,7 +302,7 @@ gboolean ws_pipe_spawn_sync(const gchar *working_directory, const gchar *command CloseHandle(child_stdout_wr); g_free(command_line); g_strfreev(argv); - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Could not create stderr handle"); + ws_debug("Could not create stderr handle"); return FALSE; } @@ -330,7 +331,7 @@ gboolean ws_pipe_spawn_sync(const gchar *working_directory, const gchar *command { if (GetLastError() != ERROR_IO_PENDING) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "ReadFile on child stdout pipe failed. Error %d", GetLastError()); + ws_debug("ReadFile on child stdout pipe failed. Error %d", GetLastError()); pending_stdout = FALSE; } } @@ -339,7 +340,7 @@ gboolean ws_pipe_spawn_sync(const gchar *working_directory, const gchar *command { if (GetLastError() != ERROR_IO_PENDING) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "ReadFile on child stderr pipe failed. Error %d", GetLastError()); + ws_debug("ReadFile on child stderr pipe failed. Error %d", GetLastError()); pending_stderr = FALSE; } } @@ -390,7 +391,7 @@ gboolean ws_pipe_spawn_sync(const gchar *working_directory, const gchar *command pending_stdout = FALSE; continue; } - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "GetOverlappedResult on stdout failed. Error %d", GetLastError()); + ws_debug("GetOverlappedResult on stdout failed. Error %d", GetLastError()); } if (process_finished && (bytes_read == 0)) { @@ -403,7 +404,7 @@ gboolean ws_pipe_spawn_sync(const gchar *working_directory, const gchar *command { if (GetLastError() != ERROR_IO_PENDING) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "ReadFile on child stdout pipe failed. Error %d", GetLastError()); + ws_debug("ReadFile on child stdout pipe failed. Error %d", GetLastError()); pending_stdout = FALSE; } } @@ -419,7 +420,7 @@ gboolean ws_pipe_spawn_sync(const gchar *working_directory, const gchar *command pending_stderr = FALSE; continue; } - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "GetOverlappedResult on stderr failed. Error %d", GetLastError()); + ws_debug("GetOverlappedResult on stderr failed. Error %d", GetLastError()); } if (process_finished && (bytes_read == 0)) { @@ -430,7 +431,7 @@ gboolean ws_pipe_spawn_sync(const gchar *working_directory, const gchar *command { if (GetLastError() != ERROR_IO_PENDING) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "ReadFile on child stderr pipe failed. Error %d", GetLastError()); + ws_debug("ReadFile on child stderr pipe failed. Error %d", GetLastError()); pending_stderr = FALSE; } } @@ -438,7 +439,7 @@ gboolean ws_pipe_spawn_sync(const gchar *working_directory, const gchar *command } else { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "WaitForMultipleObjects returned 0x%08X. Error %d", dw, GetLastError()); + ws_debug("WaitForMultipleObjects returned 0x%08X. Error %d", dw, GetLastError()); } } @@ -486,12 +487,12 @@ gboolean ws_pipe_spawn_sync(const gchar *working_directory, const gchar *command status = FALSE; #endif - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "%s finished in %.3fms", argv[0], (g_get_monotonic_time() - start_time) / 1000.0); + ws_debug("%s finished in %.3fms", argv[0], (g_get_monotonic_time() - start_time) / 1000.0); if (status) { if (local_output != NULL) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "spawn output: %s", local_output); + ws_debug("spawn output: %s", local_output); if (command_output != NULL) *command_output = g_strdup(local_output); } @@ -534,7 +535,7 @@ GPid ws_pipe_spawn_async(ws_pipe_t *ws_pipe, GPtrArray *args) gchar **argv = convert_to_argv(args_array[0], args->len - 2, args_array + 1); gchar *command_line = convert_to_command_line(argv); - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "spawn_async: %s", command_line); + ws_debug("command line: %s", command_line); #ifdef _WIN32 sa.nLength = sizeof(SECURITY_ATTRIBUTES); @@ -545,7 +546,7 @@ GPid ws_pipe_spawn_async(ws_pipe_t *ws_pipe, GPtrArray *args) { g_free(command_line); g_strfreev(argv); - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Could not create stdin handle"); + ws_debug("Could not create stdin handle"); return WS_INVALID_PID; } @@ -555,7 +556,7 @@ GPid ws_pipe_spawn_async(ws_pipe_t *ws_pipe, GPtrArray *args) CloseHandle(child_stdin_wr); g_free(command_line); g_strfreev(argv); - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Could not create stdout handle"); + ws_debug("Could not create stdout handle"); return WS_INVALID_PID; } @@ -567,7 +568,7 @@ GPid ws_pipe_spawn_async(ws_pipe_t *ws_pipe, GPtrArray *args) CloseHandle(child_stdout_wr); g_free(command_line); g_strfreev(argv); - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Could not create stderr handle"); + ws_debug("Could not create stderr handle"); return WS_INVALID_PID; } @@ -602,7 +603,7 @@ GPid ws_pipe_spawn_async(ws_pipe_t *ws_pipe, GPtrArray *args) flags, child_setup, NULL, &pid, &ws_pipe->stdin_fd, &ws_pipe->stdout_fd, &ws_pipe->stderr_fd, &error); if (!spawned) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Error creating async pipe: %s", error->message); + ws_debug("Error creating async pipe: %s", error->message); g_free(error->message); } #endif @@ -646,7 +647,7 @@ ws_pipe_wait_for_pipe(HANDLE * pipe_handles, int num_pipe_handles, HANDLE pid) if (num_pipe_handles == 0 || num_pipe_handles > 3) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Invalid number of pipes given as argument."); + ws_debug("Invalid number of pipes given as argument."); return FALSE; } @@ -655,7 +656,7 @@ ws_pipe_wait_for_pipe(HANDLE * pipe_handles, int num_pipe_handles, HANDLE pid) pipeinsts[i].ol.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); if (!pipeinsts[i].ol.hEvent) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Could not create overlapped event"); + ws_debug("Could not create overlapped event"); for (int j = 0; j < i; j++) { CloseHandle(pipeinsts[j].ol.hEvent); @@ -683,7 +684,7 @@ ws_pipe_wait_for_pipe(HANDLE * pipe_handles, int num_pipe_handles, HANDLE pid) break; default: - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "ConnectNamedPipe failed with %d\n.", error); + ws_debug("ConnectNamedPipe failed with %d\n.", error); result = FALSE; } } @@ -714,7 +715,7 @@ ws_pipe_wait_for_pipe(HANDLE * pipe_handles, int num_pipe_handles, HANDLE pid) int handle_idx = dw - WAIT_OBJECT_0; if (dw == WAIT_TIMEOUT) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "extcap didn't connect to pipe within 30 seconds."); + ws_debug("extcap didn't connect to pipe within 30 seconds."); result = FALSE; break; } @@ -723,7 +724,7 @@ ws_pipe_wait_for_pipe(HANDLE * pipe_handles, int num_pipe_handles, HANDLE pid) { if (handles[handle_idx] == pid) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "extcap terminated without connecting to pipe."); + ws_debug("extcap terminated without connecting to pipe."); result = FALSE; } for (int i = 0; i < num_pipe_handles; ++i) @@ -738,7 +739,7 @@ ws_pipe_wait_for_pipe(HANDLE * pipe_handles, int num_pipe_handles, HANDLE pid) TRUE); // wait if (!success) { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Error %d \n.", GetLastError()); + ws_debug("Error %d \n.", GetLastError()); result = FALSE; } pipeinsts[i].pendingIO = FALSE; @@ -747,7 +748,7 @@ ws_pipe_wait_for_pipe(HANDLE * pipe_handles, int num_pipe_handles, HANDLE pid) } else { - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "WaitForMultipleObjects returned 0x%08X. Error %d", dw, GetLastError()); + ws_debug("WaitForMultipleObjects returned 0x%08X. Error %d", dw, GetLastError()); result = FALSE; } } @@ -836,7 +837,7 @@ ws_read_string_from_pipe(ws_pipe_handle read_pipe, gchar *buffer, if (buffer_bytes_remaining == 0) { /* The string won't fit in the buffer. */ - g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Buffer too small (%zd).", buffer_size); + ws_debug("Buffer too small (%zd).", buffer_size); break; } diff --git a/wsutil/wsjson.c b/wsutil/wsjson.c index 567fa2dd5c..474c9246b6 100644 --- a/wsutil/wsjson.c +++ b/wsutil/wsjson.c @@ -11,6 +11,7 @@ */ #include "config.h" +#define WS_LOG_DOMAIN LOG_DOMAIN_MAIN #include "wsjson.h" @@ -19,7 +20,7 @@ #include #include #include -#include "log.h" +#include gboolean json_validate(const guint8 *buf, const size_t len) @@ -39,11 +40,11 @@ json_validate(const guint8 *buf, const size_t len) * XXX - should we check for NULs anywhere in the buffer? */ if (len == 0) { - g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "jsmn: JSON string is empty"); + ws_debug("JSON string is empty"); return FALSE; } if (buf[0] == '\0') { - g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "jsmn: invalid character inside JSON string"); + ws_debug("invalid character inside JSON string"); return FALSE; } @@ -57,17 +58,17 @@ json_validate(const guint8 *buf, const size_t len) if (rcode < 0) { switch (rcode) { case JSMN_ERROR_NOMEM: - g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "jsmn: not enough tokens were provided"); + ws_debug("not enough tokens were provided"); break; case JSMN_ERROR_INVAL: - g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "jsmn: invalid character inside JSON string"); + ws_debug("invalid character inside JSON string"); break; case JSMN_ERROR_PART: - g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "jsmn: the string is not a full JSON packet, " + ws_debug("the string is not a full JSON packet, " "more bytes expected"); break; default: - g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "jsmn: unexpected error"); + ws_debug("unexpected error"); break; } ret = FALSE; diff --git a/wsutil/wslog.c b/wsutil/wslog.c index 1d282ca621..ee93565437 100644 --- a/wsutil/wslog.c +++ b/wsutil/wslog.c @@ -9,21 +9,378 @@ #include "config.h" #include "wslog.h" -#include +#include +#include +#include #include +#include + +#include +#include #define LOGBUFSIZE 256 -void ws_log_full(const char *log_domain, GLogLevelFlags log_level, +#define LOGENVVAR "WS_LOG_LEVEL" + + +/* TODO: Add filtering by domain. */ + +static enum ws_log_level current_log_level = LOG_LEVEL_MESSAGE; + +static ws_log_writer_t *current_log_writer = ws_log_default_writer; + +static void *current_log_writer_data = NULL; + +static ws_log_writer_free_data_t *current_log_writer_data_free = NULL; + +static FILE *custom_log = NULL; + + +static void ws_log_cleanup(void); + + +static void +log_default_writer_do_work(FILE *fp, const char *message) +{ + ws_assert(message); + fputs(message, fp); + fputc('\n', fp); + fflush(fp); +} + + +void +ws_log_default_writer(const char *message, + enum ws_log_domain domain _U_, + enum ws_log_level level _U_, + void *user_data _U_) +{ + log_default_writer_do_work(stderr, message); +} + + +const char *ws_log_level_to_string(enum ws_log_level level) +{ + switch (level) { + case LOG_LEVEL_NONE: + return "(none)"; + case LOG_LEVEL_ERROR: + return "ERROR"; + case LOG_LEVEL_CRITICAL: + return "CRITICAL"; + case LOG_LEVEL_WARNING: + return "WARNING"; + case LOG_LEVEL_MESSAGE: + return "MESSAGE"; + case LOG_LEVEL_INFO: + return "INFO"; + case LOG_LEVEL_DEBUG: + return "DEBUG"; + default: + return "(BOGUS LOG LEVEL)"; + } +} + + +const char *ws_log_domain_to_string(enum ws_log_domain domain) +{ + switch (domain) { + case LOG_DOMAIN_DEFAULT: + return "Default"; + case LOG_DOMAIN_MAIN: + return "Main"; + case LOG_DOMAIN_CAPTURE: + return "Capture"; + case LOG_DOMAIN_CAPCHILD: + return "CapChild"; + case LOG_DOMAIN_WIRETAP: + return "Wiretap"; + case LOG_DOMAIN_EPAN: + return "Epan"; + case LOG_DOMAIN_WSUTIL: + return "Util"; + case LOG_DOMAIN_QTUI: + return "GUI"; + default: + return "(BOGUS LOG DOMAIN)"; + } +} + + +gboolean ws_log_level_is_active(enum ws_log_level level) +{ + return level <= current_log_level; +} + + +enum ws_log_level ws_log_get_level(void) +{ + return current_log_level; +} + + +enum ws_log_level ws_log_set_level(enum ws_log_level log_level) +{ + ws_assert(log_level > LOG_LEVEL_NONE && log_level < _LOG_LEVEL_LAST); + + current_log_level = log_level; + return current_log_level; +} + + +enum ws_log_level ws_log_set_level_str(const char *str_level) +{ + enum ws_log_level level; + + if (!str_level) + return LOG_LEVEL_NONE; + + if (g_ascii_strcasecmp(str_level, "debug") == 0) + level = LOG_LEVEL_DEBUG; + else if (g_ascii_strcasecmp(str_level, "info") == 0) + level = LOG_LEVEL_INFO; + else if (g_ascii_strcasecmp(str_level, "message") == 0) + level = LOG_LEVEL_MESSAGE; + else if (g_ascii_strcasecmp(str_level, "warning") == 0) + level = LOG_LEVEL_WARNING; + else if (g_ascii_strcasecmp(str_level, "critical") == 0) + level = LOG_LEVEL_CRITICAL; + else if (g_ascii_strcasecmp(str_level, "error") == 0) + level = LOG_LEVEL_ERROR; + else + return LOG_LEVEL_NONE; + + current_log_level = level; + return current_log_level; +} + + +static const char *set_level_and_prune_argv(int count, char **ptr, int prune_extra, + const char *optarg, int *ret_argc) +{ + if (optarg && ws_log_set_level_str(optarg) != LOG_LEVEL_NONE) + optarg = NULL; /* success */ + + /* + * We found a "--log-level" option. We will remove it from + * the argv by moving up the other strings in the array. This is + * so that it doesn't generate an unrecognized option + * error further along in the initialization process. + */ + + /* Include the terminating NULL in the memmove. */ + memmove(ptr, ptr + 1 + prune_extra, (count - prune_extra) * sizeof(*ptr)); + *ret_argc -= (1 + prune_extra); + return optarg; +} + +const char *ws_log_set_level_args(int *argc_ptr, char *argv[]) +{ + char **p; + int c; + const char *opt = "--log-level"; + size_t len = strlen(opt); + const char *optarg; + + for (p = argv, c = *argc_ptr; *p != NULL; p++, c--) { + if (strncmp(*p, opt, len) == 0) { + optarg = *p + len; + /* Two possibilities: + * --log_level + * or + * --log-level= + */ + if (optarg[0] == '\0') { + /* value is separated with blank space */ + optarg = *(p + 1); + + /* If the option value after the blank is missing or stars with '-' just ignore it. + * But we should probably signal an error (missing required value). */ + if (optarg == NULL || !*optarg || *optarg == '-') { + return set_level_and_prune_argv(c, p, 0, NULL, argc_ptr); + } + return set_level_and_prune_argv(c, p, 1, optarg, argc_ptr); + } + else if (optarg[0] == '=') { + /* value is after equals */ + optarg += 1; + return set_level_and_prune_argv(c, p, 0, optarg, argc_ptr); + } + /* we didn't find what we want */ + } + } + return NULL; /* No log-level option, ignore and return success. */ +} + + +void ws_log_init(ws_log_writer_t *_writer) +{ + if (_writer) { + current_log_writer = _writer; + } + + const char *env = g_getenv(LOGENVVAR); + if (env && ws_log_set_level_str(env) == LOG_LEVEL_NONE) { + fprintf(stderr, "Ignoring invalid environment value %s=\"%s\"\n", LOGENVVAR, env); + } + + atexit(ws_log_cleanup); +} + + +void ws_log_init_with_data(ws_log_writer_t *writer, void *user_data, + ws_log_writer_free_data_t *free_user_data) +{ + current_log_writer_data = user_data; + current_log_writer_data_free = free_user_data; + ws_log_init(writer); +} + + +static inline const char *_level_to_string(enum ws_log_level level) +{ + switch (level) { + case LOG_LEVEL_NONE: return "NUL"; + case LOG_LEVEL_ERROR: return "ERR"; + case LOG_LEVEL_CRITICAL: return "CRI"; + case LOG_LEVEL_WARNING: return "WRN"; + case LOG_LEVEL_MESSAGE: return "MSG"; + case LOG_LEVEL_INFO: return "NFO"; + case LOG_LEVEL_DEBUG: return "DBG"; + default: + return "(BOGUS LOG LEVEL)"; + } +} + + +static inline const char *_domain_to_string(enum ws_log_domain domain) +{ + switch (domain) { + case LOG_DOMAIN_DEFAULT: return "Dflt"; + case LOG_DOMAIN_MAIN: return "Main"; + case LOG_DOMAIN_CAPTURE: return "Capt"; + case LOG_DOMAIN_CAPCHILD: return "CChd"; + case LOG_DOMAIN_WIRETAP: return "Wtap"; + case LOG_DOMAIN_EPAN: return "Epan"; + case LOG_DOMAIN_WSUTIL: return "Util"; + case LOG_DOMAIN_QTUI: return "Qtui"; + default: + return "(BOGUS LOG DOMAIN)"; + } +} + + +static void ws_log_writev(enum ws_log_domain domain, enum ws_log_level level, + const char *location, const char *format, va_list ap) +{ + char timestamp[sizeof("00:00:00.000")]; + char user_string[LOGBUFSIZE]; + char message[LOGBUFSIZE*2]; + time_t curr; + struct tm *today; + + /* create a "timestamp" */ + time(&curr); + today = localtime(&curr); + guint64 microseconds = create_timestamp(); + if (today != NULL) { + snprintf(timestamp, sizeof(timestamp), "%02d:%02d:%02d.%03" G_GUINT64_FORMAT, + today->tm_hour, today->tm_min, today->tm_sec, + microseconds % 1000000 / 1000); + } + else { + snprintf(timestamp, sizeof(timestamp), "(notime)"); + } + + vsnprintf(user_string, sizeof(user_string), format, ap); + + snprintf(message, sizeof(message), "%s %s-%s %s : %s", + timestamp, + _domain_to_string(domain), + _level_to_string(level), + location ? location : "(nofile)", + user_string); + + /* Call the registered writer, or the default if one wasn't registered. */ + current_log_writer(message, domain, level, current_log_writer_data); + + /* If we have a custom file, write to it _also_. */ + if (custom_log) { + log_default_writer_do_work(custom_log, message); + } + + if (level == LOG_LEVEL_ERROR) { + G_BREAKPOINT(); + ws_assert_not_reached(); + } +} + + +void ws_logv(enum ws_log_domain domain, enum ws_log_level level, + const char *format, va_list ap) +{ + if (!ws_log_level_is_active(level)) + return; + + ws_log_writev(domain, level, NULL, format, ap); +} + + +void ws_log(enum ws_log_domain domain, enum ws_log_level level, + const char *format, ...) +{ + va_list ap; + + if (!ws_log_level_is_active(level)) + return; + + va_start(ap, format); + ws_log_writev(domain, level, NULL, format, ap); + va_end(ap); +} + + +void ws_log_full(enum ws_log_domain domain, enum ws_log_level level, const char *file, int line, const char *func, const char *format, ...) { va_list ap; - char log_msg[LOGBUFSIZE]; + char location[LOGBUFSIZE]; + + if (!ws_log_level_is_active(level)) + return; + + if (func) + snprintf(location, sizeof(location), "%s(%d) %s()", file, line, func); + else + snprintf(location, sizeof(location), "%s(%d)", file, line); va_start(ap, format); - vsnprintf(log_msg, sizeof(log_msg), format, ap); + ws_log_writev(domain, level, location, format, ap); va_end(ap); - - g_log(log_domain, log_level, "%s(%d):%s: %s", file, line, func, log_msg); +} + + +static void ws_log_cleanup(void) +{ + if (current_log_writer_data_free) { + current_log_writer_data_free(current_log_writer_data); + current_log_writer_data = NULL; + } + if (custom_log) { + fclose(custom_log); + custom_log = NULL; + } +} + + +void ws_log_add_custom_file(FILE *fp) +{ + if (custom_log != NULL) { + fclose(custom_log); + custom_log = NULL; + } + if (fp != NULL) { + custom_log = fp; + } } diff --git a/wsutil/wslog.h b/wsutil/wslog.h index 3629c89bbd..9f9bcc7f7f 100644 --- a/wsutil/wslog.h +++ b/wsutil/wslog.h @@ -11,36 +11,211 @@ #include #include +#include +#include +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ /* - * XXX This API should be consolidated with ui/console.h. The way this works - * (or doesn't) with prefs->console_log_level is also weird, because not all - * log domains are using the same log handler. The function ws_log_full() - * currently ignores the console log level preference. + * Descending order by priority needs to be maintained. Higher priorities have + * lower values. */ +enum ws_log_level { + LOG_LEVEL_NONE, /* not user facing */ + LOG_LEVEL_ERROR, /* "error" is always fatal (aborts) */ + LOG_LEVEL_CRITICAL, + LOG_LEVEL_WARNING, + LOG_LEVEL_MESSAGE, + LOG_LEVEL_INFO, + LOG_LEVEL_DEBUG, + _LOG_LEVEL_LAST +}; + +#include + +#ifndef WS_LOG_DOMAIN +#define WS_LOG_DOMAIN LOG_DOMAIN_DEFAULT +#endif + + +/** Signature for registering a log writer. */ +typedef void (ws_log_writer_t)(const char *message, + enum ws_log_domain domain, + enum ws_log_level level, + void *user_data); + + +typedef void (ws_log_writer_free_data_t)(void *user_data); + WS_DLL_PUBLIC -void ws_log_full(const char *log_domain, GLogLevelFlags log_level, +void ws_log_default_writer(const char *message, + enum ws_log_domain domain, + enum ws_log_level level, + void *user_data); + + +/** Convert a numerical level to its string representation. */ +WS_DLL_PUBLIC +const char *ws_log_level_to_string(enum ws_log_level level); + + +/** Convert a numerical domain to its string representation. */ +WS_DLL_PUBLIC +const char *ws_log_domain_to_string(enum ws_log_domain domain); + + +/** 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. */ +WS_DLL_PUBLIC +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); + + +/** Set the active log level from an argv vector. + * + * Will search the arv for the option parameter "--log-level=". + * If it finds the parameter and the string is valid sets the log level and + * returns NULL (success). Othwerise returns the invalid option argument after '='. + */ +WS_DLL_PUBLIC +const char *ws_log_set_level_args(int *argcp, char **argv); + + +/** 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 +void ws_log_init(ws_log_writer_t *writer); + + +/** 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 +void ws_log_init_with_data(ws_log_writer_t *writer, void *user_data, + ws_log_writer_free_data_t *free_user_data); + + +/** This function is called to output a message to the log. + * + * Takes a format string and a variable number of arguments. + */ +WS_DLL_PUBLIC +void ws_log(enum ws_log_domain domain, enum ws_log_level level, + const char *format, ...) G_GNUC_PRINTF(3,4); + +/** This function is called to output a message to the log. + * + * Takes a format string and a 'va_list'. + */ +WS_DLL_PUBLIC +void ws_logv(enum ws_log_domain domain, enum ws_log_level level, + 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 +void ws_log_full(enum ws_log_domain domain, enum ws_log_level level, const char *file, int line, const char *func, const char *format, ...) G_GNUC_PRINTF(6,7); -/* - * To output debug information use the environment variable - * G_MESSAGES_DEBUG=" ..." (separated with spaces) - * to produce output for specic domains, or G_MESSAGES_DEBUG="all" for - * all domains. + +#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__) + +/** Logs with "debug" level. + * + * Accepts a format string and includes the file and function name. */ #ifndef WS_DISABLE_DEBUG -#define ws_debug(...) ws_log_full(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \ - __FILE__, __LINE__, G_STRFUNC, \ - __VA_ARGS__) +#define ws_debug(...) _LOG_FULL(LOG_LEVEL_DEBUG, __VA_ARGS__) #else /* This avoids -Wunused warnings for variables referenced by ws_debug() * only. The compiler will optimize it away. */ -#define ws_debug(...) \ - G_STMT_START { if (0) ws_log_full(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \ - __FILE__, __LINE__, G_STRFUNC, __VA_ARGS__); } G_STMT_END +#define ws_debug(...) \ + G_STMT_START { \ + if (0) _LOG_FULL(LOG_LEVEL_DEBUG, __VA_ARGS__); \ + } G_STMT_END #endif + +/** 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 */ + #endif /* __WSLOG_H__ */