wslog: Check if we are initialized and add missing inits

Instead of receiving the program name from GLib, pass it explicitly
to ws_log_init() instead  and use that to initialize the GLib program
name.

ws_log_parse_args() will now exit the program when it encounters an
argument error if exit_failure >= 0.
This commit is contained in:
João Valverde 2021-06-19 19:44:58 +01:00 committed by Wireshark GitLab Utility
parent c216bb85e7
commit 759bb234d0
24 changed files with 161 additions and 70 deletions

View File

@ -84,6 +84,7 @@
#include <wsutil/str_util.h>
#include <wsutil/file_util.h>
#include <wsutil/ws_assert.h>
#include <wsutil/wslog.h>
#include <wsutil/wsgcrypt.h>
@ -1580,8 +1581,15 @@ main(int argc, char *argv[])
setlocale(LC_ALL, "");
#endif
/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("capinfos", NULL);
cmdarg_err_init(capinfos_cmdarg_err, capinfos_cmdarg_err_cont);
/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, INVALID_OPTION);
/* Get the decimal point. */
decimal_point = g_strdup(localeconv()->decimal_point);

View File

@ -50,6 +50,7 @@
#include <wsutil/report_message.h>
#include <wsutil/str_util.h>
#include <wsutil/wslog.h>
#include "ui/failure_message.h"
@ -119,8 +120,14 @@ main(int argc, char *argv[])
setlocale(LC_ALL, "");
#endif
/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("captype", NULL);
cmdarg_err_init(captype_cmdarg_err, captype_cmdarg_err_cont);
/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, 1);
/* Initialize the version information. */
ws_init_version_info("Captype (Wireshark)", NULL, NULL, NULL);

View File

@ -29,6 +29,7 @@
#include <wsutil/filesystem.h>
#include <wsutil/privileges.h>
#include <wsutil/report_message.h>
#include <wsutil/wslog.h>
#include <wiretap/wtap.h>
@ -59,8 +60,14 @@ main(int argc, char **argv)
dfilter_t *df;
gchar *err_msg;
/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("dftest", NULL);
cmdarg_err_init(dftest_cmdarg_err, dftest_cmdarg_err_cont);
/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, 1);
/*
* Get credential information for later use.
*/

View File

@ -4851,17 +4851,13 @@ main(int argc, char *argv[])
#endif
GString *str;
g_set_prgname("dumpcap");
/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init(dumpcap_log_writer);
ws_log_init("dumpcap", 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. */
if (ws_log_parse_args(&argc, argv, cmdarg_err) != 0)
exit(1);
/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, 1);
#ifdef _WIN32
create_app_running_mutex();

View File

@ -84,6 +84,7 @@
#include <wsutil/pint.h>
#include <wsutil/strtoi.h>
#include <wsutil/ws_assert.h>
#include <wsutil/wslog.h>
#include <wiretap/wtap_opttypes.h>
#include "ui/failure_message.h"
@ -1153,8 +1154,14 @@ main(int argc, char *argv[])
gboolean valid_seed = FALSE;
unsigned int seed = 0;
/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("editcap", NULL);
cmdarg_err_init(editcap_cmdarg_err, editcap_cmdarg_err_cont);
/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, INVALID_OPTION);
#ifdef _WIN32
create_app_running_mutex();
#endif /* _WIN32 */

View File

@ -2528,17 +2528,13 @@ int main(int argc, char *argv[]) {
char *help_url;
char *help_header = NULL;
g_set_prgname("androiddump");
/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init(NULL);
ws_log_init("androiddump", NULL);
cmdarg_err_init(androiddump_cmdarg_err, androiddump_cmdarg_err);
/* Command line options are parsed too late to configure logging, do it
manually. */
if (ws_log_parse_args(&argc, argv, cmdarg_err) != 0)
return EXIT_FAILURE;
/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, EXIT_FAILURE);
/*
* Get credential information for later use.

View File

@ -534,6 +534,9 @@ 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("ciscodump", NULL);
/*
* Get credential information for later use.
*/

View File

@ -489,6 +489,9 @@ int main(int argc, char *argv[])
extcap_parameters* extcap_conf = g_new0(extcap_parameters, 1);
char* help_header = NULL;
/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("dpauxmon", NULL);
/*
* Get credential information for later use.
*/

View File

@ -122,6 +122,9 @@ 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("etwdump", NULL);
/*
* Get credential information for later use.
*/

View File

@ -151,17 +151,13 @@ int main(int argc, char *argv[])
char* help_url;
char* help_header = NULL;
g_set_prgname("randpktdump");
/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init(NULL);
ws_log_init("randpktdump", NULL);
cmdarg_err_init(randpktdump_cmdarg_err, randpktdump_cmdarg_err);
/* Command line options are parsed too late to configure logging, do it
manually. */
if (ws_log_parse_args(&argc, argv, cmdarg_err) != 0)
return EXIT_FAILURE;
/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, EXIT_FAILURE);
/*
* Get credential information for later use.

View File

@ -341,6 +341,9 @@ 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("sdjournal", NULL);
/*
* Get credential information for later use.
*/

View File

@ -362,6 +362,9 @@ int main(int argc, char *argv[])
gboolean noprom = FALSE;
gchar* interface_description = g_strdup("SSH remote capture");
/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("sshdump", NULL);
sshdump_extcap_interface = g_path_get_basename(argv[0]);
/*

View File

@ -368,6 +368,9 @@ int main(int argc, char *argv[])
char* payload = NULL;
char* port_msg = NULL;
/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("udpdump", NULL);
/*
* Get credential information for later use.
*/

View File

@ -25,6 +25,7 @@
#include <wsutil/filesystem.h>
#include <wsutil/privileges.h>
#include <wsutil/report_message.h>
#include <wsutil/wslog.h>
#include <version_info.h>
#include <wiretap/wtap.h>
@ -234,6 +235,9 @@ fuzz_init(int argc _U_, char **argv)
g_setenv("WIRESHARK_DEBUG_WMEM_OVERRIDE", "simple", 0);
g_setenv("G_SLICE", "always-malloc", 0);
/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("fuzzshark", NULL);
cmdarg_err_init(fuzzshark_cmdarg_err, fuzzshark_cmdarg_err_cont);
/*

View File

@ -42,6 +42,7 @@
#include <wsutil/privileges.h>
#include <wsutil/strnatcmp.h>
#include <wsutil/ws_assert.h>
#include <wsutil/wslog.h>
#include <cli_main.h>
#include <version_info.h>
@ -227,8 +228,14 @@ main(int argc, char *argv[])
idb_merge_mode mode = IDB_MERGE_MODE_MAX;
merge_progress_callback_t cb;
/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("mergecap", NULL);
cmdarg_err_init(mergecap_cmdarg_err, mergecap_cmdarg_err_cont);
/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, 1);
#ifdef _WIN32
create_app_running_mutex();
#endif /* _WIN32 */

View File

@ -29,6 +29,7 @@
#endif
#include <wsutil/report_message.h>
#include <wsutil/wslog.h>
/*
* If we have getopt_long() in the system library, include <getopt.h>.
@ -137,6 +138,14 @@ main(int argc, char *argv[])
{0, 0, 0, 0 }
};
/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("randpkt", NULL);
cmdarg_err_init(randpkt_cmdarg_err, randpkt_cmdarg_err_cont);
/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, INVALID_OPTION);
/*
* Get credential information for later use.
*/
@ -158,8 +167,6 @@ main(int argc, char *argv[])
wtap_init(TRUE);
cmdarg_err_init(randpkt_cmdarg_err, randpkt_cmdarg_err_cont);
#ifdef _WIN32
create_app_running_mutex();
#endif /* _WIN32 */

View File

@ -449,17 +449,13 @@ main(int argc, char *argv[])
setlocale(LC_ALL, "");
#endif
g_set_prgname("rawshark");
/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init(NULL);
ws_log_init("rawshark", NULL);
cmdarg_err_init(rawshark_cmdarg_err, rawshark_cmdarg_err_cont);
/* Command line options are parsed too late to configure logging, do it
manually. */
if (ws_log_parse_args(&argc, argv, cmdarg_err) != 0)
return INVALID_OPTION;
/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, INVALID_OPTION);
/* Initialize the version information. */
ws_init_version_info("Rawshark (Wireshark)", NULL,

View File

@ -44,6 +44,7 @@
#endif
#include <wsutil/report_message.h>
#include <wsutil/wslog.h>
#include "ui/failure_message.h"
@ -207,8 +208,14 @@ main(int argc, char *argv[])
char *infile;
const char *outfile;
/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("reordercap", NULL);
cmdarg_err_init(reordercap_cmdarg_err, reordercap_cmdarg_err_cont);
/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, INVALID_OPTION);
/* Initialize the version information. */
ws_init_version_info("Reordercap (Wireshark)", NULL, NULL, NULL);

View File

@ -114,17 +114,13 @@ main(int argc, char *argv[])
cfile_close_failure_message
};
g_set_prgname("sharkd");
/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init(NULL);
ws_log_init("sharkd", NULL);
cmdarg_err_init(sharkd_cmdarg_err, sharkd_cmdarg_err_cont);
/* Command line options are parsed too late to configure logging, do it
manually. */
if (ws_log_parse_args(&argc, argv, cmdarg_err) != 0)
return INIT_FAILED;
/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, INIT_FAILED);
/*
* Get credential information for later use, and drop privileges

View File

@ -103,6 +103,7 @@
#include <cli_main.h>
#include <version_info.h>
#include <wsutil/inet_addr.h>
#include <wsutil/wslog.h>
#ifdef _WIN32
#include <io.h> /* for _setmode */
@ -1864,6 +1865,9 @@ main(int argc, char *argv[])
{
int ret = EXIT_SUCCESS;
/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("text2pcap", NULL);
#ifdef _WIN32
create_app_running_mutex();
#endif /* _WIN32 */

View File

@ -775,17 +775,13 @@ main(int argc, char *argv[])
setlocale(LC_ALL, "");
#endif
g_set_prgname("tshark");
/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init(NULL);
ws_log_init("tshark", NULL);
cmdarg_err_init(tshark_cmdarg_err, tshark_cmdarg_err_cont);
/* Command line options are parsed too late to configure logging, do it
manually. */
if (ws_log_parse_args(&argc, argv, cmdarg_err) != 0)
return INVALID_OPTION;
/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, INVALID_OPTION);
ws_debug("tshark started with %d args", argc);

View File

@ -529,10 +529,8 @@ int main(int argc, char *qt_argv[])
macos_enable_layer_backing();
#endif
g_set_prgname("wireshark");
/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init(console_log_writer);
ws_log_init("wireshark", console_log_writer);
qInstallMessageHandler(qt_log_message_handler);
@ -582,10 +580,8 @@ 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. */
if (ws_log_parse_args(&argc, argv, cmdarg_err) != 0)
exit_application(INVALID_OPTION);
/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, INVALID_OPTION);
/*
* Get credential information for later use, and drop privileges

View File

@ -46,7 +46,7 @@
#define DEFAULT_LOG_LEVEL LOG_LEVEL_MESSAGE
#define DEFAULT_APPNAME "PID"
#define DEFAULT_PROGNAME "PID"
#define DOMAIN_UNDEFED(domain) ((domain) == NULL || *(domain) == '\0')
#define DOMAIN_DEFINED(domain) (!DOMAIN_UNDEFED(domain))
@ -66,7 +66,7 @@ static enum ws_log_level current_log_level = LOG_LEVEL_NONE;
static gboolean color_enabled = FALSE;
static const char *registered_appname = NULL;
static const char *registered_progname = DEFAULT_PROGNAME;
/* List of domains to filter. */
static log_filter_t *domain_filter = NULL;
@ -87,6 +87,10 @@ static FILE *custom_log = NULL;
static enum ws_log_level fatal_log_level = LOG_LEVEL_ERROR;
#ifndef WS_DISABLE_DEBUG
static gboolean init_complete = FALSE;
#endif
static void ws_log_cleanup(void);
@ -277,7 +281,26 @@ static const char *opt_debug = "--log-debug";
static const char *opt_noisy = "--log-noisy";
int ws_log_parse_args(int *argc_ptr, char *argv[], void (*print_err)(const char *, ...))
static void print_err(void (*log_args_print_err)(const char *, va_list ap),
int log_args_exit_failure,
const char *fmt, ...)
{
va_list ap;
if (log_args_print_err == NULL)
return;
va_start(ap, fmt);
log_args_print_err(fmt, ap);
va_end(ap);
if (log_args_exit_failure >= 0)
exit(log_args_exit_failure);
}
int ws_log_parse_args(int *argc_ptr, char *argv[],
void (*vcmdarg_err)(const char *, va_list ap),
int exit_failure)
{
char **ptr = argv;
int count = *argc_ptr;
@ -331,7 +354,8 @@ int ws_log_parse_args(int *argc_ptr, char *argv[], void (*print_err)(const char
if (value == NULL || !*value || *value == '-') {
/* If the option value after the blank starts with '-' assume
* it is another option. */
print_err("Option \"%s\" requires a value.\n", *ptr);
print_err(vcmdarg_err, exit_failure,
"Option \"%s\" requires a value.\n", *ptr);
option = NULL;
prune_extra = 0;
ret += 1;
@ -351,7 +375,8 @@ int ws_log_parse_args(int *argc_ptr, char *argv[], void (*print_err)(const char
if (option == opt_level) {
if (ws_log_set_level_str(value) == LOG_LEVEL_NONE) {
print_err("Invalid log level \"%s\"\n", value);
print_err(vcmdarg_err, exit_failure,
"Invalid log level \"%s\".\n", value);
ret += 1;
}
}
@ -361,7 +386,9 @@ int ws_log_parse_args(int *argc_ptr, char *argv[], void (*print_err)(const char
else if (option == opt_file) {
FILE *fp = ws_fopen(value, "w");
if (fp == NULL) {
print_err("Error opening file '%s' for writing: %s\n", value, g_strerror(errno));
print_err(vcmdarg_err, exit_failure,
"Error opening file '%s' for writing: %s.\n",
value, g_strerror(errno));
ret += 1;
}
else {
@ -370,8 +397,9 @@ int ws_log_parse_args(int *argc_ptr, char *argv[], void (*print_err)(const char
}
else if (option == opt_fatal) {
if (ws_log_set_fatal_str(value) == LOG_LEVEL_NONE) {
print_err("Fatal log level must be \"critical\" or \"warning\", "
"not \"%s\".\n", value);
print_err(vcmdarg_err, exit_failure,
"Fatal log level must be \"critical\" or "
"\"warning\", not \"%s\".\n", value);
ret += 1;
}
}
@ -498,13 +526,14 @@ enum ws_log_level ws_log_set_fatal_str(const char *str_level)
}
void ws_log_init(ws_log_writer_cb *writer)
void ws_log_init(const char *progname, ws_log_writer_cb *writer)
{
const char *env;
registered_appname = g_get_prgname();
if (registered_appname == NULL)
registered_appname = DEFAULT_APPNAME;
if (progname != NULL) {
registered_progname = progname;
g_set_prgname(progname);
}
if (writer)
registered_log_writer = writer;
@ -543,15 +572,20 @@ void ws_log_init(ws_log_writer_cb *writer)
ws_log_set_noisy_filter(env);
atexit(ws_log_cleanup);
#ifndef WS_DISABLE_DEBUG
init_complete = TRUE;
#endif
}
void ws_log_init_with_data(ws_log_writer_cb *writer, void *user_data,
ws_log_writer_free_data_cb *free_user_data)
void ws_log_init_with_data(const char *progname, ws_log_writer_cb *writer,
void *user_data,
ws_log_writer_free_data_cb *free_user_data)
{
registered_log_writer_data = user_data;
registered_log_writer_data_free = free_user_data;
ws_log_init(writer);
ws_log_init(progname, writer);
}
@ -574,8 +608,14 @@ static void log_write_do_work(FILE *fp, gboolean use_color, const char *timestam
const char *level_str = ws_log_level_to_string(level);
gboolean doextra = (level != DEFAULT_LOG_LEVEL);
#ifndef WS_DISABLE_DEBUG
if (!init_complete) {
fprintf(fp, " ** (noinit)");
}
#endif
if (doextra)
fprintf(fp, " ** (%s:%ld) ", registered_appname, (long)getpid());
fprintf(fp, " ** (%s:%ld) ", registered_progname, (long)getpid());
else
fputs(" ** ", fp);

View File

@ -132,7 +132,9 @@ enum ws_log_level ws_log_set_fatal_str(const char *str_level);
* Returns zero for no error, non-zero for a bad option value.
*/
WS_DLL_PUBLIC
int ws_log_parse_args(int *argc_ptr, char *argv[], void (*print_err)(const char *, ...));
int ws_log_parse_args(int *argc_ptr, char *argv[],
void (*vcmdarg_err)(const char *, va_list ap),
int exit_failure);
/** Initializes the logging code.
@ -142,7 +144,7 @@ int ws_log_parse_args(int *argc_ptr, char *argv[], void (*print_err)(const char
* is NULL the default log writer is used.
*/
WS_DLL_PUBLIC
void ws_log_init(ws_log_writer_cb *writer);
void ws_log_init(const char *progname, ws_log_writer_cb *writer);
/** Initializes the logging code.
@ -152,7 +154,8 @@ void ws_log_init(ws_log_writer_cb *writer);
* 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_cb *writer, void *user_data,
void ws_log_init_with_data(const char *progname, ws_log_writer_cb *writer,
void *user_data,
ws_log_writer_free_data_cb *free_user_data);