GeezerVison is showing github.com — src: use custom fprintf alike to write errors to stderr · lance/node@32e7e81
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/debug_utils-inl.h +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -83,6 +83,11 @@ std::string COLD_NOINLINE SPrintF( // NOLINT(runtime/string) | 83 | 83 | return SPrintFImpl(format, std::forward<Args>(args)...); | 84 | 84 | } | 85 | 85 |
| | 86 | +template <typename... Args> | | 87 | +void COLD_NOINLINE FPrintF(FILE* file, const char* format, Args&&... args) { | | 88 | + FWrite(file, SPrintF(format, std::forward<Args>(args)...)); | | 89 | +} | | 90 | + | 86 | 91 | } // namespace node | 87 | 92 |
| 88 | 93 | #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | |
Collapse file Copy file name to clipboardExpand all lines: src/debug_utils.cc +43Lines changed: 43 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 | #include <features.h> | 7 | 7 | #endif | 8 | 8 |
| | 9 | +#ifdef __ANDROID__ | | 10 | +#include <android/log.h> | | 11 | +#endif | | 12 | + | 9 | 13 | #if defined(__linux__) && !defined(__GLIBC__) || \ | 10 | 14 | defined(__UCLIBC__) || \ | 11 | 15 | defined(_AIX) | @@ -437,6 +441,45 @@ std::vector<std::string> NativeSymbolDebuggingContext::GetLoadedLibraries() { | 437 | 441 | return list; | 438 | 442 | } | 439 | 443 |
| | 444 | +void FWrite(FILE* file, const std::string& str) { | | 445 | + auto simple_fwrite = [&]() { | | 446 | + // The return value is ignored because there's no good way to handle it. | | 447 | + fwrite(str.data(), str.size(), 1, file); | | 448 | + }; | | 449 | + | | 450 | + if (file != stderr && file != stdout) { | | 451 | + simple_fwrite(); | | 452 | + return; | | 453 | + } | | 454 | +#ifdef _WIN32 | | 455 | + HANDLE handle = | | 456 | + GetStdHandle(file == stdout ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE); | | 457 | + | | 458 | + // Check if stderr is something other than a tty/console | | 459 | + if (handle == INVALID_HANDLE_VALUE || handle == nullptr || | | 460 | + uv_guess_handle(_fileno(file)) != UV_TTY) { | | 461 | + simple_fwrite(); | | 462 | + return; | | 463 | + } | | 464 | + | | 465 | + // Get required wide buffer size | | 466 | + int n = MultiByteToWideChar(CP_UTF8, 0, str.data(), str.size(), nullptr, 0); | | 467 | + | | 468 | + std::vector<wchar_t> wbuf(n); | | 469 | + MultiByteToWideChar(CP_UTF8, 0, str.data(), str.size(), wbuf.data(), n); | | 470 | + | | 471 | + // Don't include the final null character in the output | | 472 | + CHECK_GT(n, 0); | | 473 | + WriteConsoleW(handle, wbuf.data(), n - 1, nullptr, nullptr); | | 474 | + return; | | 475 | +#elif defined(__ANDROID__) | | 476 | + if (file == stderr) { | | 477 | + __android_log_print(ANDROID_LOG_ERROR, "nodejs", "%s", str.data()); | | 478 | + return; | | 479 | + } | | 480 | +#endif | | 481 | + simple_fwrite(); | | 482 | +} | 440 | 483 |
| 441 | 484 | } // namespace node | 442 | 485 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/debug_utils.h +6-4Lines changed: 6 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -25,13 +25,16 @@ namespace node { | 25 | 25 | template <typename T> | 26 | 26 | inline std::string ToString(const T& value); | 27 | 27 |
| 28 | | -// C++-style variant of sprintf() that: | | 28 | +// C++-style variant of sprintf()/fprintf() that: | 29 | 29 | // - Returns an std::string | 30 | 30 | // - Handles \0 bytes correctly | 31 | 31 | // - Supports %p and %s. %d, %i and %u are aliases for %s. | 32 | 32 | // - Accepts any class that has a ToString() method for stringification. | 33 | 33 | template <typename... Args> | 34 | 34 | inline std::string SPrintF(const char* format, Args&&... args); | | 35 | +template <typename... Args> | | 36 | +inline void FPrintF(FILE* file, const char* format, Args&&... args); | | 37 | +void FWrite(FILE* file, const std::string& str); | 35 | 38 |
| 36 | 39 | template <typename... Args> | 37 | 40 | inline void FORCE_INLINE Debug(Environment* env, | @@ -40,16 +43,15 @@ inline void FORCE_INLINE Debug(Environment* env, | 40 | 43 | Args&&... args) { | 41 | 44 | if (!UNLIKELY(env->debug_enabled(cat))) | 42 | 45 | return; | 43 | | - std::string out = SPrintF(format, std::forward<Args>(args)...); | 44 | | - fwrite(out.data(), out.size(), 1, stderr); | | 46 | + FPrintF(stderr, format, std::forward<Args>(args)...); | 45 | 47 | } | 46 | 48 |
| 47 | 49 | inline void FORCE_INLINE Debug(Environment* env, | 48 | 50 | DebugCategory cat, | 49 | 51 | const char* message) { | 50 | 52 | if (!UNLIKELY(env->debug_enabled(cat))) | 51 | 53 | return; | 52 | | - fprintf(stderr, "%s", message); | | 54 | + FPrintF(stderr, "%s", message); | 53 | 55 | } | 54 | 56 |
| 55 | 57 | template <typename... Args> | |
Collapse file Copy file name to clipboardExpand all lines: src/node_errors.cc +40-87Lines changed: 40 additions & 87 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | #include <cerrno> | 2 | 2 | #include <cstdarg> | 3 | 3 |
| | 4 | +#include "debug_utils-inl.h" | 4 | 5 | #include "node_errors.h" | 5 | 6 | #include "node_internals.h" | 6 | 7 | #ifdef NODE_REPORT | | 10 | 11 | #include "node_v8_platform-inl.h" | 11 | 12 | #include "util-inl.h" | 12 | 13 |
| 13 | | -#ifdef __ANDROID__ | 14 | | -#include <android/log.h> | 15 | | -#endif | 16 | | - | 17 | 14 | namespace node { | 18 | 15 |
| 19 | 16 | using errors::TryCatchScope; | @@ -54,8 +51,6 @@ namespace per_process { | 54 | 51 | static Mutex tty_mutex; | 55 | 52 | } // namespace per_process | 56 | 53 |
| 57 | | -static const int kMaxErrorSourceLength = 1024; | 58 | | - | 59 | 54 | static std::string GetErrorSource(Isolate* isolate, | 60 | 55 | Local<Context> context, | 61 | 56 | Local<Message> message, | @@ -107,41 +102,35 @@ static std::string GetErrorSource(Isolate* isolate, | 107 | 102 | end -= script_start; | 108 | 103 | } | 109 | 104 |
| 110 | | - int max_off = kMaxErrorSourceLength - 2; | 111 | | - | 112 | | - char buf[kMaxErrorSourceLength]; | 113 | | - int off = snprintf(buf, | 114 | | - kMaxErrorSourceLength, | 115 | | - "%s:%i\n%s\n", | 116 | | - filename_string, | 117 | | - linenum, | 118 | | - sourceline.c_str()); | 119 | | - CHECK_GE(off, 0); | 120 | | - if (off > max_off) { | 121 | | - off = max_off; | 122 | | - } | | 105 | + std::string buf = SPrintF("%s:%i\n%s\n", | | 106 | + filename_string, | | 107 | + linenum, | | 108 | + sourceline.c_str()); | | 109 | + CHECK_GT(buf.size(), 0); | 123 | 110 |
| | 111 | + constexpr int kUnderlineBufsize = 1020; | | 112 | + char underline_buf[kUnderlineBufsize + 4]; | | 113 | + int off = 0; | 124 | 114 | // Print wavy underline (GetUnderline is deprecated). | 125 | 115 | for (int i = 0; i < start; i++) { | 126 | | - if (sourceline[i] == '\0' || off >= max_off) { | | 116 | + if (sourceline[i] == '\0' || off >= kUnderlineBufsize) { | 127 | 117 | break; | 128 | 118 | } | 129 | | - CHECK_LT(off, max_off); | 130 | | - buf[off++] = (sourceline[i] == '\t') ? '\t' : ' '; | | 119 | + CHECK_LT(off, kUnderlineBufsize); | | 120 | + underline_buf[off++] = (sourceline[i] == '\t') ? '\t' : ' '; | 131 | 121 | } | 132 | 122 | for (int i = start; i < end; i++) { | 133 | | - if (sourceline[i] == '\0' || off >= max_off) { | | 123 | + if (sourceline[i] == '\0' || off >= kUnderlineBufsize) { | 134 | 124 | break; | 135 | 125 | } | 136 | | - CHECK_LT(off, max_off); | 137 | | - buf[off++] = '^'; | | 126 | + CHECK_LT(off, kUnderlineBufsize); | | 127 | + underline_buf[off++] = '^'; | 138 | 128 | } | 139 | | - CHECK_LE(off, max_off); | 140 | | - buf[off] = '\n'; | 141 | | - buf[off + 1] = '\0'; | | 129 | + CHECK_LE(off, kUnderlineBufsize); | | 130 | + underline_buf[off++] = '\n'; | 142 | 131 |
| 143 | 132 | *added_exception_line = true; | 144 | | - return std::string(buf); | | 133 | + return buf + std::string(underline_buf, off); | 145 | 134 | } | 146 | 135 |
| 147 | 136 | void PrintStackTrace(Isolate* isolate, Local<StackTrace> stack) { | @@ -154,9 +143,9 @@ void PrintStackTrace(Isolate* isolate, Local<StackTrace> stack) { | 154 | 143 |
| 155 | 144 | if (stack_frame->IsEval()) { | 156 | 145 | if (stack_frame->GetScriptId() == Message::kNoScriptIdInfo) { | 157 | | - fprintf(stderr, " at [eval]:%i:%i\n", line_number, column); | | 146 | + FPrintF(stderr, " at [eval]:%i:%i\n", line_number, column); | 158 | 147 | } else { | 159 | | - fprintf(stderr, | | 148 | + FPrintF(stderr, | 160 | 149 | " at [eval] (%s:%i:%i)\n", | 161 | 150 | *script_name, | 162 | 151 | line_number, | @@ -166,12 +155,12 @@ void PrintStackTrace(Isolate* isolate, Local<StackTrace> stack) { | 166 | 155 | } | 167 | 156 |
| 168 | 157 | if (fn_name_s.length() == 0) { | 169 | | - fprintf(stderr, " at %s:%i:%i\n", *script_name, line_number, column); | | 158 | + FPrintF(stderr, " at %s:%i:%i\n", script_name, line_number, column); | 170 | 159 | } else { | 171 | | - fprintf(stderr, | | 160 | + FPrintF(stderr, | 172 | 161 | " at %s (%s:%i:%i)\n", | 173 | | - *fn_name_s, | 174 | | - *script_name, | | 162 | + fn_name_s, | | 163 | + script_name, | 175 | 164 | line_number, | 176 | 165 | column); | 177 | 166 | } | @@ -189,8 +178,8 @@ void PrintException(Isolate* isolate, | 189 | 178 | bool added_exception_line = false; | 190 | 179 | std::string source = | 191 | 180 | GetErrorSource(isolate, context, message, &added_exception_line); | 192 | | - fprintf(stderr, "%s\n", source.c_str()); | 193 | | - fprintf(stderr, "%s\n", *reason); | | 181 | + FPrintF(stderr, "%s\n", source); | | 182 | + FPrintF(stderr, "%s\n", reason); | 194 | 183 |
| 195 | 184 | Local<v8::StackTrace> stack = message->GetStackTrace(); | 196 | 185 | if (!stack.IsEmpty()) PrintStackTrace(isolate, stack); | @@ -235,7 +224,7 @@ void AppendExceptionLine(Environment* env, | 235 | 224 | env->set_printed_error(true); | 236 | 225 |
| 237 | 226 | ResetStdio(); | 238 | | - PrintErrorString("\n%s", source.c_str()); | | 227 | + FPrintF(stderr, "\n%s", source); | 239 | 228 | return; | 240 | 229 | } | 241 | 230 |
| @@ -350,10 +339,10 @@ static void ReportFatalException(Environment* env, | 350 | 339 | // range errors have a trace member set to undefined | 351 | 340 | if (trace.length() > 0 && !stack_trace->IsUndefined()) { | 352 | 341 | if (arrow.IsEmpty() || !arrow->IsString() || decorated) { | 353 | | - PrintErrorString("%s\n", *trace); | | 342 | + FPrintF(stderr, "%s\n", trace); | 354 | 343 | } else { | 355 | 344 | node::Utf8Value arrow_string(env->isolate(), arrow); | 356 | | - PrintErrorString("%s\n%s\n", *arrow_string, *trace); | | 345 | + FPrintF(stderr, "%s\n%s\n", arrow_string, trace); | 357 | 346 | } | 358 | 347 | } else { | 359 | 348 | // this really only happens for RangeErrors, since they're the only | @@ -371,76 +360,40 @@ static void ReportFatalException(Environment* env, | 371 | 360 | if (message.IsEmpty() || message.ToLocalChecked()->IsUndefined() || | 372 | 361 | name.IsEmpty() || name.ToLocalChecked()->IsUndefined()) { | 373 | 362 | // Not an error object. Just print as-is. | 374 | | - String::Utf8Value message(env->isolate(), error); | | 363 | + node::Utf8Value message(env->isolate(), error); | 375 | 364 |
| 376 | | - PrintErrorString("%s\n", | 377 | | - *message ? *message : "<toString() threw exception>"); | | 365 | + FPrintF(stderr, "%s\n", | | 366 | + *message ? message.ToString() : "<toString() threw exception>"); | 378 | 367 | } else { | 379 | 368 | node::Utf8Value name_string(env->isolate(), name.ToLocalChecked()); | 380 | 369 | node::Utf8Value message_string(env->isolate(), message.ToLocalChecked()); | 381 | 370 |
| 382 | 371 | if (arrow.IsEmpty() || !arrow->IsString() || decorated) { | 383 | | - PrintErrorString("%s: %s\n", *name_string, *message_string); | | 372 | + FPrintF(stderr, "%s: %s\n", name_string, message_string); | 384 | 373 | } else { | 385 | 374 | node::Utf8Value arrow_string(env->isolate(), arrow); | 386 | | - PrintErrorString( | 387 | | - "%s\n%s: %s\n", *arrow_string, *name_string, *message_string); | | 375 | + FPrintF(stderr, | | 376 | + "%s\n%s: %s\n", arrow_string, name_string, message_string); | 388 | 377 | } | 389 | 378 | } | 390 | 379 |
| 391 | 380 | if (!env->options()->trace_uncaught) { | 392 | | - PrintErrorString("(Use `node --trace-uncaught ...` to show " | 393 | | - "where the exception was thrown)\n"); | | 381 | + FPrintF(stderr, "(Use `node --trace-uncaught ...` to show " | | 382 | + "where the exception was thrown)\n"); | 394 | 383 | } | 395 | 384 | } | 396 | 385 |
| 397 | 386 | if (env->options()->trace_uncaught) { | 398 | 387 | Local<StackTrace> trace = message->GetStackTrace(); | 399 | 388 | if (!trace.IsEmpty()) { | 400 | | - PrintErrorString("Thrown at:\n"); | | 389 | + FPrintF(stderr, "Thrown at:\n"); | 401 | 390 | PrintStackTrace(env->isolate(), trace); | 402 | 391 | } | 403 | 392 | } | 404 | 393 |
| 405 | 394 | fflush(stderr); | 406 | 395 | } | 407 | 396 |
| 408 | | -void PrintErrorString(const char* format, ...) { | 409 | | - va_list ap; | 410 | | - va_start(ap, format); | 411 | | -#ifdef _WIN32 | 412 | | - HANDLE stderr_handle = GetStdHandle(STD_ERROR_HANDLE); | 413 | | - | 414 | | - // Check if stderr is something other than a tty/console | 415 | | - if (stderr_handle == INVALID_HANDLE_VALUE || stderr_handle == nullptr || | 416 | | - uv_guess_handle(_fileno(stderr)) != UV_TTY) { | 417 | | - vfprintf(stderr, format, ap); | 418 | | - va_end(ap); | 419 | | - return; | 420 | | - } | 421 | | - | 422 | | - // Fill in any placeholders | 423 | | - int n = _vscprintf(format, ap); | 424 | | - std::vector<char> out(n + 1); | 425 | | - vsprintf(out.data(), format, ap); | 426 | | - | 427 | | - // Get required wide buffer size | 428 | | - n = MultiByteToWideChar(CP_UTF8, 0, out.data(), -1, nullptr, 0); | 429 | | - | 430 | | - std::vector<wchar_t> wbuf(n); | 431 | | - MultiByteToWideChar(CP_UTF8, 0, out.data(), -1, wbuf.data(), n); | 432 | | - | 433 | | - // Don't include the null character in the output | 434 | | - CHECK_GT(n, 0); | 435 | | - WriteConsoleW(stderr_handle, wbuf.data(), n - 1, nullptr, nullptr); | 436 | | -#elif defined(__ANDROID__) | 437 | | - __android_log_vprint(ANDROID_LOG_ERROR, "nodejs", format, ap); | 438 | | -#else | 439 | | - vfprintf(stderr, format, ap); | 440 | | -#endif | 441 | | - va_end(ap); | 442 | | -} | 443 | | - | 444 | 397 | [[noreturn]] void FatalError(const char* location, const char* message) { | 445 | 398 | OnFatalError(location, message); | 446 | 399 | // to suppress compiler warning | @@ -449,9 +402,9 @@ void PrintErrorString(const char* format, ...) { | 449 | 402 |
| 450 | 403 | void OnFatalError(const char* location, const char* message) { | 451 | 404 | if (location) { | 452 | | - PrintErrorString("FATAL ERROR: %s %s\n", location, message); | | 405 | + FPrintF(stderr, "FATAL ERROR: %s %s\n", location, message); | 453 | 406 | } else { | 454 | | - PrintErrorString("FATAL ERROR: %s\n", message); | | 407 | + FPrintF(stderr, "FATAL ERROR: %s\n", message); | 455 | 408 | } | 456 | 409 | #ifdef NODE_REPORT | 457 | 410 | Isolate* isolate = Isolate::GetCurrent(); | |
Collapse file Copy file name to clipboardExpand all lines: src/node_errors.h -2Lines changed: 0 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -25,8 +25,6 @@ void AppendExceptionLine(Environment* env, | 25 | 25 | [[noreturn]] void FatalError(const char* location, const char* message); | 26 | 26 | void OnFatalError(const char* location, const char* message); | 27 | 27 |
| 28 | | -void PrintErrorString(const char* format, ...); | 29 | | - | 30 | 28 | // Helpers to construct errors similar to the ones provided by | 31 | 29 | // lib/internal/errors.js. | 32 | 30 | // Example: with `V(ERR_INVALID_ARG_TYPE, TypeError)`, there will be | |
Collapse file Copy file name to clipboardExpand all lines: src/node_process_methods.cc +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | #include "base_object-inl.h" | | 2 | +#include "debug_utils-inl.h" | 2 | 3 | #include "env-inl.h" | 3 | 4 | #include "node.h" | 4 | 5 | #include "node_errors.h" | @@ -216,7 +217,7 @@ void RawDebug(const FunctionCallbackInfo<Value>& args) { | 216 | 217 | CHECK(args.Length() == 1 && args[0]->IsString() && | 217 | 218 | "must be called with a single string"); | 218 | 219 | Utf8Value message(args.GetIsolate(), args[0]); | 219 | | - PrintErrorString("%s\n", *message); | | 220 | + FPrintF(stderr, "%s\n", message); | 220 | 221 | fflush(stderr); | 221 | 222 | } | 222 | 223 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/util.h +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -473,6 +473,8 @@ class ArrayBufferViewContents { | 473 | 473 | class Utf8Value : public MaybeStackBuffer<char> { | 474 | 474 | public: | 475 | 475 | explicit Utf8Value(v8::Isolate* isolate, v8::Local<v8::Value> value); | | 476 | + | | 477 | + inline std::string ToString() const { return std::string(out(), length()); } | 476 | 478 | }; | 477 | 479 |
| 478 | 480 | class TwoByteValue : public MaybeStackBuffer<uint16_t> { | @@ -483,6 +485,8 @@ class TwoByteValue : public MaybeStackBuffer<uint16_t> { | 483 | 485 | class BufferValue : public MaybeStackBuffer<char> { | 484 | 486 | public: | 485 | 487 | explicit BufferValue(v8::Isolate* isolate, v8::Local<v8::Value> value); | | 488 | + | | 489 | + inline std::string ToString() const { return std::string(out(), length()); } | 486 | 490 | }; | 487 | 491 |
| 488 | 492 | #define SPREAD_BUFFER_ARG(val, name) \ | |
|