| 1 | package com.jeroenwijering.utils { |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | import flash.external.ExternalInterface; |
|---|
| 5 | import flash.net.LocalConnection; |
|---|
| 6 | import flash.system.System; |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | /** |
|---|
| 10 | * <p>Utility class for logging debug messages. It supports the following logging systems:</p> |
|---|
| 11 | * <ul> |
|---|
| 12 | * <li>The standalone Arthropod AIR application.</li> |
|---|
| 13 | * <li>The Console.log function built into Firefox/Firebug.</li> |
|---|
| 14 | * <li>The tracing sstem built into the debugging players.</li> |
|---|
| 15 | * </ul> |
|---|
| 16 | * |
|---|
| 17 | * @todo implement log levels. |
|---|
| 18 | **/ |
|---|
| 19 | public class Logger { |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | /** Constant defining the Arthropod output type. **/ |
|---|
| 23 | public static const ARTHROPOD:String = "arthropod"; |
|---|
| 24 | /** LocalConnection instance arthropod use. **/ |
|---|
| 25 | private static const CONNECTION:LocalConnection = new LocalConnection(); |
|---|
| 26 | /** Arthropod connection name. **/ |
|---|
| 27 | private static const CONNECTION_NAME:String = |
|---|
| 28 | 'app#com.carlcalderon.Arthropod.161E714B6C1A76DE7B9865F88B32FCCE8FABA7B5.1:arthropod'; |
|---|
| 29 | /** Arthropod connection password. **/ |
|---|
| 30 | private static const CONNECTION_PASSWORD:String = 'CDC309AF'; |
|---|
| 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 | /** Current output system to use for logging. **/ |
|---|
| 40 | public 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 precedes 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 | /** Send the messages to the output system. **/ |
|---|
| 77 | private static function send(text:String):void { |
|---|
| 78 | switch(Logger.output) { |
|---|
| 79 | case ARTHROPOD: |
|---|
| 80 | CONNECTION.allowInsecureDomain('*'); |
|---|
| 81 | CONNECTION.send(CONNECTION_NAME,'debug',CONNECTION_PASSWORD,text,0xCCCCCC); |
|---|
| 82 | break; |
|---|
| 83 | case CONSOLE: |
|---|
| 84 | ExternalInterface.call('console.log',text); |
|---|
| 85 | break; |
|---|
| 86 | case TRACE: |
|---|
| 87 | trace(text); |
|---|
| 88 | break; |
|---|
| 89 | default: |
|---|
| 90 | break; |
|---|
| 91 | } |
|---|
| 92 | }; |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | } |
|---|