All files / src/debug debug.ts

0% Statements 0/8
0% Branches 0/2
0% Functions 0/3
0% Lines 0/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47                                                                                             
export enum DebugLevel {
  NONE = 0,
  ALL = 1,
}
 
/**
 * A window.console like utility but with some added features.
 *
 * ```ts
 *  import { debug, DebugLevel } from 'degu/lib/debug/debug';
 * // Use debug.log as a replacement for console.log
 * debug.log('hohoho', myvariable);
 *
 * // Now suppress console.log for prod environment.
 * debug.setDebugLevel(DebugLevel.NONE);
 * ```
 *
 * You could also use urlParams or set other conditions to display logs.
 *
 * ```
 * import { debug, DebugLevel } from 'degu/debug/debug';
 * import {urlParams} from 'degu/lib/dom/url-params';
 * if(urlParams.isTrue('debug')) {
 *   debug.setDebugLevel(DebugLevel.ALL);
 *  } else {
 *   debug.setDebugLevel(DebugLevel.NONE);
 *  }
 * ```
 */
export class debug {
  private static debugLevel: DebugLevel = DebugLevel.ALL;
 
  static setDebugLevel(level: DebugLevel) {
    debug.debugLevel = level;
  }
 
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  static log(...args: any[]) {
    if (debug.debugLevel === DebugLevel.NONE) {
      return;
    }
 
    const argumentsList = Array.prototype.slice.call(args);
    console.log(...argumentsList);
  }
}