From 0867fb15c699d95b496c3550e0b121790a97e3bf Mon Sep 17 00:00:00 2001 From: Tanguy Pruvot Date: Thu, 7 Aug 2014 07:37:29 +0200 Subject: [PATCH] output: add the --color parameter (-C) disabled by default or if syslog option is set Signed-off-by: Tanguy Pruvot --- cpu-miner.c | 45 +++++++++++++++++++++++++++++---------------- miner.h | 31 +++++++++++++++++++++++++++++++ util.c | 36 +++++++++++++++++++++++++++++++----- 3 files changed, 91 insertions(+), 21 deletions(-) diff --git a/cpu-miner.c b/cpu-miner.c index 38ae4d6..ecff875 100644 --- a/cpu-miner.c +++ b/cpu-miner.c @@ -171,6 +171,7 @@ bool want_stratum = true; bool have_stratum = false; static bool submit_old = false; bool use_syslog = false; +bool use_colors = false; static bool opt_background = false; static bool opt_quiet = false; static int opt_retries = -1; @@ -261,6 +262,7 @@ Options:\n\ --no-longpoll disable X-Long-Polling support\n\ --no-stratum disable X-Stratum support\n\ -q, --quiet disable per-thread hashmeter output\n\ + -C, --color enable colored output\n\ -D, --debug enable debug output\n\ -P, --protocol-dump verbose dump of protocol-level activities\n" #ifdef HAVE_SYSLOG_H @@ -286,7 +288,7 @@ static char const short_options[] = #ifdef HAVE_SYSLOG_H "S" #endif - "a:c:Dhp:Px:qr:R:s:t:T:o:u:O:Vd:f:mv:"; + "a:c:CDhp:Px:qr:R:s:t:T:o:u:O:Vd:f:mv:"; static struct option const options[] = { { "algo", 1, NULL, 'a' }, @@ -297,6 +299,7 @@ static struct option const options[] = { { "cputest", 0, NULL, 1006 }, { "cert", 1, NULL, 1001 }, { "config", 1, NULL, 'c' }, + { "color", 0, NULL, 'C' }, { "debug", 0, NULL, 'D' }, { "help", 0, NULL, 'h' }, { "no-longpoll", 0, NULL, 1003 }, @@ -403,12 +406,14 @@ static void share_result(int result, const char *reason) pthread_mutex_unlock(&stats_lock); sprintf(s, hashrate >= 1e6 ? "%.0f" : "%.2f", 1e-3 * hashrate); - applog(LOG_INFO, "accepted: %lu/%lu (%.2f%%), %s khash/s %s", - accepted_count, - accepted_count + rejected_count, - 100. * accepted_count / (accepted_count + rejected_count), - s, - result ? "(yay!!!)" : "(booooo)"); + applog(LOG_NOTICE, "accepted: %lu/%lu (%.2f%%), %s khash/s %s", + accepted_count, + accepted_count + rejected_count, + 100. * accepted_count / (accepted_count + rejected_count), + s, + use_colors ? + (result ? CL_GRN "(yay!!!)" : CL_RED "(booooo)") + : (result ? "(yay!!!)" : "(booooo)")); if (opt_debug && reason) applog(LOG_DEBUG, "DEBUG: reject reason: %s", reason); @@ -595,8 +600,9 @@ static bool workio_submit_work(struct workio_cmd *wc, CURL *curl) } /* pause, then restart work-request loop */ - applog(LOG_ERR, "...retry after %d seconds", - opt_fail_pause); + if (!opt_benchmark) + applog(LOG_ERR, "...retry after %d seconds", opt_fail_pause); + sleep(opt_fail_pause); } @@ -820,8 +826,8 @@ static void *miner_thread(void *userdata) * of the number of CPUs */ if (num_processors > 1 && opt_n_threads % num_processors == 0) { if (!opt_quiet) - applog(LOG_INFO, "Binding thread %d to cpu %d", - thr_id, thr_id % num_processors); + applog(LOG_DEBUG, "Binding thread %d to cpu %d", thr_id, + thr_id % num_processors); affine_to_cpu(thr_id, thr_id % num_processors); } @@ -985,8 +991,8 @@ static void *miner_thread(void *userdata) for (i = 0; i < opt_n_threads && thr_hashrates[i]; i++) hashrate += thr_hashrates[i]; if (i == opt_n_threads) { - sprintf(s, hashrate >= 1e6 ? "%.0f" : "%.2f", 1e-3 * hashrate); - applog(LOG_INFO, "Total: %s khash/s", s); + sprintf(s, hashrate >= 1e6 ? "%.0f" : "%.2f", hashrate / 1000.); + applog(LOG_NOTICE, "Total: %s khash/s", s); } } @@ -1066,7 +1072,7 @@ start: pthread_mutex_lock(&g_work_lock); if (work_decode(json_object_get(val, "result"), &g_work)) { if (opt_debug) - applog(LOG_DEBUG, "DEBUG: got new work"); + applog(LOG_BLUE, "LONGPOLL pushed new work"); time(&g_work_time); restart_threads(); } @@ -1158,7 +1164,8 @@ static void *stratum_thread(void *userdata) tq_push(thr_info[work_thr_id].q, NULL); goto out; } - applog(LOG_ERR, "...retry after %d seconds", opt_fail_pause); + if (!opt_benchmark) + applog(LOG_ERR, "...retry after %d seconds", opt_fail_pause); sleep(opt_fail_pause); } } @@ -1170,7 +1177,7 @@ static void *stratum_thread(void *userdata) time(&g_work_time); pthread_mutex_unlock(&g_work_lock); if (stratum.job.clean) { - if (!opt_quiet) applog(LOG_INFO, "Stratum detected new block"); + if (!opt_quiet) applog(LOG_BLUE, "Stratum detected new block"); restart_threads(); } } @@ -1245,6 +1252,9 @@ static void parse_arg (int key, char *arg) } break; } + case 'C': + use_colors = true; + break; case 'q': opt_quiet = true; break; @@ -1428,6 +1438,9 @@ static void parse_arg (int key, char *arg) default: show_usage_and_exit(1); } + + if (use_syslog) + use_colors = false; } static void parse_config(void) diff --git a/miner.h b/miner.h index 2b0dad1..9d24980 100644 --- a/miner.h +++ b/miner.h @@ -53,6 +53,7 @@ void *alloca (size_t); #ifdef HAVE_SYSLOG_H #include +#define LOG_BLUE 0x10 /* unique value */ #else enum { LOG_ERR, @@ -60,6 +61,8 @@ enum { LOG_NOTICE, LOG_INFO, LOG_DEBUG, + /* custom notices */ + LOG_BLUE = 0x10, }; #endif @@ -284,6 +287,7 @@ extern char *opt_cert; extern char *opt_proxy; extern long opt_proxy_type; extern bool use_syslog; +extern bool use_colors; extern pthread_mutex_t applog_lock; extern struct thr_info *thr_info; extern int longpoll_thr_id; @@ -292,6 +296,33 @@ extern struct work_restart *work_restart; extern bool opt_trust_pool; extern uint16_t opt_vote; +#define CL_N "\x1B[0m" +#define CL_RED "\x1B[31m" +#define CL_GRN "\x1B[32m" +#define CL_YLW "\x1B[33m" +#define CL_BLU "\x1B[34m" +#define CL_MAG "\x1B[35m" +#define CL_CYN "\x1B[36m" + +#define CL_BLK "\x1B[22;30m" /* black */ +#define CL_RD2 "\x1B[22;31m" /* red */ +#define CL_GR2 "\x1B[22;32m" /* green */ +#define CL_BRW "\x1B[22;33m" /* brown */ +#define CL_BL2 "\x1B[22;34m" /* blue */ +#define CL_MA2 "\x1B[22;35m" /* magenta */ +#define CL_CY2 "\x1B[22;36m" /* cyan */ +#define CL_SIL "\x1B[22;37m" /* gray */ + +#define CL_GRY "\x1B[01;30m" /* dark gray */ +#define CL_LRD "\x1B[01;31m" /* light red */ +#define CL_LGR "\x1B[01;32m" /* light green */ +#define CL_YL2 "\x1B[01;33m" /* yellow */ +#define CL_LBL "\x1B[01;34m" /* light blue */ +#define CL_LMA "\x1B[01;35m" /* light magenta */ +#define CL_LCY "\x1B[01;36m" /* light cyan */ + +#define CL_WHT "\x1B[01;37m" /* white */ + extern void applog(int prio, const char *fmt, ...); extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass, const char *rpc_req, bool, bool, int *); diff --git a/util.c b/util.c index 9ccbbdf..775a9d8 100644 --- a/util.c +++ b/util.c @@ -77,7 +77,14 @@ void applog(int prio, const char *fmt, ...) va_list ap2; char *buf; int len; - + + /* custom colors to syslog prio */ + if (prio > LOG_DEBUG) { + switch (prio) { + case LOG_BLUE: prio = LOG_NOTICE; break; + } + } + va_copy(ap2, ap); len = vsnprintf(NULL, 0, fmt, ap2) + 1; va_end(ap2); @@ -89,6 +96,7 @@ void applog(int prio, const char *fmt, ...) if (0) {} #endif else { + const char* color = ""; char *f; int len; time_t now; @@ -101,16 +109,34 @@ void applog(int prio, const char *fmt, ...) memcpy(&tm, tm_p, sizeof(tm)); pthread_mutex_unlock(&applog_lock); - len = (int)(40 + strlen(fmt) + 2); - f = (char*)alloca(len); - sprintf(f, "[%d-%02d-%02d %02d:%02d:%02d] %s\n", + switch (prio) { + case LOG_ERR: color = CL_RED; break; + case LOG_WARNING: color = CL_YLW; break; + case LOG_NOTICE: color = CL_WHT; break; + case LOG_INFO: color = ""; break; + case LOG_DEBUG: color = ""; break; + + case LOG_BLUE: + prio = LOG_NOTICE; + color = CL_CYN; + break; + } + if (!use_colors) + color = ""; + + len = 40 + strlen(fmt) + 2; + f = alloca(len); + sprintf(f, "[%d-%02d-%02d %02d:%02d:%02d]%s %s%s\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, - fmt); + color, + fmt, + use_colors ? CL_N : "" + ); pthread_mutex_lock(&applog_lock); vfprintf(stderr, f, ap); /* atomic write to stderr */ fflush(stderr);