1 (изменено: Rumata, 2013-06-18 12:47:17)

Тема: WSH/JS/VBS: Firebug console для WSH

Имитация известного плагина Firebug. Этот код можно было бы считать избыточным, так как WSH имеет достаточно универсальных средств для визуализации сообщений. Тем не менее, я реализовал этот код, подстроив его под среду.

Скрипт реализует следующие возможности:

Вывод различных типов сообщений:

console.log(object[, object, ...])
console.debug(object[, object, ...])
console.info(object[, object, ...])
console.warn(object[, object, ...])
console.error(object[, object, ...])

Проверка: если выражение false, выводит сообщение

console.assert(expression[, object, ...])

Запуск/остановка именованного таймера и вывод прошедшего времени

console.time(name)
console.timeEnd(name)

Примеры

// простой вывод
console.log("To be or not to be");

// форматированный вывод в стиле printf
// выведет строку "12 monkeys"
console.log("%d %s", 12, "monkeys");

// вывод сообщения если выражение ложно
console.assert(false);

// замерить время исполнения
console.time('loop');
for (var i = 0; i < 1000; i++) {}
console.timeEnd('loop');

Предусмотрено расширение возможностей, например, изменить формат выводимой строки, или организовать вывод сообщений в диалоговых окнах


var title = {
    16: 'ERROR', 
    48: 'WARNING', 
    64: 'INFO', 
    0: 'INFO'
};
console.fn.print = function(msgType, msg)
{
    WScript.StdOut.WriteLine(new Date() + ': ' + title[msgType] + ': ' + msg);
};

Function CustomPrint(MsgType, Msg)
    MsgBox Msg, MsgType
End Function

Set console.fn.print = GetRef("CustomPrint")

Последний пример показывает, что консоль совместима с VBScript.

Актуальная версия скрипта
http://code.google.com/p/jsxt/source/br … console.js

Исходник


(function()
{

    // Be sure to prevent defining it twice
    if ( this.console && console.fn ) {
        return;
    }


    // The main object
    this.console = {};


    // Details for the complex object
    // If JSON object is defined JSON.stringify will be used
    var _inspect = typeof JSON != 'undefined' && typeof JSON.stringify == 'function' 
        ? 
        function(object)
        {
            return object && typeof object == 'object' ? JSON.stringify(object) : object;
        } 
        : 
        function(object)
        {
            return object;
        };

    // This regular expression is used to recongnize a formatting string 
    var reFormat = /%%|%(\d+)?([idfxso])/g;

    // Checks that the object is a formatting string
    var _isFormat = function(object)
    {
        return String(object).match(reFormat);
    };

    var _formatters = {};
    _formatters.i = function(v) { return Number(v).toFixed(0); };
    _formatters.d = 
    _formatters.f = function(v) { return Number(v).toString(10); };
    _formatters.x = function(v) { return Number(v).toString(16); }, 
    _formatters.o = _inspect;
    _formatters.s = function(v) { return String(v); };

    // The formatting function immitating C-like "printf"
    var _format = function(pattern, objects)
    {
        var i = 0;
        return pattern.replace(reFormat, function(format, width, id)
        {
            if ( format == '%%' ) {
                return '%';
            }

            i++;

            var r = _formatters[id](objects[i]);

            return r;
        });
    };

    // The printing function
    var _print = function(msgType, msg)
    {
        WScript.Echo(msg);
    };


    // The core function
    var printMsg = function(msgType, objects)
    {
        // Get the actual configuration of the console
        var fn = console.fn;
        var sep = fn.separator || ' ';

        var result;

        if ( fn.isFormat(objects[0]) ) {
            //result = fn.format(objects[0], Array.prototype.slice.call(objects, 1));
            result = fn.format(objects[0], objects);
        } else {
            result = [];
            for (var i = 0; i < objects.length; i++) {
                result.push(fn.inspect(objects[i]));
            }
            result = result.join(sep);
        }

        fn.print(msgType, result);
    };


    var log = function()
    {
        printMsg(0, arguments);
    };

    var debug = log;

    var info = function()
    {
        printMsg(64, arguments);
    };

    var warn = function()
    {
        printMsg(48, arguments);
    };

    var error = function()
    {
        printMsg(16, arguments);
    };


    // If the expression is false, the rest of arguments will be logged in 
    // the console
    var assert = function(expr)
    {
        if ( expr ) {
            return;
        }
        error.apply(console, arguments.length < 2 ? ['Assertion error'] : Array.prototype.slice.call(arguments, 1));
    };


    // Processing of timers start/stop
    var timeNames = {};

    var timeStart = function(name)
    {
        if ( ! name ) {
            return;
        }

        timeNames[name] = new Date();
        log(name + ': Timer started');
    };

    var timeEnd = function(name)
    {
        if ( ! name || ! timeNames.hasOwnProperty(name) ) {
            return;
        }

        var t = new Date() - timeNames[name];
        delete timeNames[name];

        log(name + ': ' + t + 'ms');
    };


    // Stub for the non-implemented methods
    var noop = function()
    {
    };


    console = {
        // Implemented methods
        log: log, 
        debug: debug, 
        info: info, 
        warn: warn, 
        error: error, 

        assert: assert, 

        time: timeStart, 
        timeEnd: timeEnd, 

        // Not implemented methods
        clear: noop, 

        dir: noop, 
        dirxml: noop, 

        group: noop, 
        groupCollapsed: noop, 
        groupEnd: noop, 

        profile: noop, 
        profileEnd: noop, 

        table: noop, 

        trace: noop, 

        // Customizing of the console
        fn: {
            isFormat: _isFormat, 
            format: _format, 
            inspect: _inspect, 
            print: _print, 
            separator: ' '
        }
    };

})();

( 2 * b ) || ! ( 2 * b )