Skip to content

Commit

Permalink
Add debug-log setting to log everything when stderr is redirected to …
Browse files Browse the repository at this point in the history
…file

Based on code from BFGMiner
  • Loading branch information
mrbrdo committed Jul 5, 2014
1 parent 485ec9a commit eb6f47e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 37 deletions.
32 changes: 14 additions & 18 deletions api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();

Expand All @@ -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();

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -3892,9 +3889,8 @@ void api(int api_thr_id)

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);

Expand Down
22 changes: 14 additions & 8 deletions logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
9 changes: 4 additions & 5 deletions logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)

Expand Down
17 changes: 11 additions & 6 deletions sgminer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -4631,7 +4635,7 @@ static void display_options(void)
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",
Expand All @@ -4655,7 +4659,7 @@ static void display_options(void)
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;
Expand All @@ -4664,11 +4668,12 @@ static void display_options(void)
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;
Expand Down

0 comments on commit eb6f47e

Please sign in to comment.