Skip to content

Commit a1d8aac

Browse files
author
Dave Bartolomeo
committed
Better handling of I/O errors when writing to side log
1 parent 56095d3 commit a1d8aac

1 file changed

Lines changed: 25 additions & 6 deletions

File tree

extensions/ql-vscode/src/common/logging/tee-logger.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { appendFile, ensureFile } from "fs-extra";
22
import { isAbsolute } from "path";
3+
import { getErrorMessage } from "../../pure/helpers-pure";
34
import { Logger, LogOptions } from "./logger";
45

56
/**
@@ -11,6 +12,7 @@ import { Logger, LogOptions } from "./logger";
1112
*/
1213
export class TeeLogger implements Logger {
1314
private emittedRedirectMessage = false;
15+
private error = false;
1416

1517
public constructor(
1618
private readonly logger: Logger,
@@ -33,14 +35,31 @@ export class TeeLogger implements Logger {
3335
await this.logger.log(separator);
3436
}
3537

36-
const trailingNewline = options.trailingNewline ?? true;
37-
await ensureFile(this.location);
38+
if (!this.error) {
39+
try {
40+
const trailingNewline = options.trailingNewline ?? true;
41+
await ensureFile(this.location);
3842

39-
await appendFile(this.location, message + (trailingNewline ? "\n" : ""), {
40-
encoding: "utf8",
41-
});
43+
await appendFile(
44+
this.location,
45+
message + (trailingNewline ? "\n" : ""),
46+
{
47+
encoding: "utf8",
48+
},
49+
);
50+
} catch (e) {
51+
// Write an error message to the primary log, and stop trying to write to the side log.
52+
this.error = true;
53+
const errorMessage = getErrorMessage(e);
54+
await this.logger.log(
55+
`Error writing to additional log file: ${errorMessage}`,
56+
);
57+
}
58+
}
4259

43-
await this.logger.log(message, options);
60+
if (!this.error) {
61+
await this.logger.log(message, options);
62+
}
4463
}
4564

4665
show(preserveFocus?: boolean): void {

0 commit comments

Comments
 (0)