]> Git Repo - pico-vscode.git/blob - src/logger.mts
Merge branch 'main' into main
[pico-vscode.git] / src / logger.mts
1 /* eslint-disable @typescript-eslint/no-unused-vars */
2 type LogLevel = "debug" | "info" | "warn" | "error";
3
4 const logLevel: LogLevel =
5   process.env.BUILD === "production" ? "warn" : "debug";
6
7 // ANSI escape codes for colored console output
8 const red = "\x1b[31m";
9 const green = "\x1b[32m";
10 const yellow = "\x1b[33m";
11 const blue = "\x1b[34m";
12 const magenta = "\x1b[35m";
13 // ANSI escape code to reset color
14 const reset = "\x1b[0m";
15
16 const EXT_LOG_PREFIX = "raspberry-pi-pico";
17
18 /**
19  * Interface for objects that can be converted to a string with the toString() method.
20  */
21 interface Stringable {
22   toString(): string;
23 }
24
25 /**
26  * Log source identifiers for functions that aren't
27  * part of a class and therefore don't have a class name.
28  * Current convention is to use the file name.
29  *
30  * @enum {string}
31  */
32 export enum LoggerSource {
33   githubRestApi = "githubREST",
34   gitDownloader = "downloadGit",
35   downloader = "download",
36   requirements = "requirementsUtil",
37   examples = "examplesUtil",
38   githubApiCache = "githubApiCache",
39   extension = "extension",
40 }
41
42 /**
43  * Logger class to log messages to the console with different log levels and identifiable source.
44  */
45 export default class Logger {
46   private className: string;
47
48   /**
49    * Creates a new Logger instance.
50    *
51    * @param className The name of the class the logger is used in.
52    */
53   constructor(className: string) {
54     this.className = className;
55   }
56
57   /**
58    * Logs the given message to the console.
59    *
60    * @param message The message to log.
61    * @param optionalParams Optional parameters to log.
62    */
63   public static log(
64     message: string | undefined,
65     ...optionalParams: Stringable[]
66   ): void {
67     console.log(`[${EXT_LOG_PREFIX}] ${message}`, ...optionalParams);
68   }
69
70   private static shouldLog(level: LogLevel): boolean {
71     const levels: LogLevel[] = ["debug", "info", "warn", "error"];
72
73     return levels.indexOf(level) >= levels.indexOf(logLevel);
74   }
75
76   public info(message: string, ...optionalParams: Stringable[]): void {
77     Logger.info(this.className, message, ...optionalParams);
78   }
79
80   public static info(
81     source: string | LoggerSource,
82     message: string,
83     ...optionalParams: Stringable[]
84   ): void {
85     if (Logger.shouldLog("info")) {
86       console.info(
87         `[${blue}INFO${reset}] [${EXT_LOG_PREFIX} - ${source}] ${message}`,
88         ...optionalParams
89       );
90     }
91   }
92
93   public warn(message: string, ...optionalParams: Stringable[]): void {
94     Logger.warn(this.className, message, ...optionalParams);
95   }
96
97   public static warn(
98     source: string | LoggerSource,
99     message: string,
100     ...optionalParams: Stringable[]
101   ): void {
102     if (Logger.shouldLog("warn")) {
103       console.warn(
104         `[${yellow}WARN${reset}] [${EXT_LOG_PREFIX} - ${source}] ${message}`,
105         ...optionalParams
106       );
107     }
108   }
109
110   public error(message: string | Error, ...optionalParams: Stringable[]): void {
111     Logger.error(this.className, message, ...optionalParams);
112   }
113
114   public static error(
115     source: string | LoggerSource,
116     message: string | Error,
117     ...optionalParams: Stringable[]
118   ): void {
119     if (Logger.shouldLog("error")) {
120       if (message instanceof Error) {
121         message = message.message;
122       }
123
124       console.error(
125         `[${red}ERROR${reset}] [${EXT_LOG_PREFIX} - ${source}] ${message}`,
126         ...optionalParams
127       );
128     }
129   }
130
131   public debug(message: string, ...optionalParams: Stringable[]): void {
132     Logger.debug(this.className, message, ...optionalParams);
133   }
134
135   public static debug(
136     source: string | LoggerSource,
137     message: string,
138     ...optionalParams: Stringable[]
139   ): void {
140     if (this.shouldLog("debug")) {
141       console.debug(
142         `[${magenta}DEBUG${reset}] [${EXT_LOG_PREFIX} - ${source}] ${message}`,
143         ...optionalParams
144       );
145     }
146   }
147 }
This page took 0.028556 seconds and 4 git commands to generate.