|
| 1 | +import * as vscode from 'vscode'; |
| 2 | + |
| 3 | +enum LogLevel { |
| 4 | + error = 1, |
| 5 | + warning = 2, |
| 6 | + info = 3, |
| 7 | + log = 4, |
| 8 | + debug = 5 |
| 9 | +} |
| 10 | + |
| 11 | +export interface LogMessage { |
| 12 | + type: number; |
| 13 | + message: string; |
| 14 | + level: number; |
| 15 | +} |
| 16 | + |
| 17 | +export class VscodeLogger { |
| 18 | + private static _outputChannel: vscode.OutputChannel; |
| 19 | + static get outputChannel(): vscode.OutputChannel { |
| 20 | + if (!VscodeLogger._outputChannel) { |
| 21 | + VscodeLogger._outputChannel = vscode.window.createOutputChannel('VBAPro Output'); |
| 22 | + VscodeLogger._outputChannel.show(); |
| 23 | + } |
| 24 | + return VscodeLogger._outputChannel; |
| 25 | + } |
| 26 | + |
| 27 | + static info = (msg: string, lvl?: number) => this.log(LogLevel.info, msg, lvl) |
| 28 | + static debug = (msg: string, lvl?: number) => this.log(LogLevel.debug, msg, lvl) |
| 29 | + |
| 30 | + static logMessage(params: LogMessage): void { |
| 31 | + this.log(params.type, params.message, params.level); |
| 32 | + } |
| 33 | + |
| 34 | + private static log(type: LogLevel, msg: string, lvl?: number): void { |
| 35 | + const i = '> '.repeat(lvl ?? 0); |
| 36 | + const t = `${this.getFormattedTimestamp()}`; |
| 37 | + VscodeLogger.outputChannel.appendLine(`${t} [${LogLevel[type]}] ${i}${msg}`); |
| 38 | + } |
| 39 | + |
| 40 | + private static getFormattedTimestamp(): string { |
| 41 | + const now = new Date(); |
| 42 | + |
| 43 | + const year = now.getFullYear(); |
| 44 | + const month = (now.getMonth() + 1).toString().padStart(2, "0"); |
| 45 | + const day = now.getDate().toString().padStart(2, "0"); |
| 46 | + |
| 47 | + const hours = now.getHours().toString().padStart(2, "0"); |
| 48 | + const minutes = now.getMinutes().toString().padStart(2, "0"); |
| 49 | + const seconds = now.getSeconds().toString().padStart(2, "0"); |
| 50 | + const milliseconds = now.getMilliseconds().toString().padStart(3, "0"); |
| 51 | + |
| 52 | + return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}.${milliseconds}`; |
| 53 | + } |
| 54 | +} |
| 55 | + |
0 commit comments