Fix stderr flushing problem
authorRyan Dahl <ry@tinyclouds.org>
Tue, 9 Feb 2010 22:11:58 +0000 (14:11 -0800)
committerRyan Dahl <ry@tinyclouds.org>
Tue, 9 Feb 2010 22:11:58 +0000 (14:11 -0800)
src/node_stdio.cc

index adcaff2..cda79e3 100644 (file)
@@ -4,6 +4,8 @@
 
 #include <unistd.h>
 #include <fcntl.h>
+#include <string.h>
+#include <errno.h>
 
 using namespace v8;
 using namespace node;
@@ -42,6 +44,15 @@ EmitClose (void)
   emit->Call(stdio, 1, argv);
 }
 
+
+static inline Local<Value> errno_exception(int errorno) {
+  Local<Value> e = Exception::Error(String::NewSymbol(strerror(errorno)));
+  Local<Object> obj = e->ToObject();
+  obj->Set(String::NewSymbol("errno"), Integer::New(errorno));
+  return e;
+}
+
+
 /* STDERR IS ALWAY SYNC */
 static Handle<Value>
 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();
 }