| 1 | package com.jeroenwijering.utils { |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | import flash.events.StatusEvent; |
|---|
| 5 | import flash.external.ExternalInterface; |
|---|
| 6 | import flash.net.LocalConnection; |
|---|
| 7 | import flash.net.SharedObject; |
|---|
| 8 | import flash.system.System; |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | /** |
|---|
| 12 | * <p>Utility class for logging debug messages. It supports the following logging systems:</p> |
|---|
| 13 | * <ul> |
|---|
| 14 | * <li>The standalone Arthropod AIR application.</li> |
|---|
| 15 | * <li>The Console.log function built into Firefox/Firebug.</li> |
|---|
| 16 | * <li>The tracing sstem built into the debugging players.</li> |
|---|
| 17 | * </ul> |
|---|
| 18 | * |
|---|
| 19 | * @todo implement log levels. |
|---|
| 20 | **/ |
|---|
| 21 | public class Logger { |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | /** Constant defining the Arthropod output type. **/ |
|---|
| 25 | public static const ARTHROPOD:String = "arthropod"; |
|---|
| 26 | /** LocalConnection instance arthropod use. **/ |
|---|
| 27 | private static const CONNECTION:LocalConnection = new LocalConnection(); |
|---|
| 28 | /** Arthropod connection name. **/ |
|---|
| 29 | private static const CONNECTION_NAME:String = |
|---|
| 30 | 'app#com.carlcalderon.Arthropod.161E714B6C1A76DE7B9865F88B32FCCE8FABA7B5.1:arthropod'; |
|---|
| 31 | /** Constant defining the Firefox/Firebug console output type. **/ |
|---|
| 32 | public static const CONSOLE:String = "console"; |
|---|
| 33 | /** Constant defining there's no output. **/ |
|---|
| 34 | public static const NONE:String = "none"; |
|---|
| 35 | /** Constant defining the Flash tracing output type. **/ |
|---|
| 36 | public static const TRACE:String = "trace"; |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | /** Latest output position. **/ |
|---|
| 40 | private static var _output:String = Logger.NONE; |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| 44 | * Log a message to the output system. |
|---|
| 45 | * |
|---|
| 46 | * @param message The message to send forward. Arrays and objects are automatically chopped up. |
|---|
| 47 | * @param type The type of message; is capitalized and encapsulates the message. |
|---|
| 48 | **/ |
|---|
| 49 | public static function log(message:*,type:String="log"):void { |
|---|
| 50 | if(message == undefined) { |
|---|
| 51 | send(type.toUpperCase()); |
|---|
| 52 | } else if (message is String) { |
|---|
| 53 | send(type.toUpperCase()+' ('+message+')'); |
|---|
| 54 | } else if (message is Boolean || message is Number || message is Array) { |
|---|
| 55 | send(type.toUpperCase()+' ('+message.toString()+')'); |
|---|
| 56 | } else { |
|---|
| 57 | Logger.object(message,type); |
|---|
| 58 | } |
|---|
| 59 | }; |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | /** Explode an object for logging. **/ |
|---|
| 63 | private static function object(message:Object,type:String):void { |
|---|
| 64 | var txt:String = type.toUpperCase()+' ({'; |
|---|
| 65 | for(var i:String in message) { |
|---|
| 66 | if(message[i] is Object) { |
|---|
| 67 | txt += i+':'+message[i].toString()+', '; |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | txt = txt.substr(0,txt.length-2); |
|---|
| 71 | if(i) { txt += '})'; } |
|---|
| 72 | Logger.send(txt); |
|---|
| 73 | }; |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | /** |
|---|
| 77 | * Set output system to use for logging. The output is also saved in a cookie. |
|---|
| 78 | * |
|---|
| 79 | * @param put System to use (ARTHROPOD, CONSOLE, TRACE or NONE).StatusEvent |
|---|
| 80 | **/ |
|---|
| 81 | public static function set output(put:String):void { |
|---|
| 82 | if(put == ARTHROPOD) { |
|---|
| 83 | CONNECTION.allowInsecureDomain('*'); |
|---|
| 84 | CONNECTION.addEventListener(StatusEvent.STATUS,Logger.status); |
|---|
| 85 | } |
|---|
| 86 | SharedObject.getLocal('com.jeroenwijering','/').data['debug'] = put; |
|---|
| 87 | Logger._output = put; |
|---|
| 88 | }; |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | /** |
|---|
| 92 | * Get output system to use for logging. |
|---|
| 93 | * |
|---|
| 94 | * @return System to use (ARTHROPOD, CONSOLE, TRACE or NONE). |
|---|
| 95 | **/ |
|---|
| 96 | public static function get output():String { |
|---|
| 97 | return Logger._output; |
|---|
| 98 | }; |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | /** Send the messages to the output system. **/ |
|---|
| 102 | private static function send(text:String):void { |
|---|
| 103 | switch(Logger._output) { |
|---|
| 104 | case ARTHROPOD: |
|---|
| 105 | CONNECTION.send(CONNECTION_NAME,'debug','CDC309AF',text,0xCCCCCC); |
|---|
| 106 | break; |
|---|
| 107 | case CONSOLE: |
|---|
| 108 | ExternalInterface.call('console.log',text); |
|---|
| 109 | break; |
|---|
| 110 | case TRACE: |
|---|
| 111 | trace(text); |
|---|
| 112 | break; |
|---|
| 113 | case NONE: |
|---|
| 114 | break; |
|---|
| 115 | default: |
|---|
| 116 | ExternalInterface.call(Logger._output,text); |
|---|
| 117 | break; |
|---|
| 118 | } |
|---|
| 119 | }; |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | /** Manage the status call of localconnection. **/ |
|---|
| 123 | private static function status(evt:StatusEvent):void {}; |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | } |
|---|