Flash tip: Advanced trace output in Flash ActionScript 3.0, Class name, function name and line number

Hello,

Today a tip for ActionScript developers.

It’s been a while I looking for an advanced trace output with more details in few line of code.

Just add this line of code instead the trace() function to get a trace message in your ouput with all details.

Debug.log("foo", "bar");

The output will show this :

[Class][method][line] foo bar

Installation

  1. Download the Debug.as file
  2. Copy and paste de Debug.as at the root project, in the same folder of the .fla file
  3. Enable Debugging in Flash. Publish parameters > swf  > Advanced > Allow debugging
  4. Use the following code to display the advanced debugging message

Usage

Debug.log("foo", "bar");

Source code

Debug.as code:

package 
{
	/**
	* Advanced trace method
	*
	* @author Benoît Freslon
	* @version 1.0
	* @example 	Usage
	* 
	* import Debug;
	* Debug.trace("foo", "bar");
	* 
	* output > [Class][Method][line] foo bar
	**/
	public class Debug
	{

		static public function log(...rest)
		{
			var error:Error = new Error();

			var tr:String = "";
			for each (var msg:String in rest)
			{
				tr +=  msg + " ";
			}
			var s:String = error.getStackTrace();

			var arr:Array = s.split("at ")[2].split("()");

			var stack:String = arr[0];
			var arrClass:Array = stack.split("/")[0].split("::");
			var className:String = arrClass[arrClass.length - 1];
			var functionName:String = stack.split("/")[1];
			if ( functionName == null )
			{
				functionName = className;
			}
			var arrLine:Array = arr[1].split(":");
			if (arrLine.length > 1) {
				var line:String = arrLine[arrLine.length-1].split("]")[0]
				trace( "[" + className + "][" + functionName + "]" + "[" + line + "]", tr );;
			} else {
				trace( "[" + className + "][" + functionName + "]", tr );
			}
		}
	}
}

Good programming :).