DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Flex/ActionScript: Making Trace() Display The Method It Was Called In

  • submit to reddit
        While trace() is useful, its indiscriminate use by me often crowds 
the console of my fellow developers. We agreed on making every trace() show
the method it came from, but I am too lazy to do this, this makes it automatic:

public static function TRACE(s:Object):void {
  try {
    throw new Error();
  } catch (e:Error) {
    var stack:String = e.getStackTrace();
    var frames:Array = stack.split("\n");
    var myFrame:String = String(frames[2]);
    myFrame = myFrame.replace("\t", "");
    // "at " can be followed by some part of the package
    // you don't want to see. E.g., if your code is all in
    //, you can put "at" so as not
    // to crowd the display
    myFrame = myFrame.substr("at ".length);
    myFrame = myFrame.substring(0, myFrame.indexOf("["));
    trace(myFrame + ": " + s);