Browse Source

output: add the --color parameter (-C)

disabled by default or if syslog option is set

Signed-off-by: Tanguy Pruvot <tanguy.pruvot@gmail.com>
master
Tanguy Pruvot 10 years ago
parent
commit
0867fb15c6
  1. 45
      cpu-miner.c
  2. 31
      miner.h
  3. 36
      util.c

45
cpu-miner.c

@ -171,6 +171,7 @@ bool want_stratum = true;
bool have_stratum = false; bool have_stratum = false;
static bool submit_old = false; static bool submit_old = false;
bool use_syslog = false; bool use_syslog = false;
bool use_colors = false;
static bool opt_background = false; static bool opt_background = false;
static bool opt_quiet = false; static bool opt_quiet = false;
static int opt_retries = -1; static int opt_retries = -1;
@ -261,6 +262,7 @@ Options:\n\
--no-longpoll disable X-Long-Polling support\n\ --no-longpoll disable X-Long-Polling support\n\
--no-stratum disable X-Stratum support\n\ --no-stratum disable X-Stratum support\n\
-q, --quiet disable per-thread hashmeter output\n\ -q, --quiet disable per-thread hashmeter output\n\
-C, --color enable colored output\n\
-D, --debug enable debug output\n\ -D, --debug enable debug output\n\
-P, --protocol-dump verbose dump of protocol-level activities\n" -P, --protocol-dump verbose dump of protocol-level activities\n"
#ifdef HAVE_SYSLOG_H #ifdef HAVE_SYSLOG_H
@ -286,7 +288,7 @@ static char const short_options[] =
#ifdef HAVE_SYSLOG_H #ifdef HAVE_SYSLOG_H
"S" "S"
#endif #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[] = { static struct option const options[] = {
{ "algo", 1, NULL, 'a' }, { "algo", 1, NULL, 'a' },
@ -297,6 +299,7 @@ static struct option const options[] = {
{ "cputest", 0, NULL, 1006 }, { "cputest", 0, NULL, 1006 },
{ "cert", 1, NULL, 1001 }, { "cert", 1, NULL, 1001 },
{ "config", 1, NULL, 'c' }, { "config", 1, NULL, 'c' },
{ "color", 0, NULL, 'C' },
{ "debug", 0, NULL, 'D' }, { "debug", 0, NULL, 'D' },
{ "help", 0, NULL, 'h' }, { "help", 0, NULL, 'h' },
{ "no-longpoll", 0, NULL, 1003 }, { "no-longpoll", 0, NULL, 1003 },
@ -403,12 +406,14 @@ static void share_result(int result, const char *reason)
pthread_mutex_unlock(&stats_lock); pthread_mutex_unlock(&stats_lock);
sprintf(s, hashrate >= 1e6 ? "%.0f" : "%.2f", 1e-3 * hashrate); sprintf(s, hashrate >= 1e6 ? "%.0f" : "%.2f", 1e-3 * hashrate);
applog(LOG_INFO, "accepted: %lu/%lu (%.2f%%), %s khash/s %s", applog(LOG_NOTICE, "accepted: %lu/%lu (%.2f%%), %s khash/s %s",
accepted_count, accepted_count,
accepted_count + rejected_count, accepted_count + rejected_count,
100. * accepted_count / (accepted_count + rejected_count), 100. * accepted_count / (accepted_count + rejected_count),
s, s,
result ? "(yay!!!)" : "(booooo)"); use_colors ?
(result ? CL_GRN "(yay!!!)" : CL_RED "(booooo)")
: (result ? "(yay!!!)" : "(booooo)"));
if (opt_debug && reason) if (opt_debug && reason)
applog(LOG_DEBUG, "DEBUG: reject reason: %s", 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 */ /* pause, then restart work-request loop */
applog(LOG_ERR, "...retry after %d seconds", if (!opt_benchmark)
opt_fail_pause); applog(LOG_ERR, "...retry after %d seconds", opt_fail_pause);
sleep(opt_fail_pause); sleep(opt_fail_pause);
} }
@ -820,8 +826,8 @@ static void *miner_thread(void *userdata)
* of the number of CPUs */ * of the number of CPUs */
if (num_processors > 1 && opt_n_threads % num_processors == 0) { if (num_processors > 1 && opt_n_threads % num_processors == 0) {
if (!opt_quiet) if (!opt_quiet)
applog(LOG_INFO, "Binding thread %d to cpu %d", applog(LOG_DEBUG, "Binding thread %d to cpu %d", thr_id,
thr_id, thr_id % num_processors); thr_id % num_processors);
affine_to_cpu(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++) for (i = 0; i < opt_n_threads && thr_hashrates[i]; i++)
hashrate += thr_hashrates[i]; hashrate += thr_hashrates[i];
if (i == opt_n_threads) { if (i == opt_n_threads) {
sprintf(s, hashrate >= 1e6 ? "%.0f" : "%.2f", 1e-3 * hashrate); sprintf(s, hashrate >= 1e6 ? "%.0f" : "%.2f", hashrate / 1000.);
applog(LOG_INFO, "Total: %s khash/s", s); applog(LOG_NOTICE, "Total: %s khash/s", s);
} }
} }
@ -1066,7 +1072,7 @@ start:
pthread_mutex_lock(&g_work_lock); pthread_mutex_lock(&g_work_lock);
if (work_decode(json_object_get(val, "result"), &g_work)) { if (work_decode(json_object_get(val, "result"), &g_work)) {
if (opt_debug) if (opt_debug)
applog(LOG_DEBUG, "DEBUG: got new work"); applog(LOG_BLUE, "LONGPOLL pushed new work");
time(&g_work_time); time(&g_work_time);
restart_threads(); restart_threads();
} }
@ -1158,7 +1164,8 @@ static void *stratum_thread(void *userdata)
tq_push(thr_info[work_thr_id].q, NULL); tq_push(thr_info[work_thr_id].q, NULL);
goto out; 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); sleep(opt_fail_pause);
} }
} }
@ -1170,7 +1177,7 @@ static void *stratum_thread(void *userdata)
time(&g_work_time); time(&g_work_time);
pthread_mutex_unlock(&g_work_lock); pthread_mutex_unlock(&g_work_lock);
if (stratum.job.clean) { 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(); restart_threads();
} }
} }
@ -1245,6 +1252,9 @@ static void parse_arg (int key, char *arg)
} }
break; break;
} }
case 'C':
use_colors = true;
break;
case 'q': case 'q':
opt_quiet = true; opt_quiet = true;
break; break;
@ -1428,6 +1438,9 @@ static void parse_arg (int key, char *arg)
default: default:
show_usage_and_exit(1); show_usage_and_exit(1);
} }
if (use_syslog)
use_colors = false;
} }
static void parse_config(void) static void parse_config(void)

31
miner.h

@ -53,6 +53,7 @@ void *alloca (size_t);
#ifdef HAVE_SYSLOG_H #ifdef HAVE_SYSLOG_H
#include <syslog.h> #include <syslog.h>
#define LOG_BLUE 0x10 /* unique value */
#else #else
enum { enum {
LOG_ERR, LOG_ERR,
@ -60,6 +61,8 @@ enum {
LOG_NOTICE, LOG_NOTICE,
LOG_INFO, LOG_INFO,
LOG_DEBUG, LOG_DEBUG,
/* custom notices */
LOG_BLUE = 0x10,
}; };
#endif #endif
@ -284,6 +287,7 @@ extern char *opt_cert;
extern char *opt_proxy; extern char *opt_proxy;
extern long opt_proxy_type; extern long opt_proxy_type;
extern bool use_syslog; extern bool use_syslog;
extern bool use_colors;
extern pthread_mutex_t applog_lock; extern pthread_mutex_t applog_lock;
extern struct thr_info *thr_info; extern struct thr_info *thr_info;
extern int longpoll_thr_id; extern int longpoll_thr_id;
@ -292,6 +296,33 @@ extern struct work_restart *work_restart;
extern bool opt_trust_pool; extern bool opt_trust_pool;
extern uint16_t opt_vote; 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 void applog(int prio, const char *fmt, ...);
extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass, extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
const char *rpc_req, bool, bool, int *); const char *rpc_req, bool, bool, int *);

36
util.c

@ -77,7 +77,14 @@ void applog(int prio, const char *fmt, ...)
va_list ap2; va_list ap2;
char *buf; char *buf;
int len; int len;
/* custom colors to syslog prio */
if (prio > LOG_DEBUG) {
switch (prio) {
case LOG_BLUE: prio = LOG_NOTICE; break;
}
}
va_copy(ap2, ap); va_copy(ap2, ap);
len = vsnprintf(NULL, 0, fmt, ap2) + 1; len = vsnprintf(NULL, 0, fmt, ap2) + 1;
va_end(ap2); va_end(ap2);
@ -89,6 +96,7 @@ void applog(int prio, const char *fmt, ...)
if (0) {} if (0) {}
#endif #endif
else { else {
const char* color = "";
char *f; char *f;
int len; int len;
time_t now; time_t now;
@ -101,16 +109,34 @@ void applog(int prio, const char *fmt, ...)
memcpy(&tm, tm_p, sizeof(tm)); memcpy(&tm, tm_p, sizeof(tm));
pthread_mutex_unlock(&applog_lock); pthread_mutex_unlock(&applog_lock);
len = (int)(40 + strlen(fmt) + 2); switch (prio) {
f = (char*)alloca(len); case LOG_ERR: color = CL_RED; break;
sprintf(f, "[%d-%02d-%02d %02d:%02d:%02d] %s\n", 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_year + 1900,
tm.tm_mon + 1, tm.tm_mon + 1,
tm.tm_mday, tm.tm_mday,
tm.tm_hour, tm.tm_hour,
tm.tm_min, tm.tm_min,
tm.tm_sec, tm.tm_sec,
fmt); color,
fmt,
use_colors ? CL_N : ""
);
pthread_mutex_lock(&applog_lock); pthread_mutex_lock(&applog_lock);
vfprintf(stderr, f, ap); /* atomic write to stderr */ vfprintf(stderr, f, ap); /* atomic write to stderr */
fflush(stderr); fflush(stderr);

Loading…
Cancel
Save