1

I'm writing debugging tools and I want to make it clear where I'm at. My ideal output would write the class name and function name to the console like so:

Vehicle.addWheels()
  Wheels were added by the user

and in code it would be a function like:

trace(addWheels, "Wheels were added by the user");

I'm thinking of writing a custom function that gets the details like so (pseudo code):

public static myTrace(function:Function, message:String):void {
   var className:String = function.parent;
   var functionName:String = getQualifiedName(function.prototype);
   trace(className + "." + functionName + ": " + message);
}

This is an AS3 example but AS3 is based on JavaScript so if it works in Javascript it may work in ActionScript.

If that doesn't work I can pass in the class reference easily using a third parameter but still need to know how to get the name of the function.

public static myTrace(object:Object, function:Function, message:String):void {
   var className:String = getQualifiedName(object);
   var functionName:String = getQualifiedName(function.prototype);
   trace(className + "." + functionName + ": " + message);
}
1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231

1 Answers1

0

If I use error.getStackTrace() I can use the following methods to get the name of the current class, method, document, document file path and line number. The document, file path and line number aren't available in release builds. This only works in release builds after FP 11.4.

protected function drawLayer_clickHandler(event:MouseEvent):void
{
    var stack:Array = getStackArray();
    var object:Object = getCurrentLocation();
    var className:Object = getCurrentClassName();
    var functionName:Object = getCurrentFunctionName();

}

protected static function getStackTrace(removeLines:Boolean = true):String {
    var error:Error = new Error();
    var value:String;
    var stackTrace:Array;

    if ("getStackTrace" in error) {
        value = error.getStackTrace();
        value = value.replace(/\t/g, "");
        if (removeLines) {
            value = value.replace(/\[.*\]/g, "");
            value = value.replace(/.*?::/g, "");
        }
        stackTrace = value.split("\n");
        stackTrace.shift();
        stackTrace.shift();
        return stackTrace.join("\n");
    }

    return null;
}

protected static function getCurrentClassName():String {
    var object:Object = getCurrentLocation(2);
    var className:String = object ? object.className: null;

    return className;
}

protected static function getCurrentFunctionName():String {
    var object:Object = getCurrentLocation(2);
    var functionName:String = object ? object.functionName: null;

    return functionName;
}

public static function getCurrentLocation(offset:int = 1):Object {
    var stack:Array = getStackArray(1, offset);
    var object:Object = stack && stack.length ? stack[0] : null;

    return object;
}

protected static function getStackArray(results:int = 0, offset:int = 0):Array {
    var error:Error = new Error();
    var value:String;
    var stackTrace:Array;
    var object:Object;
    var className:String;
    var functionName:String;
    var fileLocation:String;
    var lineNumber:String;
    var message:String;
    var components:Object;
    var matchPattern:RegExp;
    var path:String;
    var stack:Array;
    var locations:Array;

    matchPattern = /^at\s(.+?)\(\)\[(.*?):(.*)]/;

    if ("getStackTrace" in error) {
        value = error.getStackTrace();
        value = value.replace(/\t/g, "");
        //value = value.replace(/\[.*\]/g, "");
        //value = value.replace(/.*?::/g, "");
        stackTrace = value.split("\n");
        stackTrace.shift();// removes Error at
        stackTrace.shift(); // removes this function

        for (; offset >0 && stackTrace.length; offset--) {
            stackTrace.shift();
        }

        stack = [];

        for (var i:int = 0; i < stackTrace.length; i++) {
            object = {};
            message = stackTrace[i];
            components = message.match(matchPattern);

            // matches 
            // "at TransformTests/drawLayer_clickHandler()[/Documents/Project/src/TransformTests.mxml:244]"
            if (components) {
                locations = components[1].split(/[\\|\/]/);

                // class and method
                if (locations.length>1) {
                    object.className = locations[0];
                    object.functionName = locations[1];
                }
                // global method - has no class
                else if (locations.length) {
                    object.functionName = locations[0];
                }

                path = components[2];

                object.location = path;
                object.document = path ? path.split(/[\\|\/]/).pop() : null;
                object.lineNumber = components[3];
                stack.push(object);
            }
            else {
                // matches "at Transform/drawLayer_clickHandler()" or "at drawLayer_clickHandler()"
                components = message.match(/^at\s(.*)\(\)/);

                // runtime has no file path or line number
                if (components) {
                    path = components[1];
                    locations = path.split(/[\\|\/]/);

                    // class and method
                    if (locations.length>1) {
                        object.className = locations[0];
                        object.functionName = locations[1];
                    }
                    // global method - no class
                    else if (locations.length) {
                        object.functionName = locations[0];
                    }

                    stack.push(object);
                }

            }

            if (results==i+1) {
                break;
            }
        }

        return stack;
    }

    return null;
}

I added a few global functions for getting this information in the github library. Add the swc to your class and you can call any of the following methods:

log();
log("message");
logTarget(object, "message");
getCurrentClassName();
getCurrentFunctionName();
getCurrentLocation();
getStackArray();

The getCurrentLocation() returns an object that contains the class name, method name and if running in the content debugger returns the document, document path and line number.

1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231