package bontos.firebugTracer
{
import mx.logging.ILogger;
import mx.logging.LogEvent;
import mx.logging.LogEventLevel;
import mx.logging.AbstractTarget;
import bontos.firebugTracer.Fbug;
/**
* FirebugTarget has the same API as LineFormattedTarget and
* provides default behavior for including date, time, category, and level
* within the output.
* FirebugTarget sends log messages to Firebug console.
* NOTE: Firebug log levels different from levels in
* LogEventLevel and fatal and error log level handles as error.
*/
public class FirebugTarget extends AbstractTarget
{
[Inspectable(defaultValue=false)]
/**
* Indicates if the date should be added to the message.
*/
public var includeDate : Boolean = false;
[Inspectable(defaultValue=false)]
/**
* Indicates if the time should be added to the message.
*/
public var includeTime : Boolean = false;
[Inspectable(defaultValue=false)]
/**
* Indicates if the level for the event should added to the message.
*/
public var includeLevel : Boolean = false;
[Inspectable(defaultValue=false)]
/**
* Indicates if the category for this target should added to the message.
*/
public var includeCategory : Boolean = false;
[Inspectable(defaultValue=" ")]
/**
* The separator string to use between fields (the default is " ")
*/
public var fieldSeparator : String = " ";
/**
* Constructor
*/
public function FirebugTarget( )
{
super( );
}
/**
* This method handles a LogEvent
from an associated logger.
* A target uses this method to translate the event into the appropriate
* format for transmission, storage, or display.
* This method will be called only if the event's level is in range of the
* target's level.
*/
override public function logEvent( event : LogEvent ) : void
{
var date : String = "";
if( includeDate || includeTime )
{
var d : Date = new Date( );
if( includeDate )
{
date += Number( d.getUTCMonth( ) + 1 ).toString( ) + "/" +
d.getUTCDate( ).toString( ) + "/" +
d.getUTCFullYear( ) + fieldSeparator;
}
if( includeTime )
{
date += pad( d.getUTCHours( ) ) + ":" +
pad( d.getUTCMinutes( ) ) + ":" +
pad( d.getUTCSeconds( ) ) + "." +
pad( d.getUTCMilliseconds( ) ) + fieldSeparator;
}
}
var level : String = "";
if( includeLevel )
{
level = "[" + LogEvent.getLevelString( event.level ) +
"]" + fieldSeparator;
}
var category : String = includeCategory ?
ILogger( event.target ).category + fieldSeparator :
"";
switch( event.level )
{
case LogEventLevel.DEBUG :
Fbug.debug( date + level + category + event.message );
break;
case LogEventLevel.INFO :
Fbug.info( date + level + category + event.message );
break;
case LogEventLevel.WARN :
Fbug.warn( date + level + category + event.message );
break;
case LogEventLevel.ERROR :
Fbug.error( date + level + category + event.message );
break;
case LogEventLevel.FATAL :
Fbug.error( date + level + category + event.message );
break;
default:
Fbug.log( date + level + category + event.message );
break;
}
}
/**
* @private
*/
private function pad ( num : Number ) : String
{
return num > 9 ? num.toString( ) : "0" + num.toString( );
}
}
}