Browse Source

Add debug-log setting to log everything when stderr is redirected to file

Based on code from BFGMiner
djm34
Jan Berdajs 11 years ago
parent
commit
eb6f47e503
  1. 32
      api.c
  2. 22
      logging.c
  3. 9
      logging.h
  4. 17
      sgminer.c

32
api.c

@ -2755,9 +2755,10 @@ static void debugstate(struct io_data *io_data, __maybe_unused SOCKETTYPE c, cha
opt_quiet = false; opt_quiet = false;
break; break;
case 'd': case 'd':
opt_debug ^= true; opt_debug = true;
opt_verbose = opt_debug; opt_debug_console ^= true;
if (opt_debug) opt_verbose = opt_debug_console;
if (opt_debug_console)
opt_quiet = false; opt_quiet = false;
break; break;
case 'r': case 'r':
@ -2771,7 +2772,7 @@ static void debugstate(struct io_data *io_data, __maybe_unused SOCKETTYPE c, cha
break; break;
case 'n': case 'n':
opt_verbose = false; opt_verbose = false;
opt_debug = false; opt_debug_console = false;
opt_quiet = false; opt_quiet = false;
opt_protocol = false; opt_protocol = false;
want_per_device_stats = 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, "Silent", &opt_realquiet, false);
root = api_add_bool(root, "Quiet", &opt_quiet, false); root = api_add_bool(root, "Quiet", &opt_quiet, false);
root = api_add_bool(root, "Verbose", &opt_verbose, 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, "RPCProto", &opt_protocol, false);
root = api_add_bool(root, "PerDevice", &want_per_device_stats, false); root = api_add_bool(root, "PerDevice", &want_per_device_stats, false);
root = api_add_bool(root, "WorkTime", &opt_worktime, 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_lock(&quit_restart_lock);
mutex_unlock(&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(); kill_work();
@ -3376,8 +3376,7 @@ static void *restart_thread(__maybe_unused void *userdata)
mutex_lock(&quit_restart_lock); mutex_lock(&quit_restart_lock);
mutex_unlock(&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(); app_restart();
@ -3734,12 +3733,10 @@ void api(int api_thr_id)
else else
buf[n] = '\0'; buf[n] = '\0';
if (opt_debug) { if (SOCKETFAIL(n))
if (SOCKETFAIL(n)) applog(LOG_DEBUG, "API: recv failed: %s", SOCKERRMSG);
applog(LOG_DEBUG, "API: recv failed: %s", SOCKERRMSG); else
else applog(LOG_DEBUG, "API: recv command: (%d) '%s'", n, buf);
applog(LOG_DEBUG, "API: recv command: (%d) '%s'", n, buf);
}
if (!SOCKETFAIL(n)) { if (!SOCKETFAIL(n)) {
// the time of the request in now // the time of the request in now
@ -3892,9 +3889,8 @@ die:
free(apisock); free(apisock);
if (opt_debug) applog(LOG_DEBUG, "API: terminating due to: %s",
applog(LOG_DEBUG, "API: terminating due to: %s", do_a_quit ? "QUIT" : (do_a_restart ? "RESTART" : (bye ? "BYE" : "UNKNOWN!")));
do_a_quit ? "QUIT" : (do_a_restart ? "RESTART" : (bye ? "BYE" : "UNKNOWN!")));
mutex_lock(&quit_restart_lock); mutex_lock(&quit_restart_lock);

22
logging.c

@ -16,6 +16,7 @@
#include "miner.h" #include "miner.h"
bool opt_debug = false; bool opt_debug = false;
bool opt_debug_console = false;
bool opt_verbose = false; bool opt_verbose = false;
int last_date_output_day = 0; int last_date_output_day = 0;
int opt_log_show_date = false; 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) void vapplogsiz(int prio, int size, const char* fmt, va_list args)
{ {
if (opt_debug || prio != LOG_DEBUG) { if (opt_debug || prio != LOG_DEBUG) {
if (use_syslog || opt_verbose || prio <= opt_log_level) { char *tmp42 = (char *)calloc(size + 1, 1);
char *tmp42 = (char *)calloc(size + 1, 1); vsnprintf(tmp42, size, fmt, args);
vsnprintf(tmp42, size, fmt, args); _applog(prio, tmp42, false);
_applog(prio, tmp42, false); free(tmp42);
free(tmp42);
}
} }
} }
@ -91,6 +90,11 @@ void _applog(int prio, const char *str, bool force)
if (0) {} if (0) {}
#endif #endif
else { 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]; char datetime[64];
struct timeval tv = {0, 0}; struct timeval tv = {0, 0};
struct tm *tm; 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 */ /* 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 */ fprintf(stderr, "%s%s\n", datetime, str); /* atomic write to stderr */
fflush(stderr); fflush(stderr);
} }
my_log_curses(prio, datetime, str, force); if (write_console) {
my_log_curses(prio, datetime, str, force);
}
} }
} }

9
logging.h

@ -19,6 +19,7 @@ enum {
/* debug flags */ /* debug flags */
extern bool opt_debug; extern bool opt_debug;
extern bool opt_debug_console;
extern bool opt_verbose; extern bool opt_verbose;
extern bool opt_realquiet; extern bool opt_realquiet;
extern bool want_per_device_stats; 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 { \ #define forcelog(prio, fmt, ...) do { \
if (opt_debug || prio != LOG_DEBUG) { \ if (opt_debug || prio != LOG_DEBUG) { \
if (use_syslog || opt_verbose || prio <= opt_log_level) { \ char tmp42[LOGBUFSIZ]; \
char tmp42[LOGBUFSIZ]; \ snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \ _applog(prio, tmp42, true); \
_applog(prio, tmp42, true); \
} \
} \ } \
} while (0) } while (0)

17
sgminer.c

@ -1100,6 +1100,7 @@ static char *set_pool_description(char *arg)
static char *enable_debug(bool *flag) static char *enable_debug(bool *flag)
{ {
*flag = true; *flag = true;
opt_debug_console = true;
/* Turn on verbose output, too. */ /* Turn on verbose output, too. */
opt_verbose = true; opt_verbose = true;
return NULL; return NULL;
@ -1304,6 +1305,9 @@ struct opt_table opt_config_table[] = {
OPT_WITHOUT_ARG("--debug|-D", OPT_WITHOUT_ARG("--debug|-D",
enable_debug, &opt_debug, enable_debug, &opt_debug,
"Enable debug output"), "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", OPT_WITH_ARG("--default-profile",
set_default_profile, NULL, NULL, set_default_profile, NULL, NULL,
"Set Default Profile"), "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" 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" "[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", "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", want_per_device_stats? "on" : "off",
opt_quiet ? "on" : "off", opt_quiet ? "on" : "off",
opt_verbose ? "on" : "off", opt_verbose ? "on" : "off",
@ -4655,7 +4659,7 @@ retry:
goto retry; goto retry;
} else if (!strncasecmp(&input, "n", 1)) { } else if (!strncasecmp(&input, "n", 1)) {
opt_verbose = false; opt_verbose = false;
opt_debug = false; opt_debug_console = false;
opt_quiet = false; opt_quiet = false;
opt_protocol = false; opt_protocol = false;
opt_compact = false; opt_compact = false;
@ -4664,11 +4668,12 @@ retry:
switch_logsize(false); switch_logsize(false);
goto retry; goto retry;
} else if (!strncasecmp(&input, "d", 1)) { } else if (!strncasecmp(&input, "d", 1)) {
opt_debug ^= true; opt_debug = true;
opt_verbose = opt_debug; opt_debug_console ^= true;
if (opt_debug) opt_verbose = opt_debug_console;
if (opt_debug_console)
opt_quiet = false; opt_quiet = false;
wlogprint("Debug mode %s\n", opt_debug ? "enabled" : "disabled"); wlogprint("Debug mode %s\n", opt_debug_console ? "enabled" : "disabled");
goto retry; goto retry;
} else if (!strncasecmp(&input, "i", 1)) { } else if (!strncasecmp(&input, "i", 1)) {
opt_incognito ^= true; opt_incognito ^= true;

Loading…
Cancel
Save