-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Simplify server logger #17271
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
Simplify server logger #17271
Changes from 1 commit
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 |
|---|---|---|
|
|
@@ -141,8 +141,6 @@ namespace ts.server { | |
| class Logger implements server.Logger { | ||
| private fd = -1; | ||
| private seq = 0; | ||
| private inGroup = false; | ||
| private firstInGroup = true; | ||
|
|
||
| constructor(private readonly logFilename: string, | ||
| private readonly traceToConsole: boolean, | ||
|
|
@@ -172,22 +170,24 @@ namespace ts.server { | |
| } | ||
|
|
||
| perftrc(s: string) { | ||
| this.msg(s, Msg.Perf); | ||
|
Contributor
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. Could we instead turn |
||
| this.msg(s, "Perf"); | ||
| } | ||
|
|
||
| info(s: string) { | ||
| this.msg(s, Msg.Info); | ||
| this.msg(s, "Info"); | ||
| } | ||
|
|
||
| startGroup() { | ||
| this.inGroup = true; | ||
| this.firstInGroup = true; | ||
| err(s: string) { | ||
| this.msg(s, "Err"); | ||
| } | ||
|
|
||
| endGroup() { | ||
| this.inGroup = false; | ||
| group(logGroupEntries: (log: (msg: string) => void) => void) { | ||
| let firstInGroup = false; | ||
| logGroupEntries(s => { | ||
| this.msg(s, "Info", /*inGroup*/ true, firstInGroup); | ||
| firstInGroup = false; | ||
| }); | ||
| this.seq++; | ||
| this.firstInGroup = true; | ||
| } | ||
|
|
||
| loggingEnabled() { | ||
|
|
@@ -198,26 +198,32 @@ namespace ts.server { | |
| return this.loggingEnabled() && this.level >= level; | ||
| } | ||
|
|
||
| msg(s: string, type: Msg.Types = Msg.Err) { | ||
| if (this.fd >= 0 || this.traceToConsole) { | ||
| s = s + "\n"; | ||
| private msg(s: string, type: string, inGroup = false, firstInGroup = false) { | ||
| if (!this.canWrite) return; | ||
|
|
||
| s = s + "\n"; | ||
| if (!inGroup || firstInGroup) { | ||
| const prefix = Logger.padStringRight(type + " " + this.seq.toString(), " "); | ||
| if (this.firstInGroup) { | ||
| s = prefix + s; | ||
| this.firstInGroup = false; | ||
| } | ||
| if (!this.inGroup) { | ||
| this.seq++; | ||
| this.firstInGroup = true; | ||
| } | ||
| if (this.fd >= 0) { | ||
| const buf = new Buffer(s); | ||
| // tslint:disable-next-line no-null-keyword | ||
| fs.writeSync(this.fd, buf, 0, buf.length, /*position*/ null); | ||
| } | ||
| if (this.traceToConsole) { | ||
| console.warn(s); | ||
| } | ||
| s = prefix + s; | ||
| } | ||
| this.write(s); | ||
| if (!inGroup) { | ||
| this.seq++; | ||
| } | ||
| } | ||
|
|
||
| private get canWrite() { | ||
| return this.fd >= 0 || this.traceToConsole; | ||
| } | ||
|
|
||
| private write(s: string) { | ||
| if (this.fd >= 0) { | ||
| const buf = new Buffer(s); | ||
| // tslint:disable-next-line no-null-keyword | ||
| fs.writeSync(this.fd, buf, 0, buf.length, /*position*/ null); | ||
| } | ||
| if (this.traceToConsole) { | ||
| console.warn(s); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,22 +17,11 @@ namespace ts.server { | |
| loggingEnabled(): boolean; | ||
| perftrc(s: string): void; | ||
| info(s: string): void; | ||
| startGroup(): void; | ||
| endGroup(): void; | ||
| msg(s: string, type?: Msg.Types): void; | ||
| err(s: string): void; | ||
| group(logGroupEntries: (log: (msg: string) => void) => void): void; | ||
| getLogFileName(): string; | ||
| } | ||
|
|
||
| export namespace Msg { | ||
|
Contributor
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. Why not turn this into a string enum?
Author
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. There should only be one reference to each kind of message -- one each in |
||
| export type Err = "Err"; | ||
| export const Err: Err = "Err"; | ||
| export type Info = "Info"; | ||
| export const Info: Info = "Info"; | ||
| export type Perf = "Perf"; | ||
| export const Perf: Perf = "Perf"; | ||
| export type Types = Err | Info | Perf; | ||
| } | ||
|
|
||
| function getProjectRootPath(project: Project): Path { | ||
| switch (project.projectKind) { | ||
| case ProjectKind.Configured: | ||
|
|
||
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.
Can you remove the count updating responsibility from printProjects and replace it with
const counter = this.externalProjects.length + this.configuredProjects.length + this.inferredProjects.length. Then printProjects can be lifted out of this function entirely.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.
@sandersn,
counteris used on line 948, though its still feasible to do something like:And then use it like this: