extcap: Use standard --log-level and --log-file CLI options

This should allow simultaneous logging to the console and the log
file when running an extcap from the CLI.

One difference is that the extcap error/warning dialogs in the GUI
have extra information in standard wslog format (may or may not
be a good thing).
This commit is contained in:
João Valverde 2021-12-07 10:39:38 +00:00 committed by Wireshark GitLab Utility
parent c560226f3b
commit 9b0b3c118a
2 changed files with 33 additions and 43 deletions

View File

@ -46,6 +46,8 @@ typedef struct _extcap_option {
static FILE *custom_log = NULL;
static void extcap_init_log_file(const char *filename);
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 );
@ -107,29 +109,6 @@ void extcap_base_set_running_with(extcap_parameters * extcap, const char *fmt, .
va_end(ap);
}
/* This is only active with a debug log file. */
static void extcap_custom_log(const char *domain, enum ws_log_level level,
ws_log_time_t timestamp,
const char *file, int line, const char *func,
const char *user_format, va_list user_ap,
void *user_data _U_)
{
if (!ws_log_msg_is_active(domain, level)) {
return;
}
if (custom_log) {
va_list user_ap_copy;
G_VA_COPY(user_ap_copy, user_ap);
ws_log_file_writer(custom_log, domain, level, timestamp, file, line, func, user_format, user_ap_copy);
va_end(user_ap_copy);
}
if (level > LOG_LEVEL_INFO) {
/* This writes errors and warnings to the parent process. */
vfprintf(stderr, user_format, user_ap);
}
}
void extcap_log_init(const char *progname)
{
ws_log_init(progname, NULL);
@ -138,15 +117,21 @@ void extcap_log_init(const char *progname)
uint8_t extcap_base_parse_options(extcap_parameters * extcap, int result, char * optargument)
{
uint8_t ret = 1;
enum ws_log_level level;
switch (result) {
case EXTCAP_OPT_DEBUG:
extcap->debug = TRUE;
ws_log_set_level(LOG_LEVEL_DEBUG);
case EXTCAP_OPT_LOG_LEVEL:
level = ws_log_set_level_str(optargument);
if (level == LOG_LEVEL_NONE) {
/* Invalid log level string. */
ret = 0;
}
else if (level <= LOG_LEVEL_DEBUG) {
extcap->debug = TRUE;
}
break;
case EXTCAP_OPT_DEBUG_FILE:
extcap_init_custom_log(optargument);
ws_log_set_writer(extcap_custom_log);
case EXTCAP_OPT_LOG_FILE:
extcap_init_log_file(optargument);
break;
case EXTCAP_OPT_LIST_INTERFACES:
extcap->do_list_interfaces = 1;
@ -331,26 +316,32 @@ void extcap_help_add_header(extcap_parameters * extcap, char * help_header)
extcap_help_add_option(extcap, "--extcap-capture-filter <filter>", "the capture filter");
extcap_help_add_option(extcap, "--fifo <file>", "dump data to file or fifo");
extcap_help_add_option(extcap, "--extcap-version", "print tool version");
extcap_help_add_option(extcap, "--debug", "print additional messages");
extcap_help_add_option(extcap, "--debug-file", "print debug messages to file");
extcap_help_add_option(extcap, "--log-level", "Set the log level");
extcap_help_add_option(extcap, "--log-file", "Set a log file to log messages in addition to the console");
}
void extcap_init_custom_log(const char* filename)
static void extcap_init_log_file(const char* filename)
{
if (!filename || strlen(filename) == 0)
return;
ws_error("Missing log file name");
custom_log = fopen(filename, "w");
if (!custom_log)
ws_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)
{
printf("arg {number=%u}{call=--debug}{display=Run in debug mode}"
"{type=boolflag}{default=false}{tooltip=Print debug messages}{required=false}"
"{group=Debug}\n", (*count)++);
printf("arg {number=%u}{call=--debug-file}{display=Use a file for debug}"
"{type=string}{tooltip=Set a file where the debug messages are written}{required=false}"
printf("arg {number=%u}{call=--log-level}{display=Set the log level}"
"{type=selector}{default=message}{tooltip=Set the log level}{required=false}"
"{group=Debug}\n", *count);
printf("value {arg=%u}{value=message}{display=Message}\n", *count);
printf("value {arg=%u}{value=info}{display=Info}\n", *count);
printf("value {arg=%u}{value=debug}{display=Debug}\n", *count);
printf("value {arg=%u}{value=noisy}{display=Noisy}\n", *count);
(*count)++;
printf("arg {number=%u}{call=--log-file}{display=Use a file for logging}"
"{type=fileselect}{tooltip=Set a file where log messages are written}{required=false}"
"{group=Debug}\n", (*count)++);
}

View File

@ -36,8 +36,8 @@
EXTCAP_OPT_CAPTURE, \
EXTCAP_OPT_CAPTURE_FILTER, \
EXTCAP_OPT_FIFO, \
EXTCAP_OPT_DEBUG, \
EXTCAP_OPT_DEBUG_FILE
EXTCAP_OPT_LOG_LEVEL, \
EXTCAP_OPT_LOG_FILE
#define EXTCAP_BASE_OPTIONS \
@ -49,8 +49,8 @@
{ "capture", ws_no_argument, NULL, EXTCAP_OPT_CAPTURE}, \
{ "extcap-capture-filter", ws_required_argument, NULL, EXTCAP_OPT_CAPTURE_FILTER}, \
{ "fifo", ws_required_argument, NULL, EXTCAP_OPT_FIFO}, \
{ "debug", ws_no_argument, NULL, EXTCAP_OPT_DEBUG}, \
{ "debug-file", ws_required_argument, NULL, EXTCAP_OPT_DEBUG_FILE}
{ "log-level", ws_required_argument, NULL, EXTCAP_OPT_LOG_LEVEL}, \
{ "log-file", ws_required_argument, NULL, EXTCAP_OPT_LOG_FILE}
typedef struct _extcap_parameters
{
@ -93,7 +93,6 @@ void extcap_help_add_option(extcap_parameters * extcap, const char * help_option
void extcap_version_print(extcap_parameters * extcap);
void extcap_help_print(extcap_parameters * extcap);
void extcap_cmdline_debug(char** ar, const unsigned n);
void extcap_init_custom_log(const char* filename);
void extcap_config_debug(unsigned* count);
void extcap_base_help(void);
void extcap_log_init(const char *progname);