Skip to content

Commit

Permalink
output: add the --color parameter (-C)
Browse files Browse the repository at this point in the history
disabled by default or if syslog option is set

Signed-off-by: Tanguy Pruvot <[email protected]>
  • Loading branch information
tpruvot committed Aug 26, 2014
1 parent 52eb8a1 commit c64468e
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 25 deletions.
64 changes: 42 additions & 22 deletions cpu-miner.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ bool allow_getwork = true;
bool want_stratum = true;
bool have_stratum = false;
bool use_syslog = false;
bool use_colors = false;
static bool opt_background = false;
static bool opt_quiet = false;
static int opt_retries = -1;
Expand Down Expand Up @@ -247,6 +248,7 @@ Options:\n\
--no-stratum disable X-Stratum support\n\
--no-redirect ignore requests to change the URL of the mining server\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
Expand All @@ -272,7 +274,7 @@ static char const short_options[] =
#ifdef HAVE_SYSLOG_H
"S"
#endif
"a:c:Dhp:Px:qr:R:s:t:T:o:u:O:V";
"a:c:CDhp:Px:qr:R:s:t:T:o:u:O:V";

static struct option const options[] = {
{ "algo", 1, NULL, 'a' },
Expand All @@ -284,6 +286,7 @@ static struct option const options[] = {
{ "coinbase-addr", 1, NULL, 1013 },
{ "coinbase-sig", 1, NULL, 1015 },
{ "config", 1, NULL, 'c' },
{ "color", 0, NULL, 'C' },
{ "debug", 0, NULL, 'D' },
{ "help", 0, NULL, 'h' },
{ "no-gbt", 0, NULL, 1011 },
Expand Down Expand Up @@ -479,7 +482,7 @@ bool rpc2_job_decode(const json_t *job, struct work *work)
hashrate += thr_hashrates[i];
pthread_mutex_unlock(&stats_lock);
double difficulty = (((double) 0xffffffff) / target);
applog(LOG_INFO, "Pool set diff to %g", difficulty);
applog(LOG_NOTICE, "Pool set diff to %g", difficulty);
rpc2_target = target;
}

Expand All @@ -491,7 +494,7 @@ bool rpc2_job_decode(const json_t *job, struct work *work)
}
if(work) {
if (!rpc2_blob) {
applog(LOG_ERR, "Requested work before work was received");
applog(LOG_WARNING, "Requested work before work was received");
goto err_out;
}
memcpy(work->data, rpc2_blob, rpc2_bloblen);
Expand Down Expand Up @@ -691,7 +694,7 @@ static bool gbt_work_decode(const json_t *val, struct work *work)
int64_t cbvalue;
if (!pk_script_size) {
if (allow_getwork) {
applog(LOG_INFO, "No payout address provided, switching to getwork");
applog(LOG_NOTICE, "No payout address provided, switching to getwork");
have_gbt = false;
} else
applog(LOG_ERR, "No payout address provided");
Expand Down Expand Up @@ -871,18 +874,23 @@ static void share_result(int result, struct work *work, const char *reason)

switch (opt_algo) {
case ALGO_CRYPTONIGHT:
applog(LOG_INFO, "accepted: %lu/%lu (%.2f%%), %.2f H/s at diff %g %s",
sprintf(s, hashrate >= 1e6 ? "%.0f" : "%.2f", hashrate);
applog(LOG_NOTICE, "accepted: %lu/%lu (%.2f%%), %s H/s at diff %g %s",
accepted_count, accepted_count + rejected_count,
100. * accepted_count / (accepted_count + rejected_count), hashrate,
100. * accepted_count / (accepted_count + rejected_count), s,
(((double) 0xffffffff) / (work ? work->target[7] : rpc2_target)),
result ? "(yay!!!)" : "(booooo)");
use_colors ?
(result ? CL_GRN "(yay!!!)" : CL_RED "(booooo)")
: (result ? "(yay!!!)" : "(booooo)"));
break;
default:
sprintf(s, hashrate >= 1e6 ? "%.0f" : "%.2f", 1e-3 * hashrate);
applog(LOG_INFO, "accepted: %lu/%lu (%.2f%%), %s khash/s %s",
sprintf(s, hashrate >= 1e6 ? "%.0f" : "%.2f", hashrate / 1000.0);
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,
result ? "(yay!!!)" : "(booooo)");
use_colors ?
(result ? CL_GRN "(yay!!!)" : CL_RED "(booooo)")
: (result ? "(yay!!!)" : "(booooo)"));
break;
}

Expand Down Expand Up @@ -1099,7 +1107,7 @@ static bool get_upstream_work(CURL *curl, struct work *work)
}

if (have_gbt && allow_getwork && !val && err == CURLE_OK) {
applog(LOG_INFO, "getblocktemplate failed, falling back to getwork");
applog(LOG_NOTICE, "getblocktemplate failed, falling back to getwork");
have_gbt = false;
goto start;
}
Expand Down Expand Up @@ -1234,7 +1242,8 @@ 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);
}

Expand All @@ -1255,7 +1264,8 @@ static bool workio_login(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);
pthread_mutex_unlock(&rpc2_login_lock);
pthread_mutex_lock(&rpc2_login_lock);
Expand Down Expand Up @@ -1320,7 +1330,8 @@ static bool get_work(struct thr_info *thr, struct work *work)
struct work *work_heap;

if (opt_benchmark) {
memset(work->data, 0x55, 76);
for (int n=0; n<74; n++) ((char*)work->data)[n] = n;
//memset(work->data, 0x55, 76);
work->data[17] = swab32(time(NULL));
memset(work->data + 19, 0x00, 52);
work->data[20] = 0x80000000;
Expand Down Expand Up @@ -1464,7 +1475,7 @@ 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,
applog(LOG_DEBUG, "Binding thread %d to cpu %d", thr_id,
thr_id % num_processors);
affine_to_cpu(thr_id, thr_id % num_processors);
}
Expand Down Expand Up @@ -1514,7 +1525,11 @@ static void *miner_thread(void *userdata)
continue;
}
}
if (jsonrpc_2 ? memcmp(work.data, g_work.data, 39) || memcmp(((uint8_t*) work.data) + 43, ((uint8_t*) g_work.data) + 43, 33) : memcmp(work.data, g_work.data, 76)) {
if (jsonrpc_2
? memcmp(work.data, g_work.data, 39) ||
memcmp(((uint8_t*) work.data) + 43, ((uint8_t*) g_work.data) + 43, 33)
: memcmp(work.data, g_work.data, 76))
{
work_free(&work);
work_copy(&work, &g_work);
nonceptr = (uint32_t*) (((char*)work.data) + (jsonrpc_2 ? 39 : 76));
Expand Down Expand Up @@ -1670,11 +1685,11 @@ static void *miner_thread(void *userdata)
if (i == opt_n_threads) {
switch(opt_algo) {
case ALGO_CRYPTONIGHT:
applog(LOG_INFO, "Total: %s H/s", hashrate);
applog(LOG_NOTICE, "Total: %s H/s", hashrate);
break;
default:
sprintf(s, hashrate >= 1e6 ? "%.0f" : "%.2f", hashrate / 1000);
applog(LOG_INFO, "Total: %s khash/s", s);
applog(LOG_NOTICE, "Total: %s khash/s", s);
break;
}
}
Expand Down Expand Up @@ -1786,7 +1801,7 @@ static void *longpoll_thread(void *userdata)
rc = work_decode(res, &g_work);
if (rc) {
if (strcmp(start_job_id, g_work.job_id)) {
applog(LOG_INFO, "LONGPOLL pushed new work");
applog(LOG_BLUE, "LONGPOLL pushed new work");
time(&g_work_time);
restart_threads();
}
Expand Down Expand Up @@ -1894,7 +1909,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);
}
}
Expand All @@ -1907,7 +1923,7 @@ static void *stratum_thread(void *userdata)
stratum_gen_work(&stratum, &g_work);
time(&g_work_time);
pthread_mutex_unlock(&g_work_lock);
applog(LOG_INFO, "Stratum requested work restart");
applog(LOG_BLUE, "Stratum requested work restart");
restart_threads();
}
} else {
Expand All @@ -1919,7 +1935,7 @@ static void *stratum_thread(void *userdata)
time(&g_work_time);
pthread_mutex_unlock(&g_work_lock);
if (stratum.job.clean) {
applog(LOG_INFO, "Stratum requested work restart");
applog(LOG_BLUE, "Stratum requested work restart");
restart_threads();
}
}
Expand Down Expand Up @@ -2056,6 +2072,9 @@ static void parse_arg(int key, char *arg, char *pname)
json_decref(config);
break;
}
case 'C':
use_colors = true;
break;
case 'q':
opt_quiet = true;
break;
Expand Down Expand Up @@ -2230,6 +2249,7 @@ static void parse_arg(int key, char *arg, char *pname)
break;
case 'S':
use_syslog = true;
use_colors = false;
break;
case 'V':
show_version_and_exit();
Expand Down
31 changes: 31 additions & 0 deletions miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@ void *alloca (size_t);

#ifdef HAVE_SYSLOG_H
#include <syslog.h>
#define LOG_BLUE 0x10 /* unique value */
#else
enum {
LOG_ERR,
LOG_WARNING,
LOG_NOTICE,
LOG_INFO,
LOG_DEBUG,
/* custom notices */
LOG_BLUE = 0x10,
};
#endif

Expand Down Expand Up @@ -229,6 +232,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;
Expand All @@ -241,6 +245,33 @@ extern bool aes_ni_supported;
#define JSON_RPC_QUIET_404 (1 << 1)
#define JSON_RPC_IGNOREERR (1 << 2)

#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, int *curl_err, int flags);
Expand Down
32 changes: 29 additions & 3 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,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);
Expand All @@ -91,6 +98,7 @@ void applog(int prio, const char *fmt, ...)
if (0) {}
#endif
else {
const char* color = "";
char *f;
int len;
time_t now;
Expand All @@ -103,16 +111,34 @@ void applog(int prio, const char *fmt, ...)
memcpy(&tm, tm_p, sizeof(tm));
pthread_mutex_unlock(&applog_lock);

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\n",
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);
Expand Down

0 comments on commit c64468e

Please sign in to comment.