Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log warnings directly to stderr locally #1444

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/workerd/io/worker.c++
Original file line number Diff line number Diff line change
Expand Up @@ -1553,7 +1553,6 @@ void Worker::handleLog(jsg::Lock& js, ConsoleMode consoleMode, LogLevel level,
const v8::Global<v8::Function>& original,
const v8::FunctionCallbackInfo<v8::Value>& info) {
// Call original V8 implementation so messages sent to connected inspector if any
// TODO(now): does this need to run within a handle scope?
auto context = js.v8Context();
int length = info.Length();
v8::Local<v8::Value> args[length + 1]; // + 1 used for `colors` later
Expand Down Expand Up @@ -1670,7 +1669,6 @@ void Worker::handleLog(jsg::Lock& js, ConsoleMode consoleMode, LogLevel level,
auto colors = COLOR_MODE == ColorMode::ENABLED ||
(COLOR_MODE == ColorMode::ENABLED_IF_TTY && tty);

// TODO(now): does this need to run within a handle scope?
auto registry = jsg::ModuleRegistry::from(js);
auto inspectModule = registry->resolveInternalImport(js, "node-internal:internal_inspect"_kj);
auto inspectModuleHandle = inspectModule.getHandle(js).As<v8::Object>();
Expand Down Expand Up @@ -2707,9 +2705,13 @@ void Worker::Isolate::logWarning(kj::StringPtr description, Lock& lock) {
});
}

// TODO(now): should these be logged to stderr like `console.warn()`?
// Run with --verbose to log JS exceptions to stderr. Useful when running tests.
KJ_LOG(INFO, "console warning", description);
if (consoleMode == ConsoleMode::INSPECTOR_ONLY) {
// Run with --verbose to log JS exceptions to stderr. Useful when running tests.
KJ_LOG(INFO, "console warning", description);
} else {
fprintf(stderr, "%s\n", description.cStr());
fflush(stderr);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stderr is unbuffered, fflush probably isn't helping here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that initially too, but when spawning workerd from Node and piping the output to a stream, it looks like you need explicit flushes. See #1343.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I thought it was buffered too, that's surprising. An alternative might be to use setvbuf()/setlinebuf() to enable line buffering for stderr, but that would affect it outside of this scope too.

}
}

void Worker::Isolate::logWarningOnce(kj::StringPtr description, Lock& lock) {
Expand Down