From eb6f47e50356d83711a30b91c24084ad342add4d Mon Sep 17 00:00:00 2001 From: Jan Berdajs Date: Sun, 6 Jul 2014 01:07:32 +0200 Subject: [PATCH] Add debug-log setting to log everything when stderr is redirected to file Based on code from BFGMiner --- api.c | 32 ++++++++++++++------------------ logging.c | 22 ++++++++++++++-------- logging.h | 9 ++++----- sgminer.c | 17 +++++++++++------ 4 files changed, 43 insertions(+), 37 deletions(-) diff --git a/api.c b/api.c index 8513ea98..8c65e60a 100644 --- a/api.c +++ b/api.c @@ -2755,9 +2755,10 @@ static void debugstate(struct io_data *io_data, __maybe_unused SOCKETTYPE c, cha opt_quiet = false; break; case 'd': - opt_debug ^= true; - opt_verbose = opt_debug; - if (opt_debug) + opt_debug = true; + opt_debug_console ^= true; + opt_verbose = opt_debug_console; + if (opt_debug_console) opt_quiet = false; break; case 'r': @@ -2771,7 +2772,7 @@ static void debugstate(struct io_data *io_data, __maybe_unused SOCKETTYPE c, cha break; case 'n': opt_verbose = false; - opt_debug = false; + opt_debug_console = false; opt_quiet = false; opt_protocol = false; want_per_device_stats = false; @@ -2799,7 +2800,7 @@ static void debugstate(struct io_data *io_data, __maybe_unused SOCKETTYPE c, cha root = api_add_bool(root, "Silent", &opt_realquiet, false); root = api_add_bool(root, "Quiet", &opt_quiet, false); root = api_add_bool(root, "Verbose", &opt_verbose, false); - root = api_add_bool(root, "Debug", &opt_debug, false); + root = api_add_bool(root, "Debug", &opt_debug_console, false); root = api_add_bool(root, "RPCProto", &opt_protocol, false); root = api_add_bool(root, "PerDevice", &want_per_device_stats, false); root = api_add_bool(root, "WorkTime", &opt_worktime, false); @@ -3362,8 +3363,7 @@ static void *quit_thread(__maybe_unused void *userdata) mutex_lock(&quit_restart_lock); mutex_unlock(&quit_restart_lock); - if (opt_debug) - applog(LOG_DEBUG, "API: killing sgminer"); + applog(LOG_DEBUG, "API: killing sgminer"); kill_work(); @@ -3376,8 +3376,7 @@ static void *restart_thread(__maybe_unused void *userdata) mutex_lock(&quit_restart_lock); mutex_unlock(&quit_restart_lock); - if (opt_debug) - applog(LOG_DEBUG, "API: restarting sgminer"); + applog(LOG_DEBUG, "API: restarting sgminer"); app_restart(); @@ -3734,12 +3733,10 @@ void api(int api_thr_id) else buf[n] = '\0'; - if (opt_debug) { - if (SOCKETFAIL(n)) - applog(LOG_DEBUG, "API: recv failed: %s", SOCKERRMSG); - else - applog(LOG_DEBUG, "API: recv command: (%d) '%s'", n, buf); - } + if (SOCKETFAIL(n)) + applog(LOG_DEBUG, "API: recv failed: %s", SOCKERRMSG); + else + applog(LOG_DEBUG, "API: recv command: (%d) '%s'", n, buf); if (!SOCKETFAIL(n)) { // the time of the request in now @@ -3892,9 +3889,8 @@ die: free(apisock); - if (opt_debug) - applog(LOG_DEBUG, "API: terminating due to: %s", - do_a_quit ? "QUIT" : (do_a_restart ? "RESTART" : (bye ? "BYE" : "UNKNOWN!"))); + applog(LOG_DEBUG, "API: terminating due to: %s", + do_a_quit ? "QUIT" : (do_a_restart ? "RESTART" : (bye ? "BYE" : "UNKNOWN!"))); mutex_lock(&quit_restart_lock); diff --git a/logging.c b/logging.c index f7bc3ddf..5d189506 100644 --- a/logging.c +++ b/logging.c @@ -16,6 +16,7 @@ #include "miner.h" bool opt_debug = false; +bool opt_debug_console = false; bool opt_verbose = false; int last_date_output_day = 0; int opt_log_show_date = false; @@ -69,12 +70,10 @@ void applogsiz(int prio, int size, const char* fmt, ...) void vapplogsiz(int prio, int size, const char* fmt, va_list args) { if (opt_debug || prio != LOG_DEBUG) { - if (use_syslog || opt_verbose || prio <= opt_log_level) { - char *tmp42 = (char *)calloc(size + 1, 1); - vsnprintf(tmp42, size, fmt, args); - _applog(prio, tmp42, false); - free(tmp42); - } + char *tmp42 = (char *)calloc(size + 1, 1); + vsnprintf(tmp42, size, fmt, args); + _applog(prio, tmp42, false); + free(tmp42); } } @@ -91,6 +90,11 @@ void _applog(int prio, const char *str, bool force) if (0) {} #endif else { + bool write_console = opt_debug_console || (opt_verbose && prio != LOG_DEBUG) || prio <= opt_log_level; + bool write_stderr = !isatty(fileno((FILE *)stderr)); + if (!(write_console || write_stderr)) + return; + char datetime[64]; struct timeval tv = {0, 0}; struct tm *tm; @@ -128,11 +132,13 @@ void _applog(int prio, const char *str, bool force) } /* Only output to stderr if it's not going to the screen as well */ - if (!isatty(fileno((FILE *)stderr))) { + if (write_stderr) { fprintf(stderr, "%s%s\n", datetime, str); /* atomic write to stderr */ fflush(stderr); } - my_log_curses(prio, datetime, str, force); + if (write_console) { + my_log_curses(prio, datetime, str, force); + } } } diff --git a/logging.h b/logging.h index e314dcd9..58474a6e 100644 --- a/logging.h +++ b/logging.h @@ -19,6 +19,7 @@ enum { /* debug flags */ extern bool opt_debug; +extern bool opt_debug_console; extern bool opt_verbose; extern bool opt_realquiet; extern bool want_per_device_stats; @@ -40,11 +41,9 @@ extern void _applog(int prio, const char *str, bool force); #define forcelog(prio, fmt, ...) do { \ if (opt_debug || prio != LOG_DEBUG) { \ - if (use_syslog || opt_verbose || prio <= opt_log_level) { \ - char tmp42[LOGBUFSIZ]; \ - snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \ - _applog(prio, tmp42, true); \ - } \ + char tmp42[LOGBUFSIZ]; \ + snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \ + _applog(prio, tmp42, true); \ } \ } while (0) diff --git a/sgminer.c b/sgminer.c index b44678dd..7635167e 100644 --- a/sgminer.c +++ b/sgminer.c @@ -1100,6 +1100,7 @@ static char *set_pool_description(char *arg) static char *enable_debug(bool *flag) { *flag = true; + opt_debug_console = true; /* Turn on verbose output, too. */ opt_verbose = true; return NULL; @@ -1304,6 +1305,9 @@ struct opt_table opt_config_table[] = { OPT_WITHOUT_ARG("--debug|-D", enable_debug, &opt_debug, "Enable debug output"), + OPT_WITHOUT_ARG("--debug-log", + opt_set_bool, &opt_debug, + "Enable debug logging when stderr is redirected to file"), OPT_WITH_ARG("--default-profile", set_default_profile, NULL, NULL, "Set Default Profile"), @@ -4631,7 +4635,7 @@ retry: wlogprint("[D]ebug: %s\n[P]er-device: %s\n[Q]uiet: %s\n[V]erbose: %s\n" "[R]PC debug: %s\n[W]orkTime details: %s\n[I]ncognito: %s\n" "co[M]pact: %s\n[L]og interval: %d\n[Z]ero statistics\n", - opt_debug ? "on" : "off", + opt_debug_console ? "on" : "off", want_per_device_stats? "on" : "off", opt_quiet ? "on" : "off", opt_verbose ? "on" : "off", @@ -4655,7 +4659,7 @@ retry: goto retry; } else if (!strncasecmp(&input, "n", 1)) { opt_verbose = false; - opt_debug = false; + opt_debug_console = false; opt_quiet = false; opt_protocol = false; opt_compact = false; @@ -4664,11 +4668,12 @@ retry: switch_logsize(false); goto retry; } else if (!strncasecmp(&input, "d", 1)) { - opt_debug ^= true; - opt_verbose = opt_debug; - if (opt_debug) + opt_debug = true; + opt_debug_console ^= true; + opt_verbose = opt_debug_console; + if (opt_debug_console) opt_quiet = false; - wlogprint("Debug mode %s\n", opt_debug ? "enabled" : "disabled"); + wlogprint("Debug mode %s\n", opt_debug_console ? "enabled" : "disabled"); goto retry; } else if (!strncasecmp(&input, "i", 1)) { opt_incognito ^= true;