Preview

Untitled

1. color.util.ts

enum EColor {
    Red    = 31,
    Green  = 32,
    Yellow = 33,
    Blue   = 34,
    Purple = 35,
    Cyan   = 36,
    Gray   = 37,

    BgRed    = 41,
    BgGreen  = 42,
    BgYellow = 43,
    BgBlue   = 44,
    BgPurple = 45,
    BgCyan   = 46,
    BgGray   = 47,
}

export class Color {
    public static Colorize(msg: any, color: EColor) { return `\\u001b[1;${color}m${msg}\\u001b[0m`; }

    public static Red   (msg: any) { return `\\u001b[1;31m${msg}\\u001b[0m`; }
    public static Green (msg: any) { return `\\u001b[1;32m${msg}\\u001b[0m`; }
    public static Yellow(msg: any) { return `\\u001b[1;33m${msg}\\u001b[0m`; }
    public static Blue  (msg: any) { return `\\u001b[1;34m${msg}\\u001b[0m`; }
    public static Purple(msg: any) { return `\\u001b[1;35m${msg}\\u001b[0m`; }
    public static Cyan  (msg: any) { return `\\u001b[1;36m${msg}\\u001b[0m`; }
    public static Gray  (msg: any) { return `\\u001b[1;37m${msg}\\u001b[0m`; }

    public static BgRed   (msg: any) { return `\\u001b[1;41m${msg}\\u001b[0m`; }
    public static BgGreen (msg: any) { return `\\u001b[1;42m${msg}\\u001b[0m`; }
    public static BgYellow(msg: any) { return `\\u001b[1;43m${msg}\\u001b[0m`; }
    public static BgBlue  (msg: any) { return `\\u001b[1;44m${msg}\\u001b[0m`; }
    public static BgPurple(msg: any) { return `\\u001b[1;45m${msg}\\u001b[0m`; }
    public static BgCyan  (msg: any) { return `\\u001b[1;46m${msg}\\u001b[0m`; }
    public static BgGray  (msg: any) { return `\\u001b[1;47m${msg}\\u001b[0m`; }
}

2. debug.util.ts

import * as Util from 'util';
import { Color, EColor } from './color.util';
import { config } from '../config/config';

export class Debug {

    private static _PrintLineCyan(len: number = 25) {
        console.log(Color.Colorize("─────".repeat(len), EColor.Cyan));
    }
    private static _PrintLineYellow(len: number = 25) {
        console.log(Color.Colorize("─────".repeat(len), EColor.Yellow));
    }
    private static _PrintLineRed(len: number = 25) {
        console.log(Color.Colorize("─────".repeat(len), EColor.Red));
    }

    private static _getCallerPath() {

        var getStackTrace = function() {
            var obj = {} as any;
            Error.captureStackTrace(obj, getStackTrace);
            return obj.stack;
        };

        const stackTrace: string = getStackTrace();
        const stackLines = stackTrace.split('\\n    at ');
        const targetStack = stackLines[3].match(/\\(.+\\)/);
        return targetStack;
    }

    public static Log(...args: any) {

        this._PrintLineCyan();

        if(config.showCommonLogCallerPath) {
            const caller = this._getCallerPath();
            if(caller != null) {
                console.log(`${Color.Yellow("Caller: ")}${caller}`);
                this._PrintLineYellow(20);
            }
        }
        
        console.log(...args);
        console.log();
    }

    public static DeepLog(obj: any) {
        this._PrintLineCyan();
        console.log(Util.inspect(obj, true, null, true));
    }

    public static LogError(msg: string) {

        this._PrintLineCyan();

        if(config.showErrorLogCallerPath) {
            const caller = this._getCallerPath();
            if(caller != null) {
                console.log(`${Color.Yellow("Caller: ")}${caller}`);
                this._PrintLineRed(20);
            }
        }

        console.log(`${Color.Red("[ERROR]")} ${Color.Yellow(msg)}` );
        console.log();
    }
}