From 88b935928419819a6547e30dd2c8fa68fa815291 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Tue, 9 Feb 2010 14:11:58 -0800 Subject: [PATCH] Fix stderr flushing problem --- src/node_stdio.cc | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/node_stdio.cc b/src/node_stdio.cc index adcaff2557..cda79e3a93 100644 --- a/src/node_stdio.cc +++ b/src/node_stdio.cc @@ -4,6 +4,8 @@ #include #include +#include +#include using namespace v8; using namespace node; @@ -42,6 +44,15 @@ EmitClose (void) emit->Call(stdio, 1, argv); } + +static inline Local errno_exception(int errorno) { + Local e = Exception::Error(String::NewSymbol(strerror(errorno))); + Local obj = e->ToObject(); + obj->Set(String::NewSymbol("errno"), Integer::New(errorno)); + return e; +} + + /* STDERR IS ALWAY SYNC */ static Handle WriteError (const Arguments& args) @@ -53,8 +64,13 @@ WriteError (const Arguments& args) String::Utf8Value msg(args[0]->ToString()); - fprintf(stderr, "%s", *msg); - fflush(stderr); + ssize_t r; + size_t written = 0; + while (written < msg.length()) { + r = write(STDERR_FILENO, (*msg) + written, msg.length() - written); + if (r < 0) return ThrowException(errno_exception(errno)); + written += (size_t)r; + } return Undefined(); }