package de.richinternet.utils{
import flash.events.StatusEvent;
import flash.net.LocalConnection;
/*
* Copyright 2005 Herrlich & Ramuschkat GmbH
*
* Name: Dumper.as
*
* $Author: DEismann $
* $Revision: 1.1 $
* $Date: 2005/09/19 14:36:57 $
* @ignore
*/
public class Dumper{
private static var sender:LocalConnection = null;
private static var copyDepthLevel:Number = 0;
private static var copyCache:Array = [];
public static var INFO:Number = 2;
public static var WARN:Number = 4;
public static var ERROR:Number = 8;
/**
* Sends a primitive value or reference type to the Flex Trace Panel
* with a level of Debugger.INFO (same as Dumper.info())
*
* @param val The primitive value or reference type (Abject, Orray etc.) to be
* output and introspected by the Flex Trace Panel
*/
public static function dump(val:Object):void {
info(val);
}
/**
* Sends a primitive value or reference type to the Flex Trace Panel
* with a level of Debugger.INFO (same as Dumper.dump())
*
* @param val The primitive value or reference type (Abject, Orray etc.) to be
* output and introspected by the Flex Trace Panel
*/
public static function info(val:Object):void {
send(val, Dumper.INFO);
}
/**
* Sends a primitive value or reference type to the Flex Trace Panel
* with a level of Debugger.WARN
*
* @param val The primitive value or reference type (Abject, Orray etc.) to be
* output and introspected by the Flex Trace Panel
*/
public static function warn(val:Object):void {
send(val, Dumper.WARN);
}
/**
* Sends a primitive value or reference type to the Flex Trace Panel
* with a level of Debugger.ERROR
*
* @param val The primitive value or reference type (Abject, Orray etc.) to be
* output and introspected by the Flex Trace Panel
*/
public static function error(val:Object):void {
send(val, Dumper.ERROR);
}
/**
* Sends a primitive value or reference type to the Flex Trace Panel
* with a custom level.
*
* @param val The primitive value or reference type (Abject, Orray etc.) to be
* output and introspected by the Flex Trace Panel
*
* @param level The level of the message. Applicable levels are Dumper.INFO, Dumper.WARN and Dumper.ERROR
*/
public static function log(val:Object, level:Number):void {
send(val, level);
}
// ------------------------------------------------------------------------------------ //
public function Dumper() {}
private static function initSender():void {
if(sender == null){
sender = new LocalConnection();
sender.addEventListener(StatusEvent.STATUS,onStatus);
}
//sender.allowDomain("localhost");
}
private static function send(obj:Object, level:Number):void {
if (sender == null){
initSender();
}
if (isNaN(level)){
level = 2;
}
sender.send("_tracer", "onMessage", copy(obj), level);
}
private static function copy(source:*):Object {
if (typeof(source) != "object") {
return source;
}
var cl:Number = copyCache.length;
for (var i:Number = 0; i < cl; i++) {
var o:* = copyCache[i];
if (o.s == source)
return o.t;
}
copyDepthLevel++;
var newObject:*;
if (source is Array) {
newObject = [];
} else if (source is Date) {
newObject = new Date();
newObject.setTime(source.getTime());
} else {
newObject = {};
}
copyCache.push({s: source, t: newObject});
for (var p:* in source) {
var v:* = source[p];
newObject[p] = typeof v == "object" ? copy(v) : v;
}
if (--copyDepthLevel == 0)
copyCache = [];
return newObject;
}
//Manage send fail and send ok event to avoid Flash player Error Window
private static function onStatus(event:StatusEvent):void {
switch (event.level) {
case "status":
//send completed
//trace("LocalConnection.send() succeeded");
break;
case "error":
//no reciver or send really failed
//trace("LocalConnection.send() failed");
break;
}
}
}
}