Skip to content

Commit fee3e89

Browse files
committed
Print error and V8Worker2.log to stderr
This commit makes sure any output from the worker code (rather than the script being run) is printed to stderr; and gives V8Worker2 a `log` method for printing to stderr, so the same can be done for the jk runtime.
1 parent 6677fe9 commit fee3e89

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

binding.cc

+18-7
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ void ExitOnPromiseRejectCallback(PromiseRejectMessage promise_reject_message) {
124124
func->Call(context->Global(), 5, args);
125125
/* message, source, lineno, colno, error */
126126
} else {
127-
printf("Unhandled Promise\n");
128-
message->PrintCurrentStackTrace(isolate, stdout);
127+
fprintf(stderr, "Unhandled Promise\n");
128+
message->PrintCurrentStackTrace(isolate, stderr);
129129
}
130130

131131
exit(1);
@@ -336,21 +336,29 @@ int worker_load_module(worker* w, char* name_s, char* source_s, int callback_ind
336336
return 0;
337337
}
338338

339-
void Print(const FunctionCallbackInfo<Value>& args) {
339+
void Fprint(FILE * out, const FunctionCallbackInfo<Value>& args) {
340340
bool first = true;
341341
for (int i = 0; i < args.Length(); i++) {
342342
HandleScope handle_scope(args.GetIsolate());
343343
if (first) {
344344
first = false;
345345
} else {
346-
printf(" ");
346+
fprintf(out, " ");
347347
}
348348
String::Utf8Value str(args[i]);
349349
const char* cstr = ToCString(str);
350-
printf("%s", cstr);
350+
fprintf(out, "%s", cstr);
351351
}
352-
printf("\n");
353-
fflush(stdout);
352+
fprintf(out, "\n");
353+
fflush(out);
354+
}
355+
356+
void Print(const FunctionCallbackInfo<Value>& args) {
357+
Fprint(stdout, args);
358+
}
359+
360+
void Log(const FunctionCallbackInfo<Value>& args) {
361+
Fprint(stderr, args);
354362
}
355363

356364
// Sets the recv callback.
@@ -475,6 +483,9 @@ worker* worker_new(int table_index) {
475483
v8worker2->Set(String::NewFromUtf8(w->isolate, "print"),
476484
FunctionTemplate::New(w->isolate, Print));
477485

486+
v8worker2->Set(String::NewFromUtf8(w->isolate, "log"),
487+
FunctionTemplate::New(w->isolate, Log));
488+
478489
v8worker2->Set(String::NewFromUtf8(w->isolate, "recv"),
479490
FunctionTemplate::New(w->isolate, Recv));
480491

worker_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ func TestPrint(t *testing.T) {
5353
}
5454
}
5555

56+
func TestLog(t *testing.T) {
57+
worker := New(func(msg []byte) []byte {
58+
t.Fatal("shouldn't recieve Message")
59+
return nil
60+
})
61+
err := worker.Load("code.js", `V8Worker2.log("log message");`)
62+
if err != nil {
63+
t.Fatal(err)
64+
}
65+
}
66+
5667
func TestSyntaxError(t *testing.T) {
5768
worker := New(func(msg []byte) []byte {
5869
t.Fatal("shouldn't recieve Message")

0 commit comments

Comments
 (0)