Created
October 24, 2014 05:58
-
-
Save chunpu/e09d9d99457b4c9c100a to your computer and use it in GitHub Desktop.
copy TJ's debug to as3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package { | |
import flash.external.ExternalInterface; | |
public class Log { | |
public static var logs:Array = []; | |
private static const INHERIT:String = 'color:inherit'; | |
private static const PREFIX:String = ''; | |
private static const LS_KEY:String = 'debug'; | |
private static const MAX_LOG_LEN:int = 3000; | |
private static const COLORS:Array = | |
'lightseagreen forestgreen goldenrod dodgerblue darkorchid crimson'.split(' '); | |
private static var colorIndex:int = 0; | |
private static var names:Array = []; | |
private static var skips:Array = []; | |
private static var prev:int; | |
private static var curr:int; | |
private static var ms:int; | |
private var label:String; | |
private var color:String; | |
private var isEnabled:Boolean = false; | |
init(); | |
public function Log(name:String) { | |
isEnabled = shouldEnable(name); | |
if (isEnabled) { | |
color = getColor(); | |
label = PREFIX + name; | |
} | |
} | |
public function debug(...args):void { | |
console('debug', args); | |
} | |
public function error(...args):void { | |
console('error', args); | |
} | |
public function log(...args):void { | |
console('log', args); | |
} | |
public function info(...args):void { | |
console('log', args); | |
} | |
public function warn(...args):void { | |
console('warn', args); | |
} | |
private function console(level:String, args:Array):void { | |
saveLogs(args); | |
if (!isEnabled) return; | |
curr = new Date().getTime(); | |
ms = curr - (prev || curr); | |
prev = curr; | |
var main:String = '%c' + label + '%c'; | |
var arr:Array = ['console.' + level, null, color, INHERIT]; | |
for (var i:int = 0; i < args.length; i++) { | |
arr.push(args[i]); | |
main += ' %o'; | |
} | |
arr.push(color); | |
main += '%c +' + ms + 'ms'; | |
arr[1] = main; | |
ExternalInterface.call.apply(null, arr); | |
} | |
private static function getColor():String { | |
return 'color:' + COLORS[colorIndex++ % COLORS.length]; | |
} | |
private static function shouldEnable(name:String):Boolean { | |
var i:int, reg:RegExp; | |
for (i = 0; reg = skips[i++];) { | |
if (reg.test(name)) return false; | |
} | |
for (i = 0; reg = names[i++];) { | |
if (reg.test(name)) return true; | |
} | |
return false; | |
} | |
private static function saveLogs(args:Array):void { | |
logs.push(args); | |
if (logs.length > MAX_LOG_LEN) { | |
logs.unshift(); | |
} | |
} | |
private static function init():void { | |
var ns:String = getLocalStorage(); | |
if (!ns) return; | |
var split:Array = ns.split(/[\s,]+/); | |
for (var i:int = 0; i < split.length; i++) { | |
var item:String = split[i]; | |
if (!item) continue; | |
item = item.replace(/\*/g, '.*?'); | |
if ('-' == item.charAt(0)) { | |
skips.push(new RegExp('^' + item.substr(1) + '$')); | |
} else { | |
names.push(new RegExp('^' + item + '$')) | |
} | |
} | |
} | |
private static function getLocalStorage():String { | |
var func:String = 'function(arr) {' + | |
'var ls = window.localStorage || {};' + | |
'return ls.' + LS_KEY + '}'; | |
return ExternalInterface.call(func) || ''; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment