/as3logger/src/de/dev_lab/logging/publisher/SOSPublisher.as
http://as3logger.googlecode.com/ · ActionScript · 156 lines · 84 code · 20 blank · 52 comment · 6 complexity · 57594bae1a26c9e357ee87006cf913d3 MD5 · raw file
- package de.dev_lab.logging.publisher
- {
- import de.dev_lab.logging.Logger;
-
- import flash.events.Event;
- import flash.events.IOErrorEvent;
- import flash.events.SecurityErrorEvent;
- import flash.net.XMLSocket;
-
- /**
- * Publisher for Powerflasher Socket Output Server (SOS).
- *
- * http://sos.powerflasher.com/
- *
- * @author Sebastian Weyrauch
- *
- * LastChanged:
- *
- * $Author: sebastian.weyrauch $
- * $Revision: 10 $
- * $LastChangedDate: 2010-07-04 20:34:28 +0200 (Sun, 04 Jul 2010) $
- * $URL: http://as3logger.googlecode.com/svn/trunk/as3logger/src/de/dev_lab/logging/publisher/SOSPublisher.as $
- *
- */
- public class SOSPublisher implements IPublisher
- {
- private var _socket : XMLSocket;
-
- private var _queue : Array;
-
- private var _clearOnConnect : Boolean;
-
- /**
- * Constructor. Creates the socket and the connects to it.
- *
- * @param clearOnConnect <code>true</code> if the output shall be reset on connect,
- * <code>false</code> if not
- * @param hostname The hostname to connect
- * @param port The port to connect
- */
- public function SOSPublisher( clearOnConnect : Boolean = true , hostname : String = "localhost" , port : int = 4445 )
- {
- _clearOnConnect = clearOnConnect;
- _queue = [];
-
- _socket = new XMLSocket;
- _socket.addEventListener( Event.CONNECT , onConnect );
- _socket.addEventListener( IOErrorEvent.IO_ERROR , onError );
- _socket.addEventListener( SecurityErrorEvent.SECURITY_ERROR , onError );
- _socket.connect( hostname , port );
- }
-
- /**
- * Publishs a message. If the socket is not connect yet,
- * it will be added to a queue.
- *
- * @param logLevel Log level
- * @param message Message
- */
- public function publish ( logLevel : int , object : * , ...additional ) : void
- {
- var message : String = String ( object );
-
- if( message.length == 0 ) return;
-
- if ( _socket.connected )
- {
- sendToSocket ( logLevel , message );
- }
- else
- {
- if ( _queue ) _queue.push ( new LogObject ( logLevel , message ) );
- }
- }
-
- /**
- * Resets the output of SOS.
- */
- public function clear () : void
- {
- _socket.send( "<clear/>\n" );
- }
-
- /**
- * Disconnects from the socket.
- */
- public function destroy () : void
- {
- _socket.removeEventListener( IOErrorEvent.IO_ERROR , onError );
- _socket.removeEventListener( SecurityErrorEvent.SECURITY_ERROR , onError );
- _socket.removeEventListener( Event.CONNECT , onConnect );
-
- try
- {
- _socket.close();
- }
- catch ( error : Error )
- {
- trace( error );
- }
- }
-
- /**
- * Sends a message to the socket.
- *
- * @param logLevel Log level
- * @param message Message
- */
- private function sendToSocket ( logLevel : int, message : String ) : void
- {
- var xmlMessage : String = "<showMessage key=\"" + Logger.getLogType ( logLevel ) + "\"/> " + message;
- _socket.send( xmlMessage + "\n");
- }
-
- /**
- * Handler for Socket connect.
- * Sends all messages in the queue.
- *
- * @param event
- */
- private function onConnect ( event : Event ) : void
- {
- if ( _clearOnConnect ) clear();
-
- for each ( var logObject : LogObject in _queue )
- {
- sendToSocket ( logObject.logLevel , logObject.message );
- }
-
- _queue = null;
- }
-
- /**
- * Handler for Socker errors.
- * Destroys the publisher.
- */
- private function onError ( event : Event ) : void
- {
- _queue = null;
-
- destroy();
- }
- }
- }
-
- internal class LogObject
- {
- public var logLevel : int;
- public var message : String;
-
- public function LogObject ( logLevel : int , message : String )
- {
- this.logLevel = logLevel;
- this.message = message;
- }
- }