import flash.external.ExternalInterface; // By Josh Santangelo, of endquote.com // Enables logging from Flash to Firebug // http://getfirebug.com/ // Adapted from the flashBug class by Aaron Bassett // http://foobr.co.uk/2007/02/debug_flash_with_firebug/ class console extends Object { static var __instance:console; static var __group:Object; static var __timers:Object; static var RDEPTH:Number = 1; static var getString:Function; public static var TAB:String = "\t"; public static var PREFIX:String = ""; static var PREFIX_LOG:String = "LOG: "; static var PREFIX_INFO:String = "INFO: "; static var PREFIX_DEBUG:String = "DEBUG: "; static var PREFIX_WARNING:String = "WARNING: "; static var PREFIX_ERROR:String = "ERROR: "; static var PREFIX_GROUP:String = " // "; static var BOUNDARY_WARN:String = '--------------------------------------------------------------'; static var BOUNDARY_ERROR:String = '**************************************************************'; // Executing this code before anything else will keep JS errors from appearing when Firebug is not available. See: http://www.getfirebug.com/lite.html static var FIREBUGX:String = 'if (!("console" in window) || !("firebug" in console)) { var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"]; window.console = {}; for (var i = 0; i < names.length; ++i) { window.console[names[i]] = function() {}}}'; // These methods are part of Firebug, but aren't relevant in the land of ActionScript. static var profile:Function; static var profileEnd:Function; static var count:Function; function console() { ExternalInterface.call("window.eval", FIREBUGX); __timers = {}; } static function log(msg:Object):Void { if(!__instance) { __instance = new console(); } trace(PREFIX_LOG +msg); ExternalInterface.call("console.log",msg); } static function info(msg:Object):Void { if(!__instance) { __instance = new console(); } trace(PREFIX_INFO +msg); ExternalInterface.call("console.info",msg); } static function debug(msg:Object):Void { if(!__instance) { __instance = new console(); } trace(PREFIX_DEBUG +msg); ExternalInterface.call("console.debug",msg); } static function warn(msg:Object):Void { if(!__instance) { __instance = new console(); } trace(BOUNDARY_WARN + '\n' + msg + '\n' + BOUNDARY_WARN); ExternalInterface.call("console.warn",msg); } static function error(msg:Object):Void { if(!__instance) { __instance = new console(); } trace(BOUNDARY_ERROR + '\n' + msg + '\n' + BOUNDARY_ERROR); ExternalInterface.call("console.error",msg); } static function group(msg:Object):Void { if(!__instance) { __instance = new console(); } __group = msg; trace(PREFIX_GROUP + "[ Start group: " + msg + " ]"); ExternalInterface.call("console.group",msg); } static function groupEnd():Void { if(!__group) { return; } trace(PREFIX_GROUP + "[ End group: " + __group + " ]"); ExternalInterface.call("console.groupEnd"); __group = undefined; } var __profile = null var PREFIX_PROFILE = ""; static function profile(title:Object):Void { if(!__instance) { __instance = new console(); } __profile = title; trace(PREFIX_PROFILE +"[ Start profile: " + title + " ]"); ExternalInterface.call("console.profile",title); } static function profileEnd():Void { if(!__profile) { return; } trace(PREFIX_PROFILE + "[ End profile: " + __profile + " ]"); ExternalInterface.call("console.profileEnd"); __profile = undefined; } static function time(name:String):Void { if(!__instance) { __instance = new console(); } __timers[name] = getTimer(); ExternalInterface.call("console.time", name); } static function timeEnd(name:String):Number { if(!__timers[name]) { return; } var time:Number = getTimer() - __timers[name]; trace(PREFIX_GROUP + name + ": " + time + "ms"); ExternalInterface.call("console.timeEnd", name); delete(__timers[name]); return time; } static function assert(expression:Boolean, msg:Object):Void { if(!expression) { trace(BOUNDARY_ERROR + '\nAssertion Failure [ ' + msg + ' ]\n' + BOUNDARY_ERROR); } ExternalInterface.call("console.assert", expression,msg); } static function dir(msg:Object):Void { if(!__instance) { __instance = new console(); } ExternalInterface.call("console.dir",msg); } }