1 /* eslint-disable @typescript-eslint/no-unused-vars */
2 type LogLevel = "debug" | "info" | "warn" | "error";
4 const logLevel: LogLevel =
5 process.env.BUILD === "production" ? "warn" : "debug";
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";
16 const EXT_LOG_PREFIX = "raspberry-pi-pico";
19 * Interface for objects that can be converted to a string with the toString() method.
21 interface Stringable {
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.
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",
43 * Logger class to log messages to the console with different log levels and identifiable source.
45 export default class Logger {
46 private className: string;
49 * Creates a new Logger instance.
51 * @param className The name of the class the logger is used in.
53 constructor(className: string) {
54 this.className = className;
58 * Logs the given message to the console.
60 * @param message The message to log.
61 * @param optionalParams Optional parameters to log.
64 message: string | undefined,
65 ...optionalParams: Stringable[]
67 console.log(`[${EXT_LOG_PREFIX}] ${message}`, ...optionalParams);
70 private static shouldLog(level: LogLevel): boolean {
71 const levels: LogLevel[] = ["debug", "info", "warn", "error"];
73 return levels.indexOf(level) >= levels.indexOf(logLevel);
76 public info(message: string, ...optionalParams: Stringable[]): void {
77 Logger.info(this.className, message, ...optionalParams);
81 source: string | LoggerSource,
83 ...optionalParams: Stringable[]
85 if (Logger.shouldLog("info")) {
87 `[${blue}INFO${reset}] [${EXT_LOG_PREFIX} - ${source}] ${message}`,
93 public warn(message: string, ...optionalParams: Stringable[]): void {
94 Logger.warn(this.className, message, ...optionalParams);
98 source: string | LoggerSource,
100 ...optionalParams: Stringable[]
102 if (Logger.shouldLog("warn")) {
104 `[${yellow}WARN${reset}] [${EXT_LOG_PREFIX} - ${source}] ${message}`,
110 public error(message: string | Error, ...optionalParams: Stringable[]): void {
111 Logger.error(this.className, message, ...optionalParams);
115 source: string | LoggerSource,
116 message: string | Error,
117 ...optionalParams: Stringable[]
119 if (Logger.shouldLog("error")) {
120 if (message instanceof Error) {
121 message = message.message;
125 `[${red}ERROR${reset}] [${EXT_LOG_PREFIX} - ${source}] ${message}`,
131 public debug(message: string, ...optionalParams: Stringable[]): void {
132 Logger.debug(this.className, message, ...optionalParams);
136 source: string | LoggerSource,
138 ...optionalParams: Stringable[]
140 if (this.shouldLog("debug")) {
142 `[${magenta}DEBUG${reset}] [${EXT_LOG_PREFIX} - ${source}] ${message}`,