Daniel@0: "use strict"; Daniel@0: Daniel@0: /* Daniel@0: * Logging is controlled by Logger library. Each module has got its own named logger, Daniel@0: * the app has its own named logger too (App.logger) Daniel@0: */ Daniel@0: App.addInitializer(function(options){ Daniel@0: // Default log level Daniel@0: Logger.setLevel( Daniel@0: // Logger.ERROR Daniel@0: Logger.WARN Daniel@0: // Logger.INFO Daniel@0: // Logger.DEBUG Daniel@0: ); Daniel@0: Daniel@0: // App logger Daniel@0: App.logger = Logger.get("App"); Daniel@0: App.logger.setLevel(Logger.DEBUG); Daniel@0: Daniel@0: // Replace the default handle of the Logger to prepend time coordinate and the name of the logger Daniel@0: // Based on a native setHandler in js-logger (time, time diff and logger name are prepended) Daniel@0: var previousLogTime = null; Daniel@0: Logger.setHandler(function (originalArguments, context) { Daniel@0: var hdlr = console.log; Daniel@0: var messages = Array.prototype.slice.call(originalArguments); Daniel@0: Daniel@0: // Prepend the logger's name to the log message for easy identification. Daniel@0: if (context.name) { Daniel@0: messages.unshift(_.str.pad(_.str.sprintf("[%s]", context.name), 25, " ", "right")); Daniel@0: } Daniel@0: // Prepend time diff Daniel@0: var logDate = new Date(); Daniel@0: var logTime = logDate.getTime(); Daniel@0: if (previousLogTime && logTime - previousLogTime < 3000) { Daniel@0: messages.unshift(_.str.sprintf(" %4d", logTime - previousLogTime )); Daniel@0: } else { Daniel@0: messages.unshift(" "); Daniel@0: } Daniel@0: messages.unshift(_.str.sprintf("%s.%03d", logDate.toTimeString().slice(0, 8),logTime % 1000)); Daniel@0: previousLogTime = logTime; Daniel@0: Daniel@0: // Delegate through to custom warn/error loggers if present on the console. Daniel@0: if (context.level === Logger.WARN && console.warn) { Daniel@0: hdlr = console.warn; Daniel@0: } else if (context.level === Logger.ERROR && console.error) { Daniel@0: hdlr = console.error; Daniel@0: } else if (context.level === Logger.INFO && console.info) { Daniel@0: hdlr = console.info; Daniel@0: } Daniel@0: Daniel@0: // Support for IE8+ (and other, slightly more sane environments) Daniel@0: Function.prototype.apply.call(hdlr, console, messages); Daniel@0: }); Daniel@0: Daniel@0: // Log all errors in code (unless jasmine is on) Daniel@0: if (!options.enableJasmine) { Daniel@0: window.onerror = function(message, file, line) { Daniel@0: App.logger.error("[unhandled]", message, file, line); Daniel@0: }; Daniel@0: } Daniel@0: Daniel@0: });