-
Notifications
You must be signed in to change notification settings - Fork 108
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
Add prefix output #975
base: main
Are you sure you want to change the base?
Add prefix output #975
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
import * as pathlib from 'path'; | ||
import {unreachable} from '../util/unreachable.js'; | ||
|
||
import type {Event} from '../event.js'; | ||
import type {Event, Stderr, Stdout} from '../event.js'; | ||
import type {Logger, Console} from './logger.js'; | ||
import {type PackageReference, type ScriptReference} from '../config.js'; | ||
import {DiagnosticPrinter} from '../error.js'; | ||
|
@@ -42,6 +42,8 @@ export class DefaultLogger implements Logger { | |
readonly console: Console; | ||
readonly #diagnosticPrinter: DiagnosticPrinter; | ||
|
||
private outputWrite: typeof DefaultLogger.prototype.outputWriteDebug; | ||
|
||
/** | ||
* @param rootPackage The npm package directory that the root script being | ||
* executed belongs to. | ||
|
@@ -50,6 +52,15 @@ export class DefaultLogger implements Logger { | |
this.#rootPackageDir = rootPackage; | ||
this.#diagnosticPrinter = new DiagnosticPrinter(this.#rootPackageDir); | ||
this.console = ourConsole; | ||
this.outputWrite = process.env['WIREIT_LOGGER_PREFIX'] ? this.outputWriteDebug : this.outputWriteDefault; | ||
} | ||
|
||
private outputWriteDefault(this: void, stream: NodeJS.WritableStream, event: Stdout | Stderr) { | ||
stream.write(event.data); | ||
} | ||
|
||
private outputWriteDebug(this: void, stream: NodeJS.WritableStream, event: Stdout | Stderr, label: string) { | ||
stream.write(event.data.toString().split('\n').map(line => line.trim() ? `[${label}] ${line}` : line).join('\n')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't quite right, because output events don't have to map cleanly onto lines. For example, you could have a program that logs "abc" with a pause between each letter. What we want to log here is:
But as you've written this, I believe wireit would log:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't seem to work in the current version There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that looks like a bug as well There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes indeed, but even if it worked (I can send a PR after the discussion), what should be output in the example I posted I see two possible options, or output what is sent
or save the line until I get a transition to a new line (which is, of course, a bad option, since the user needs an answer immediately, and not waiting for the end of the line) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see what you mean. Looking into it our options a bit:
With strategy 3, the output of your example would be:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is super necessary feature for complex service dependency chains. And it looks like this PR has stalled due to the complexity of correctly handling this relatively rare scenario. Couldn't we just add a caveat in the documentation for the env variable letting users know about the effect that would have on their log output? For dev tools, the correctness requirements are often less stringent. For example, the wildly popular I think this feature is too important to delay for this reason. |
||
} | ||
|
||
log(event: Event) { | ||
|
@@ -222,11 +233,11 @@ export class DefaultLogger implements Logger { | |
// TODO(aomarks) More advanced handling of output streams so that | ||
// output isn't simply interweaved. | ||
case 'stdout': { | ||
this.console.stdout.write(event.data); | ||
this.outputWrite(this.console.stdout, event, label); | ||
break; | ||
} | ||
case 'stderr': { | ||
this.console.stderr.write(event.data); | ||
this.outputWrite(this.console.stderr, event, label); | ||
break; | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's put this logic on our
Console
class as a new method that takes an output event and writes it out to the proper underlying stream, using the presence / absence of the env variable to determine whether to use the prefix. That way the implementation lives in a single place.