1 /* eslint-disable @typescript-eslint/no-unused-vars */
2 type LogLevel = "debug" | "info" | "warn" | "error";
4 const logLevel: LogLevel = process.env.BUILD ? "warn" : "debug";
6 // ANSI escape codes for colored console output
7 const red = "\x1b[31m";
8 const green = "\x1b[32m";
9 const yellow = "\x1b[33m";
10 const blue = "\x1b[34m";
11 const magenta = "\x1b[35m";
12 // ANSI escape code to reset color
13 const reset = "\x1b[0m";
16 * Interface for objects that can be converted to a string with the toString() method.
18 interface Stringable {
23 * Logger class to log messages to the console with different log levels and identifiable source.
25 export default class Logger {
26 private className: string;
29 * Creates a new Logger instance.
31 * @param className The name of the class the logger is used in.
33 constructor(className: string) {
34 this.className = className;
38 * Logs the given message to the console.
40 * @param message The message to log.
41 * @param optionalParams Optional parameters to log.
44 message: string | undefined,
45 ...optionalParams: Stringable[]
47 console.log(`[raspberry-pi-pico] ${message}`, ...optionalParams);
50 private shouldLog(level: LogLevel): boolean {
51 const levels: LogLevel[] = ["debug", "info", "warn", "error"];
53 return levels.indexOf(level) >= levels.indexOf(logLevel);
56 public info(message: string, ...optionalParams: Stringable[]): void {
57 if (this.shouldLog("info")) {
58 console.info(`[INFO] [${this.className}] ${message}`, ...optionalParams);
62 public warn(message: string, ...optionalParams: Stringable[]): void {
63 if (this.shouldLog("warn")) {
65 `[${yellow}WARN${reset}] [${this.className}] ${message}`,
71 public error(message: string | Error, ...optionalParams: Stringable[]): void {
72 if (this.shouldLog("error")) {
73 if (message instanceof Error) {
74 message = message.message;
78 `[${red}ERROR${reset}] [${this.className}] ${message}`,
84 public debug(message: string, ...optionalParams: Stringable[]): void {
85 if (this.shouldLog("debug")) {
87 `[DEBUG] [${this.className}] ${message}`,