Skip to content

Commit

Permalink
Add application logging (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
grddavies authored May 17, 2023
1 parent 936a620 commit f668951
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 4 deletions.
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ plugins:
- 'simple-import-sort'
root: true
rules:
no-console: error
# Non-null assertions used for refs in solidJS
'@typescript-eslint/no-non-null-assertion': 0
'@typescript-eslint/no-unused-vars':
Expand Down
4 changes: 2 additions & 2 deletions src/store/AppState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createStore } from 'solid-js/store';
import { NUM_PADS } from 'src/defaults/constants';
import { Defaults } from 'src/defaults/Defaults';
import { SamplePlayer } from 'src/models/SamplePlayer';
import { Logger } from 'src/utils/Logger';

import { Convert } from './Convert';

Expand All @@ -24,7 +25,7 @@ function restoreFromLocalStorage(): AppState | null {
try {
return Convert.toAppState(jsonString);
} catch (e) {
console.error(e);
Logger.error('Failed to parse App state from JSON', e);
return null;
}
}
Expand Down Expand Up @@ -60,4 +61,3 @@ export const [GlobalState, setGlobalState] = createStore<AppState>(initState);
export function persistGlobalState(): void {
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(GlobalState));
}

75 changes: 75 additions & 0 deletions src/utils/Logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Logging severity levels
*/
enum LogLevel {
ERROR,
WARN,
INFO,
DEBUG,
}

/**
* String representations of logging severity levels
*/
type LogLevelName = keyof typeof LogLevel;

/**
* Logger
* Log application events to the console
*/
export class Logger {
private static Loggers = [
/* eslint-disable no-console */
console.error,
console.warn,
console.info,
console.debug,
/* eslint-enable no-console */
];

/**
* Log a message to the console
* @param msg message to write to the console
* @param level the log level to write the message at
* @param params objects to log alongside the message
*/
public static log(
msg: string,
level: LogLevelName,
...params: unknown[]
): void {
Logger.Loggers[LogLevel[level]](`[${level}] ${msg}`, ...params);
}

/**
* Log an error
* @param message describing the error
*/
public static error(msg: string, ...params: unknown[]): void {
Logger.log(msg, 'ERROR', ...params);
}

/**
* Log a warning
* @param message describing the warning
*/
public static warn(msg: string, ...params: unknown[]): void {
Logger.log(msg, 'WARN', ...params);
}

/**
* Log some information
* @param message information to log
*/
public static info(msg: string, ...params: unknown[]): void {
Logger.log(msg, 'INFO', ...params);
}

/**
* Log some debug information
* @param message information to log
*/
public static debug(msg: string, ...params: unknown[]): void {
Logger.log(msg, 'DEBUG', ...params);
}
}
35 changes: 33 additions & 2 deletions src/utils/appInit.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,46 @@
import { Defaults } from 'src/defaults/Defaults';
import { SampleStore } from 'src/samples';

import { Logger } from './Logger';

/**
* Initialise App resources
*/
export async function appInit(): Promise<void> {
SampleStore.init();
await Promise.all(
performance.mark('fetch-sample-start');
const results = await Promise.allSettled(
Defaults.samples.map(async (x) => {
try {
await x.ensureCached();
return Promise.resolve();
} catch (e) {
console.error(e);
Logger.error(`Failed to load sample '${x}'`, e);
return Promise.reject();
}
}),
);
performance.mark('fetch-sample-end');
const counts = results.reduce(
(acc, x) => {
acc[x.status] += 1;
return acc;
},
{
fulfilled: 0,
rejected: 0,
},
);
const measure = performance.measure(
'fetch-sample-duration',
'fetch-sample-start',
'fetch-sample-end',
);
if (counts.fulfilled) {
Logger.info(`Successfully loaded ${counts.fulfilled} samples`);
}
if (counts.rejected) {
Logger.warn(`Failed to load ${counts.rejected} samples`);
}
Logger.debug(`Sample fetch took ${Math.ceil(measure.duration)}ms`);
}

0 comments on commit f668951

Please sign in to comment.