From 4bfb344caaae75dd7b3ddc534c98688daa23d379 Mon Sep 17 00:00:00 2001 From: jfreegman Date: Mon, 4 May 2020 16:15:24 -0400 Subject: [PATCH] Add option for toxcore logging in stderr --- doc/toxic.1 | 5 +++++ doc/toxic.1.asc | 3 +++ src/toxic.c | 30 ++++++++++++++++++++++++++---- src/windows.h | 1 + 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/doc/toxic.1 b/doc/toxic.1 index fad14a9..96f24fb 100644 --- a/doc/toxic.1 +++ b/doc/toxic.1 @@ -78,6 +78,11 @@ instead of Show help message .RE .PP +\-l, \-\-logging +.RS 4 +Enable toxcore logging to stderr +.RE +.PP \-n, \-\-nodes nodes\-file .RS 4 Use specified diff --git a/doc/toxic.1.asc b/doc/toxic.1.asc index dcfc01a..971795a 100644 --- a/doc/toxic.1.asc +++ b/doc/toxic.1.asc @@ -40,6 +40,9 @@ OPTIONS -h, --help:: Show help message +-l, --logging:: + Enable toxcore logging to stderr + -n, --nodes nodes-file:: Use specified 'nodes-file' for DHT bootstrap nodes instead of '~/.config/tox/DHTnodes.json' diff --git a/src/toxic.c b/src/toxic.c index b95ea11..0e5f092 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -219,6 +219,16 @@ void exit_toxic_err(const char *errmsg, int errcode) exit(EXIT_FAILURE); } +void cb_toxcore_logger(Tox *m, TOX_LOG_LEVEL level, const char *file, uint32_t line, const char *func, + const char *message, void *user_data) +{ + UNUSED_VAR(user_data); + UNUSED_VAR(file); + UNUSED_VAR(m); + + fprintf(stderr, "[%d] %u:%s() - %s\n", level, line, func, message); +} + static void init_term(void) { #if HAVE_WIDECHAR @@ -645,6 +655,10 @@ static void init_tox_options(struct Tox_Options *tox_opts) tox_options_set_proxy_type(tox_opts, arg_opts.proxy_type); tox_options_set_tcp_port(tox_opts, arg_opts.tcp_port); + if (arg_opts.logging) { + tox_options_set_log_callback(tox_opts, cb_toxcore_logger); + } + if (!tox_options_get_ipv6_enabled(tox_opts)) { queue_init_message("Forcing IPv4 connection"); } @@ -942,6 +956,7 @@ static void print_usage(void) fprintf(stderr, " -e, --encrypt-data Encrypt an unencrypted data file\n"); fprintf(stderr, " -f, --file Use specified data file\n"); fprintf(stderr, " -h, --help Show this message and exit\n"); + fprintf(stderr, " -l, --logging Enable toxcore logging to stderr\n"); fprintf(stderr, " -n, --nodes Use specified DHTnodes file\n"); fprintf(stderr, " -o, --noconnect Do not connect to the DHT network\n"); fprintf(stderr, " -p, --SOCKS5-proxy Use SOCKS5 proxy: Requires [IP] [port]\n"); @@ -978,6 +993,7 @@ static void parse_args(int argc, char *argv[]) {"default-locale", no_argument, 0, 'd'}, {"config", required_argument, 0, 'c'}, {"encrypt-data", no_argument, 0, 'e'}, + {"logging", no_argument, 0, 'l'}, {"nodes", required_argument, 0, 'n'}, {"help", no_argument, 0, 'h'}, {"noconnect", no_argument, 0, 'o'}, @@ -991,7 +1007,7 @@ static void parse_args(int argc, char *argv[]) {NULL, no_argument, NULL, 0}, }; - const char *opts_str = "4bdehotuxvc:f:n:r:p:P:T:"; + const char *opts_str = "4bdehlotuxvc:f:n:r:p:P:T:"; int opt, indexptr; long int port = 0; @@ -1055,6 +1071,12 @@ static void parse_args(int argc, char *argv[]) break; + case 'l': + arg_opts.debug = true; + arg_opts.logging = true; + queue_init_message("Toxcore logging enabled to stderr"); + break; + case 'n': snprintf(arg_opts.nodes_path, sizeof(arg_opts.nodes_path), "%s", optarg); break; @@ -1248,6 +1270,9 @@ void DnD_callback(const char *asdv, DropType dt) int main(int argc, char **argv) { + /* Make sure all written files are read/writeable only by the current user. */ + umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); + parse_args(argc, argv); /* Use the -b flag to enable stderr */ @@ -1263,9 +1288,6 @@ int main(int argc, char **argv) queue_init_message("Warning: Using --unencrypt-data and --encrypt-data simultaneously has no effect"); } - /* Make sure all written files are read/writeable only by the current user. */ - umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); - init_default_data_files(); bool datafile_exists = file_exists(DATA_FILE); diff --git a/src/windows.h b/src/windows.h index 1c3074a..a4951e5 100644 --- a/src/windows.h +++ b/src/windows.h @@ -89,6 +89,7 @@ struct arg_opts { bool no_connect; bool encrypt_data; bool unencrypt_data; + bool logging; char nameserver_path[MAX_STR_SIZE]; char config_path[MAX_STR_SIZE];