All files / rusted-chromium/log logger.ts

100% Statements 16/16
100% Branches 4/4
100% Functions 9/9
100% Lines 16/16

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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65  12x   12x 12x 12x             12x   19x     19x     25x       1x 1x             1x       2x           2x             2x           5x               12x  
import type { PrinterWriteStream } from '../interfaces/printer.interfaces'
import { Printer } from './printer'
 
export enum DebugMode {
    NONE,
    DEBUG,
}
 
/**
 * Just a simple logger which holds no state. Try to prevent this when giving feedback to the user.
 * Rather try to use a stateful logging like Spinner, Progress or Status
 */
export class Logger extends Printer<Logger> {
    public constructor(stdio: PrinterWriteStream) {
        super(stdio)
    }
 
    private debugMode = DebugMode.NONE
    
    protected self(): Logger {
        return this
    }
 
    public setDebugMode(mode: DebugMode): Logger {
        this.debugMode = mode
        return this
    }
 
    /**
     * No state, no stop
     */
    public stop(): Logger {
        return this
    }
 
    public info(text: string): Logger {
        return this.clearLine()
            .write(this.INFO_FN(text))
            .newline()
    }
 
    public error(text: string): Logger {
        return this.clearLine()
            .write(this.ERROR_FN(text))
            .newline()
 
    }
 
    public warn(text: string): Logger {
        return this.clearLine()
            .write(this.WARN_FN(text))
            .newline()
    }
 
    public debug(text: string): Logger {
        return this.debugMode !== DebugMode.NONE
            ? this.clearLine()
                .write(this.DEBUG_FN(text))
                .newline()
            : this.self()
    }
}
 
export const logger = new Logger(process.stdout)