summaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2024-02-23 16:46:10 -0700
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2024-02-23 17:04:54 -0700
commitd0d6aae1e56428f597f69f5c9cfb261afe671f5d (patch)
treeef1d572f2bda148ef1d13602b6b4117be8e18aa4 /src/utils
downloadcps-interpreter-d0d6aae1e56428f597f69f5c9cfb261afe671f5d.tar.gz
cps-interpreter-d0d6aae1e56428f597f69f5c9cfb261afe671f5d.zip
initial parser
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/exception.ts1
-rw-r--r--src/utils/index.ts2
-rw-r--r--src/utils/logger.ts51
3 files changed, 54 insertions, 0 deletions
diff --git a/src/utils/exception.ts b/src/utils/exception.ts
new file mode 100644
index 0000000..625c32a
--- /dev/null
+++ b/src/utils/exception.ts
@@ -0,0 +1 @@
+export class NotImplementedException extends Error {}
diff --git a/src/utils/index.ts b/src/utils/index.ts
new file mode 100644
index 0000000..f44ed5a
--- /dev/null
+++ b/src/utils/index.ts
@@ -0,0 +1,2 @@
+export * from './logger';
+export * from './exception';
diff --git a/src/utils/logger.ts b/src/utils/logger.ts
new file mode 100644
index 0000000..80126c3
--- /dev/null
+++ b/src/utils/logger.ts
@@ -0,0 +1,51 @@
+export interface TracingLogger {
+ info(log: string): void;
+ warn(log: string): void;
+ error(log: string): void;
+ debug(log: string): void;
+
+ createChild(prefix: string): TracingLogger;
+}
+
+export type LogLevel = 'debug' | 'info' | 'warn' | 'error';
+
+export class ConsoleTracingLogger implements TracingLogger {
+ protected prefix: string;
+ private levels: LogLevel[];
+
+ constructor(prefix: string, levels: LogLevel[] = ['warn', 'error']) {
+ this.prefix = prefix;
+ this.levels = levels;
+ }
+
+ private makePrefix(level: LogLevel): string {
+ return `[${new Date().toISOString()}] ${level} (${this.prefix}) > `;
+ }
+
+ public info(log: string) {
+ if (this.levels.includes('info'))
+ console.log(this.makePrefix('info') + log);
+ }
+
+ public warn(log: string) {
+ if (this.levels.includes('warn'))
+ console.warn(this.makePrefix('warn') + log);
+ }
+
+ public error(log: string) {
+ if (this.levels.includes('error'))
+ console.error(this.makePrefix('error') + log);
+ }
+
+ public debug(log: string) {
+ if (this.levels.includes('debug'))
+ console.debug(this.makePrefix('debug') + log);
+ }
+
+ public createChild(prefix: string, levels?: LogLevel[]) {
+ return new ConsoleTracingLogger(
+ `${this.prefix} -> ${prefix}`,
+ levels ?? this.levels,
+ );
+ }
+}