• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14*/
15
16const vscode = require("vscode");
17import * as fs from 'fs';
18import * as path from 'path';
19import { getLogName, getLogPath } from './conf';
20
21const DEBUG_MSG = '[debug]';
22const INFO_MSG = '[info]';
23const WARN_MSG = '[warn]';
24const ERROR_MSG = '[error]';
25
26export class Logger {
27  private logFilePath: string;
28  private maxFileSize: number = 1024 * 1024; // 1MB
29  private static instance: Logger;
30
31  constructor() {
32    let logDirectory: string = getLogPath();
33    let fileName: string = getLogName();
34    this.logFilePath = path.join(logDirectory, fileName);
35    this.initLogFile();
36  }
37
38  static getInstance(): Logger {
39    if (!Logger.instance) {
40      Logger.instance = new Logger();
41    }
42    return Logger.instance;
43  }
44
45  private initLogFile(): void {
46    if (!fs.existsSync(this.logFilePath)) {
47        fs.writeFileSync(this.logFilePath, '');
48    }
49  }
50
51  public debug(message: string) {
52    console.log(message);
53    this.log(DEBUG_MSG + message);
54  }
55
56  public info(message: string) {
57    console.info(message);
58    this.log(INFO_MSG + message);
59  }
60
61  public error(message: string) {
62    console.error(message);
63    this.log(ERROR_MSG + message);
64  }
65
66  public warn(message: string) {
67    console.warn(message);
68    this.log(WARN_MSG + message);
69  }
70
71  public log(message: string): void {
72    this.initLogFile();
73    const timestamp = new Date().toISOString();
74    const logMessage = `${timestamp} -${message}\n`;
75    this.appendLog(logMessage);
76  }
77
78  private appendLog(message: string): void {
79    // Check if the current log file exceeds the max size
80    const stats = fs.statSync(this.logFilePath);
81    if (stats.size + Buffer.byteLength(message) > this.maxFileSize) {
82        this.rotateLogFile();
83    }
84
85    // Append the log message to the file
86    fs.appendFileSync(this.logFilePath, message);
87  }
88
89  private rotateLogFile(): void {
90    // Get the current file name and extension
91    const { name, ext } = path.parse(this.logFilePath);
92    // Generate a new file name with a timestamp
93    const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
94    const newFileName = `${name}-${timestamp}${ext}`;
95    const newFilePath = path.join(path.dirname(this.logFilePath), newFileName);
96
97    // Rename the current log file
98    fs.renameSync(this.logFilePath, newFilePath);
99
100    // Create a new log file
101    fs.writeFileSync(this.logFilePath, '');
102  }
103}