|
3 | 3 |
|
4 | 4 | "use strict"; |
5 | 5 |
|
6 | | -import fs = require("fs"); |
| 6 | +import utils = require("./utils"); |
7 | 7 | import os = require("os"); |
8 | 8 | import vscode = require("vscode"); |
9 | 9 |
|
@@ -62,7 +62,7 @@ export class Logger implements ILogger { |
62 | 62 | return vscode.Uri.joinPath(this.logSessionPath, `${baseName}.log`); |
63 | 63 | } |
64 | 64 |
|
65 | | - public writeAtLevel(logLevel: LogLevel, message: string, ...additionalMessages: string[]): void { |
| 65 | + private writeAtLevel(logLevel: LogLevel, message: string, ...additionalMessages: string[]): void { |
66 | 66 | if (logLevel >= this.MinimumLogLevel) { |
67 | 67 | this.writeLine(message, logLevel); |
68 | 68 |
|
@@ -170,22 +170,26 @@ export class Logger implements ILogger { |
170 | 170 | } |
171 | 171 | } |
172 | 172 |
|
173 | | - private writeLine(message: string, level: LogLevel = LogLevel.Normal): void { |
| 173 | + // TODO: Should we await this function above? |
| 174 | + private async writeLine(message: string, level: LogLevel = LogLevel.Normal): Promise<void> { |
174 | 175 | const now = new Date(); |
175 | 176 | const timestampedMessage = |
176 | | - `${now.toLocaleDateString()} ${now.toLocaleTimeString()} [${LogLevel[level].toUpperCase()}] - ${message}`; |
| 177 | + `${now.toLocaleDateString()} ${now.toLocaleTimeString()} [${LogLevel[level].toUpperCase()}] - ${message}${os.EOL}`; |
177 | 178 |
|
178 | 179 | this.logChannel.appendLine(timestampedMessage); |
179 | 180 | if (this.logFilePath && this.MinimumLogLevel !== LogLevel.None) { |
180 | | - fs.appendFile( |
181 | | - this.logFilePath.fsPath, |
182 | | - timestampedMessage + os.EOL, |
183 | | - (err) => { |
184 | | - if (err) { |
185 | | - // tslint:disable-next-line:no-console |
186 | | - console.log(`Error writing to vscode-powershell log file: ${err}`); |
187 | | - } |
188 | | - }); |
| 181 | + try { |
| 182 | + let log = new Uint8Array(); |
| 183 | + if (await utils.checkIfFileExists(this.logFilePath)) { |
| 184 | + log = await vscode.workspace.fs.readFile(this.logFilePath); |
| 185 | + } |
| 186 | + await vscode.workspace.fs.writeFile( |
| 187 | + this.logFilePath, |
| 188 | + Buffer.concat([log, Buffer.from(timestampedMessage)])); |
| 189 | + } catch (e) { |
| 190 | + // tslint:disable-next-line:no-console |
| 191 | + console.log(`Error writing to vscode-powershell log file: ${e}`) |
| 192 | + } |
189 | 193 | } |
190 | 194 | } |
191 | 195 | } |
0 commit comments