| 1 | package { |
|---|
| 2 | import com.rails2u.utils.ObjectUtil; |
|---|
| 3 | import flash.external.ExternalInterface; |
|---|
| 4 | import flash.utils.getQualifiedClassName; |
|---|
| 5 | |
|---|
| 6 | public class console { |
|---|
| 7 | public static const ALL:int = 0; |
|---|
| 8 | public static const DEBUG:int = 2; |
|---|
| 9 | public static const INFO:int = 4; |
|---|
| 10 | public static const WARN:int = 6; |
|---|
| 11 | public static const ERROR:int = 8; |
|---|
| 12 | public static const FATAL:int = 1000; |
|---|
| 13 | |
|---|
| 14 | private static var _level:uint = console.ALL; |
|---|
| 15 | |
|---|
| 16 | public static function get level():uint { |
|---|
| 17 | return console._level; |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | public static function set level(level:uint):void { |
|---|
| 21 | switch (level) { |
|---|
| 22 | case console.ALL: |
|---|
| 23 | case console.DEBUG: |
|---|
| 24 | case console.INFO: |
|---|
| 25 | case console.WARN: |
|---|
| 26 | case console.ERROR: |
|---|
| 27 | case console.FATAL: |
|---|
| 28 | break; |
|---|
| 29 | default: |
|---|
| 30 | console.fatal("invalid level"); |
|---|
| 31 | return; |
|---|
| 32 | } |
|---|
| 33 | console._level = level; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | public static function debug(...args):void { |
|---|
| 37 | if (console.level <= console.DEBUG) { |
|---|
| 38 | console.call("console.debug", args); |
|---|
| 39 | } |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | public static function info(...args):void { |
|---|
| 43 | if (console.level <= console.INFO) { |
|---|
| 44 | console.call("console.info", args); |
|---|
| 45 | } |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | public static function warn(...args):void { |
|---|
| 49 | if (console.level <= console.WARN) { |
|---|
| 50 | console.call("console.warn", args); |
|---|
| 51 | } |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | public static function error(...args):void { |
|---|
| 55 | if (console.level <= console.ERROR) { |
|---|
| 56 | console.call("console.error", args); |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | public static function fatal(...args):void { |
|---|
| 61 | if (console.level <= console.FATAL) { |
|---|
| 62 | console.call("alert", [ObjectUtil.inspect.apply(null, [args])]); |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | public static function log(...args):void { |
|---|
| 67 | console.call("console.debug", args); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | private static function call(method:String, args:Array):void { |
|---|
| 71 | var arg:* = args.length == 1 ? args[0] : args; |
|---|
| 72 | |
|---|
| 73 | if (ExternalInterface.available) { |
|---|
| 74 | try { |
|---|
| 75 | ExternalInterface.call("function(o,c){o.toString=function(){return c};"+method+"(o);}", |
|---|
| 76 | ObjectUtil.clone(arg), getQualifiedClassName(arg)); |
|---|
| 77 | } catch(e:Error) { |
|---|
| 78 | ExternalInterface.call(method, ObjectUtil.inspect.apply(null, args)); |
|---|
| 79 | } |
|---|
| 80 | } else { |
|---|
| 81 | trace(ObjectUtil.inspect.apply(null, args)); |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | } |
|---|