| 1 | package com.longtailvideo.jwplayer.utils { |
|---|
| 2 | import com.longtailvideo.jwplayer.model.PlayerConfig; |
|---|
| 3 | |
|---|
| 4 | import flash.events.AsyncErrorEvent; |
|---|
| 5 | import flash.events.Event; |
|---|
| 6 | import flash.events.SecurityErrorEvent; |
|---|
| 7 | import flash.events.StatusEvent; |
|---|
| 8 | import flash.external.ExternalInterface; |
|---|
| 9 | import flash.net.LocalConnection; |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | /** |
|---|
| 13 | * <p>Utility class for logging debug messages. It supports the following logging systems:</p> |
|---|
| 14 | * <ul> |
|---|
| 15 | * <li>The standalone Arthropod AIR application.</li> |
|---|
| 16 | * <li>The Console.log function built into Firefox/Firebug.</li> |
|---|
| 17 | * <li>The tracing sstem built into the debugging players.</li> |
|---|
| 18 | * </ul> |
|---|
| 19 | * |
|---|
| 20 | **/ |
|---|
| 21 | public class Logger { |
|---|
| 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 = 'app#com.carlcalderon.Arthropod.161E714B6C1A76DE7B9865F88B32FCCE8FABA7B5.1:arthropod'; |
|---|
| 28 | /** Constant defining the Firefox/Firebug console output type. **/ |
|---|
| 29 | public static const CONSOLE:String = "console"; |
|---|
| 30 | /** Constant defining there's no output. **/ |
|---|
| 31 | public static const NONE:String = "none"; |
|---|
| 32 | /** Constant defining the Flash tracing output type. **/ |
|---|
| 33 | public static const TRACE:String = "trace"; |
|---|
| 34 | /** Reference to the player config **/ |
|---|
| 35 | private static var _config:PlayerConfig; |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | /** |
|---|
| 39 | * Log a message to the output system. |
|---|
| 40 | * |
|---|
| 41 | * @param message The message to send forward. Arrays and objects are automatically chopped up. |
|---|
| 42 | * @param type The type of message; is capitalized and encapsulates the message. |
|---|
| 43 | **/ |
|---|
| 44 | public static function log(message:*, type:String = "log"):void { |
|---|
| 45 | try{ |
|---|
| 46 | if (message == undefined) { |
|---|
| 47 | send(type.toUpperCase()); |
|---|
| 48 | } else if (message is String) { |
|---|
| 49 | send(type.toUpperCase() + ' (' + message + ')'); |
|---|
| 50 | } else if (message is Boolean || message is Number || message is Array) { |
|---|
| 51 | send(type.toUpperCase() + ' (' + message.toString() + ')'); |
|---|
| 52 | } else { |
|---|
| 53 | Logger.object(message, type); |
|---|
| 54 | } |
|---|
| 55 | } catch (err:Error){ |
|---|
| 56 | trace(message); |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | /** Explode an object for logging. **/ |
|---|
| 62 | private static function object(message:Object, type:String):void { |
|---|
| 63 | var txt:String = type.toUpperCase() + ' ('; |
|---|
| 64 | Strings.print_r(message); |
|---|
| 65 | txt += ')'; |
|---|
| 66 | Logger.send(txt); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | /** Send the messages to the output system. **/ |
|---|
| 70 | private static function send(text:String):void { |
|---|
| 71 | var debug:String = _config ? _config.debug : TRACE; |
|---|
| 72 | switch (debug) { |
|---|
| 73 | case ARTHROPOD: |
|---|
| 74 | try { |
|---|
| 75 | CONNECTION.send(CONNECTION_NAME, 'debug', 'CDC309AF', text, 0xCCCCCC); |
|---|
| 76 | } catch(e:Error) { |
|---|
| 77 | trace(text); |
|---|
| 78 | } |
|---|
| 79 | break; |
|---|
| 80 | case CONSOLE: |
|---|
| 81 | if (ExternalInterface.available) { |
|---|
| 82 | ExternalInterface.call('console.log', text); |
|---|
| 83 | } |
|---|
| 84 | break; |
|---|
| 85 | case TRACE: |
|---|
| 86 | trace(text); |
|---|
| 87 | break; |
|---|
| 88 | case NONE: |
|---|
| 89 | break; |
|---|
| 90 | default: |
|---|
| 91 | if (ExternalInterface.available) { |
|---|
| 92 | ExternalInterface.call(_config.debug, text); |
|---|
| 93 | } |
|---|
| 94 | break; |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | public static function setConfig(config:PlayerConfig):void { |
|---|
| 99 | _config = config; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | CONNECTION.addEventListener(AsyncErrorEvent.ASYNC_ERROR, localConnectionEventHandler); |
|---|
| 103 | CONNECTION.addEventListener(SecurityErrorEvent.SECURITY_ERROR, localConnectionEventHandler); |
|---|
| 104 | CONNECTION.addEventListener(StatusEvent.STATUS, localConnectionEventHandler); |
|---|
| 105 | |
|---|
| 106 | /** |
|---|
| 107 | * Handle LocalConnection events |
|---|
| 108 | **/ |
|---|
| 109 | private static function localConnectionEventHandler(evt:Event):void{ |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | } |
|---|