<commit_before>﻿import events = require("events");
import util = require("util");

import EventEmitter = events.EventEmitter;

export interface ILogWriter {
    trace(format: string, ...params: any[]): void;
    trace(obj: Object, format?: string, ...params: any[]): void;
    debug(format: string, ...params: any[]): void;
    debug(obj: Object, format?: string, ...params: any[]): void;
    info(format: string, ...params: any[]): void;
    info(obj: Object, format?: string, ...params: any[]): void;
    warn(format: string, ...params: any[]): void;
    warn(obj: Object, format?: string, ...params: any[]): void;
    error(format: string, ...params: any[]): void;
    error(obj: Object, format?: string, ...params: any[]): void;
    fatal(format: string, ...params: any[]): void;
    fatal(obj: Object, format?: string, ...params: any[]): void;
    level(): string|number;
}

export function toLogWriter(writer?: ILogWriter): ILogWriter {

    function check(names: string[]) {
        if (typeof writer !== "object") return false;

        for (var i = 0; i < names.length; i++) {
            if (typeof writer[names[i]] !== "function") return false;
        }

        return true;
    };

    var levels = ["trace", "debug", "info", "warn", "error", "fatal"];

    if (writer == null || typeof writer === "undefined") {
        // no writer specified, create a dummy writer
        var proxy = <ILogWriter>new Object();

        levels.forEach(level => {
            proxy[level] = (obj?: Object, format?: any, ...params: any[]): void => { };
        });

        proxy["level"] = () => { return 90; }

        return <ILogWriter>proxy;
    }

    if (check(levels)) {
        // looks like bunyan, great!
        return writer;
    }

    // #if NODE
    if (check(["log", "debug", "info", "warn", "error", "query"])) {
        // looks like winston, lets's create a proxy for it
        var proxy = <ILogWriter>new Object();

        levels.forEach(level => {
            proxy[level] = (obj?: Object, format?: any, ...params: any[]): void => {
                // log(level: string, msg: string, meta: any, callback ?: (err: Error, level: string, msg: string, meta: any) => void): LoggerInstance;
                if (typeof obj === "string") {
                    var msg = util.format(obj, format, params);
                    (<any>writer).log(level, msg);
                } else {
                    var msg = util.format(format, params);
                    (<any>writer).log(level, msg, obj);
                }
            };
        });

        proxy["level"] = () => { return (<any>writer).level; }

        return <ILogWriter>proxy;
    }
    // #endif

    if (check(["log", "info", "warn", "error", "dir"])) {
        // looks like console, lets's create a proxy for it
        var proxy = <ILogWriter>new Object();
        var console = <Console><any>writer;

        levels.forEach(level => {
            proxy[level] = function (obj?: Object, format?: any, ...params: any[]): void {

                // force actual console "log levels"
                var func;
                switch (level) {
                    case "trace":
                    case "debug":
                        func = "log";
                        break;
                    case "fatal":
                        func = "error";
                        break;
                    default:
                        func = level;
                        break;
                }

                var array = params;
                if (typeof format !== "undefined") array.unshift(format);
                if (typeof obj === "string") {
                    array.unshift(obj);
                } else {
                    array.push(obj);
                }
                
                array = [level.toUpperCase() + ":", util.format.apply(util, array)]; // WEB: array.push("(" + level.toUpperCase() + ")");

                (<Function>console[func]).apply(console, array);
            };
        });

        proxy["level"] = () => { return "debug"; }

        return <ILogWriter>proxy;
    }<commit_msg>Improved console-based log writer's readability<commit_after>﻿import events = require("events");
import util = require("util");

import EventEmitter = events.EventEmitter;

export interface ILogWriter {
    trace(format: string, ...params: any[]): void;
    trace(obj: Object, format?: string, ...params: any[]): void;
    debug(format: string, ...params: any[]): void;
    debug(obj: Object, format?: string, ...params: any[]): void;
    info(format: string, ...params: any[]): void;
    info(obj: Object, format?: string, ...params: any[]): void;
    warn(format: string, ...params: any[]): void;
    warn(obj: Object, format?: string, ...params: any[]): void;
    error(format: string, ...params: any[]): void;
    error(obj: Object, format?: string, ...params: any[]): void;
    fatal(format: string, ...params: any[]): void;
    fatal(obj: Object, format?: string, ...params: any[]): void;
    level(): string|number;
}

export function toLogWriter(writer?: ILogWriter): ILogWriter {

    function check(names: string[]) {
        if (typeof writer !== "object") return false;

        for (var i = 0; i < names.length; i++) {
            if (typeof writer[names[i]] !== "function") return false;
        }

        return true;
    };

    var levels = ["trace", "debug", "info", "warn", "error", "fatal"];

    if (writer == null || typeof writer === "undefined") {
        // no writer specified, create a dummy writer
        var proxy = <ILogWriter>new Object();

        levels.forEach(level => {
            proxy[level] = (obj?: Object, format?: any, ...params: any[]): void => { };
        });

        proxy["level"] = () => { return 90; }

        return <ILogWriter>proxy;
    }

    if (check(levels)) {
        // looks like bunyan, great!
        return writer;
    }

    // #if NODE
    if (check(["log", "debug", "info", "warn", "error", "query"])) {
        // looks like winston, lets's create a proxy for it
        var proxy = <ILogWriter>new Object();

        levels.forEach(level => {
            proxy[level] = (obj?: Object, format?: any, ...params: any[]): void => {
                // log(level: string, msg: string, meta: any, callback ?: (err: Error, level: string, msg: string, meta: any) => void): LoggerInstance;
                if (typeof obj === "string") {
                    var msg = util.format(obj, format, params);
                    (<any>writer).log(level, msg);
                } else {
                    var msg = util.format(format, params);
                    (<any>writer).log(level, msg, obj);
                }
            };
        });

        proxy["level"] = () => { return (<any>writer).level; }

        return <ILogWriter>proxy;
    }
    // #endif

    if (check(["log", "info", "warn", "error", "dir"])) {
        // looks like console, lets's create a proxy for it
        var proxy = <ILogWriter>new Object();
        var console = <Console><any>writer;

        levels.forEach(level => {
            proxy[level] = function (obj?: Object, format?: any, ...params: any[]): void {

                // force actual console "log levels"
                var func;
                switch (level) {
                    case "trace":
                    case "debug":
                        func = "log";
                        break;
                    case "fatal":
                        func = "error";
                        break;
                    default:
                        func = level;
                        break;
                }

                var array = params;
                if (typeof format !== "undefined") array.unshift(format);
                if (typeof obj === "string" || obj === null) {
                    array.unshift(obj);
                    obj = null;
                }
                
                array = [level.toUpperCase() + ":", util.format.apply(util, array)]; // WEB: array.push("(" + level.toUpperCase() + ")");

                (<Function>console[func]).apply(console, array);
                if (obj !== null) (<Function>console[func]).call(console, obj);
            };
        });

        proxy["level"] = () => { return (<any>console).level || "debug"; }

        return <ILogWriter>proxy;
    }
{'fraction_non_alphanumeric': 0.16810551558752998, 'fraction_numerical': 0.0007194244604316547, 'mean_word_length': 1.7582671957671958, 'pattern_counts': {'":': 8, '<': 31, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6765578', 'n_tokens_mistral': 2362, 'n_tokens_neox': 2230, 'n_words': 654}