remove output redirection.
This commit is contained in:
parent
01b3ee7ee8
commit
f863d494ca
|
@ -143,20 +143,6 @@ start_node(void);
|
||||||
static void
|
static void
|
||||||
start_node_thread(void);
|
start_node_thread(void);
|
||||||
|
|
||||||
#if OUTPUT_REDIR
|
|
||||||
static void
|
|
||||||
open_pipes(int **out_pipe, int **log_pipe);
|
|
||||||
|
|
||||||
static void
|
|
||||||
parse_logs(int **out_pipe, int **log_pipe);
|
|
||||||
|
|
||||||
static void
|
|
||||||
async_parse_logs(uv_work_t *req);
|
|
||||||
|
|
||||||
static void
|
|
||||||
async_parse_logs_after(uv_work_t *req);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
async_get_block(uv_work_t *req);
|
async_get_block(uv_work_t *req);
|
||||||
|
|
||||||
|
@ -265,33 +251,6 @@ NAN_METHOD(StartBitcoind) {
|
||||||
|
|
||||||
Local<Function> callback = Local<Function>::Cast(args[0]);
|
Local<Function> callback = Local<Function>::Cast(args[0]);
|
||||||
|
|
||||||
//
|
|
||||||
// Setup pipes to differentiate our logs from bitcoind's.
|
|
||||||
// Run in a separate thread.
|
|
||||||
//
|
|
||||||
|
|
||||||
#if OUTPUT_REDIR
|
|
||||||
int *out_pipe = (int *)malloc(2 * sizeof(int));
|
|
||||||
int *log_pipe = (int *)malloc(2 * sizeof(int));
|
|
||||||
|
|
||||||
open_pipes(&out_pipe, &log_pipe);
|
|
||||||
|
|
||||||
uv_work_t *req_parse_logs = new uv_work_t();
|
|
||||||
async_log_data* data_parse_logs = new async_log_data();
|
|
||||||
data_parse_logs->out_pipe = &out_pipe;
|
|
||||||
data_parse_logs->log_pipe = &log_pipe;
|
|
||||||
data_parse_logs->err_msg = NULL;
|
|
||||||
data_parse_logs->result = NULL;
|
|
||||||
data_parse_logs->callback = Persistent<Function>::New(callback);
|
|
||||||
req_parse_logs->data = data_parse_logs;
|
|
||||||
|
|
||||||
int status_parse_logs = uv_queue_work(uv_default_loop(),
|
|
||||||
req_parse_logs, async_parse_logs,
|
|
||||||
(uv_after_work_cb)async_parse_logs_after);
|
|
||||||
|
|
||||||
assert(status_parse_logs == 0);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Run bitcoind's StartNode() on a separate thread.
|
// Run bitcoind's StartNode() on a separate thread.
|
||||||
//
|
//
|
||||||
|
@ -310,11 +269,7 @@ NAN_METHOD(StartBitcoind) {
|
||||||
|
|
||||||
assert(status_start_node == 0);
|
assert(status_start_node == 0);
|
||||||
|
|
||||||
#if OUTPUT_REDIR
|
|
||||||
NanReturnValue(NanNew<Number>(log_pipe[1]));
|
|
||||||
#else
|
|
||||||
NanReturnValue(NanNew<Number>(-1));
|
NanReturnValue(NanNew<Number>(-1));
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -449,160 +404,6 @@ start_node_thread(void) {
|
||||||
shutdownComplete = true;
|
shutdownComplete = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if OUTPUT_REDIR
|
|
||||||
/**
|
|
||||||
* parse_logs(int **out_pipe, int **log_pipe)
|
|
||||||
* Differentiate our logs and bitcoind's logs.
|
|
||||||
* Send bitcoind's logs to a pipe instead.
|
|
||||||
*/
|
|
||||||
|
|
||||||
const char bitcoind_char[256] = {
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
0, 0, 0, 0, 0, 0, 0, /* <- ' ' */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '.',
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ':', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
'b', 'c', 'd', 0, 0, 0, 0, 'i', 'j', 0, 0, 0, 'n', 'o', 0, 0, 0, 's', 't', 0,
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
};
|
|
||||||
|
|
||||||
static void
|
|
||||||
open_pipes(int **out_pipe, int **log_pipe) {
|
|
||||||
pipe(*out_pipe);
|
|
||||||
dup2((*out_pipe)[1], STDOUT_FILENO);
|
|
||||||
dup2((*out_pipe)[1], STDERR_FILENO);
|
|
||||||
pipe(*log_pipe);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
parse_logs(int **out_pipe, int **log_pipe) {
|
|
||||||
unsigned int rtotal = 0;
|
|
||||||
ssize_t r = 0;
|
|
||||||
size_t rcount = 80 * sizeof(char);
|
|
||||||
char *buf = (char *)malloc(rcount);
|
|
||||||
char cur[13];
|
|
||||||
unsigned int cp = 0;
|
|
||||||
unsigned int reallocs = 0;
|
|
||||||
|
|
||||||
while ((r = read((*out_pipe)[0], buf, rcount))) {
|
|
||||||
unsigned int i;
|
|
||||||
char *rbuf;
|
|
||||||
|
|
||||||
if (r == -1) {
|
|
||||||
fprintf(stderr, "bitcoind.js: error=\"parse_logs(): bad read.\"\n");
|
|
||||||
sleep(1);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (r <= 0) continue;
|
|
||||||
|
|
||||||
// Grab the buffer at the start of the bytes that were read:
|
|
||||||
rbuf = (char *)(buf + rtotal);
|
|
||||||
|
|
||||||
// If these are our logs, write them to stdout:
|
|
||||||
for (i = 0; i < r; i++) {
|
|
||||||
// A naive semi-boyer-moore string search (is it a bitcoind: char?):
|
|
||||||
unsigned char ch = rbuf[i];
|
|
||||||
if (bitcoind_char[ch]) {
|
|
||||||
cur[cp] = rbuf[0];
|
|
||||||
cp++;
|
|
||||||
cur[cp] = '\0';
|
|
||||||
if (strcmp(cur, "bitcoind.js:") == 0) {
|
|
||||||
size_t wcount = r;
|
|
||||||
ssize_t w = 0;
|
|
||||||
ssize_t wtotal = 0;
|
|
||||||
// undo redirection
|
|
||||||
close((*out_pipe)[0]);
|
|
||||||
close((*out_pipe)[1]);
|
|
||||||
w = write(STDOUT_FILENO, cur, cp);
|
|
||||||
wtotal += w;
|
|
||||||
while ((w = write(STDOUT_FILENO, rbuf + i + wtotal, wcount))) {
|
|
||||||
if (w == -1) {
|
|
||||||
fprintf(stderr, "bitcoind.js: error=\"parse_logs(): bad write.\"\n");
|
|
||||||
sleep(1);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (w == 0 || (size_t)wtotal == rcount) break;
|
|
||||||
wtotal += w;
|
|
||||||
}
|
|
||||||
// reopen redirection
|
|
||||||
pipe(*out_pipe);
|
|
||||||
dup2((*out_pipe)[1], STDOUT_FILENO);
|
|
||||||
dup2((*out_pipe)[1], STDERR_FILENO);
|
|
||||||
break;
|
|
||||||
} else if (cp == sizeof cur - 1) {
|
|
||||||
cp = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If these logs are from bitcoind, write them to the log pipe:
|
|
||||||
for (i = 0; i < r; i++) {
|
|
||||||
if ((rbuf[i] == '\r' && rbuf[i] == '\n')
|
|
||||||
|| rbuf[i] == '\r' || rbuf[i] == '\n') {
|
|
||||||
size_t wcount = r;
|
|
||||||
ssize_t w = 0;
|
|
||||||
ssize_t wtotal = 0;
|
|
||||||
while ((w = write((*log_pipe)[1], rbuf + i + wtotal + 1, wcount))) {
|
|
||||||
if (w == -1) {
|
|
||||||
fprintf(stderr, "bitcoind.js: error=\"parse_logs(): bad write.\"\n");
|
|
||||||
sleep(1);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (w == 0 || (size_t)wtotal == rcount) break;
|
|
||||||
wtotal += w;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
rtotal += r;
|
|
||||||
while (rtotal > rcount) {
|
|
||||||
reallocs++;
|
|
||||||
rcount = (rcount * 2) / reallocs;
|
|
||||||
buf = (char *)realloc(buf, rcount);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
free(buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
async_parse_logs(uv_work_t *req) {
|
|
||||||
async_log_data *log_data = static_cast<async_log_data*>(req->data);
|
|
||||||
parse_logs(log_data->out_pipe, log_data->log_pipe);
|
|
||||||
log_data->err_msg = (char *)strdup("parse_logs(): failed.");
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
async_parse_logs_after(uv_work_t *req) {
|
|
||||||
NanScope();
|
|
||||||
async_log_data *log_data = static_cast<async_log_data*>(req->data);
|
|
||||||
|
|
||||||
if (log_data->err_msg != NULL) {
|
|
||||||
Local<Value> err = Exception::Error(String::New(log_data->err_msg));
|
|
||||||
free(log_data->err_msg);
|
|
||||||
const unsigned argc = 1;
|
|
||||||
Local<Value> argv[argc] = { err };
|
|
||||||
TryCatch try_catch;
|
|
||||||
log_data->callback->Call(Context::GetCurrent()->Global(), argc, argv);
|
|
||||||
if (try_catch.HasCaught()) {
|
|
||||||
node::FatalException(try_catch);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
assert(0 && "parse_logs(): should never happen.");
|
|
||||||
}
|
|
||||||
|
|
||||||
// log_data->callback.Dispose();
|
|
||||||
|
|
||||||
delete log_data;
|
|
||||||
delete req;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* StopBitcoind
|
* StopBitcoind
|
||||||
* bitcoind.stop(callback)
|
* bitcoind.stop(callback)
|
||||||
|
|
|
@ -5,5 +5,3 @@
|
||||||
* bitcoindjs.h:
|
* bitcoindjs.h:
|
||||||
* A bitcoind node.js binding header file.
|
* A bitcoind node.js binding header file.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define OUTPUT_REDIR 0
|
|
||||||
|
|
Loading…
Reference in New Issue