(function() {
    var jsm = { };
    if (typeof XPCOMUtils == "undefined") {    
        Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    }
    
    XPCOMUtils.defineLazyGetter(jsm, "utils", function() {
        let obj = { };
        Cu["import"]("resource://livemargins/utils.jsm", obj);
        return obj.utils;
    });
    
    XPCOMUtils.defineLazyGetter(jsm, "Log4Moz", function() {
        let obj = { };
        Cu["import"]("resource://livemargins/3rd/log4moz.js", obj);
        return obj.Log4Moz;
    });
    
    function setupLogging() {
        // The basic formatter will output lines like:
        // DATE/TIME LoggerName LEVEL (log message)
        var formatter = new jsm.Log4Moz.BasicFormatter();
        
        // Loggers are hierarchical, lowering this log level will affect all output
        var root = jsm.Log4Moz.repository.rootLogger;
        root.level = jsm.Log4Moz.Level['All'];
    
        if (jsm.utils.getPref('logging.console.use', false)) {    
            // A console appender outputs to the JS Error Console
            var capp = new jsm.Log4Moz.ConsoleAppender(formatter);
            capp.level = jsm.utils.getPref('logging.console.level', jsm.Log4Moz.Level['Info']);
            root.addAppender(capp);
        }
        
        if (jsm.utils.getPref('logging.dump.use', false)) {    
            // A dump appender outputs to standard out
            var dapp = new jsm.Log4Moz.DumpAppender(formatter);
            dapp.level = jsm.utils.getPref('logging.dump.level', jsm.Log4Moz.Level['Info']);
            root.addAppender(dapp);
        }
    }
    
    window.addEventListener('load', function() {
        window.setTimeout(setupLogging, 500);
        window.removeEventListener("load", arguments.callee, false);
    }, false);
})();
