Flex / actionscript 3 equivalents for __FILE__ and __LINE__

I am new to flex / actionscript, and I was wondering if there is equivalent for php (and other languages) FILE and LINE identifiers?

Basically I want to make some custom error logging and would like something like:

 var mymessage:String = 'Oops, a hiccup occured at ' + __FILE__ + ', line: ' + __LINE__; 

In the case when the file and line will be replaced with their values ​​at compile time.

Is it possible?

+3
source share
3 answers

This is not possible, but there is a rather convenient solution for personal testing.

var stackTrace:String = new Error().getStackTrace();
if (stackTrace) {
    var mymessage:String = "Oops, a hiccup occurred " + stackTrace.split("\n")[1];
}

Correct your abuse getStackTraceto taste.

+3
source

To add to Corey the answer to the above. First add:

-define=CONFIG::debugging,true

( "-locale en_US" " " ). quickie:

package ddd
{
  public class Stack
  {
    protected static function str(val:*):String
    {
      if( val == null      ) return "<null>";
      if( val == undefined ) return "<undefined>";
      return val.toString();
    }

    protected static var removeAt :RegExp = /^\s*at\s*/i;
    protected static var matchFile:RegExp = /[(][)][\[][^:]*?:[0-9]+[\]]\s*$/i;
    protected static var trimFile :RegExp = /[()\[\]\s]*/ig;

    /* Must maintain number of stack levels, so that _stack can assume the 4th line of getStackTrace */
    private static function _stack( msg:String="", ...params ):String
    {
      var s   :String = new Error().getStackTrace();
      var func:String = "??";
      var file:String = "??";
      var args:String = null;
      if(s)
      {
        func = s.split("\n")[4];
        func = func.replace( removeAt, "" );
        var farr:Array  = func.match( matchFile );
        if( farr != null && farr.length > 0 ) file = farr[0].replace( trimFile, "" );
        func = func.replace( matchFile, "" );
      }
      for each( var param:* in params )
      {
        args = ( args == null ? "" : args.concat(",") );
        args = args.concat( str(param) );
      }
      return func + "(" + (args==null?"":args) + ")" + ( (msg!=null && msg!="") ? ":"+msg : "" ) + " at " + file;
    }

    /* Must maintain number of stack levels, so that _stack can assume the 4th line of getStackTrace */
    public static function stack( msg:String="", ...params ):String
    {
      params.unshift( msg );
      return _stack.apply( null, params );
    }

    /* Must maintain number of stack levels, so that _stack can assume the 4th line of getStackTrace */
    public static function pstack( msg:String="", ...params ):void
    {
      CONFIG::debugging {
        params.unshift(msg);
        trace( _stack.apply( null, params ) );
      }
    }
  }
}

:

Stack.pstack();

, , :

package::classname/function() at /wherever/src/package/classname.mxml:999

false , , , - pstack, - .

+1

IMHO Flex. , , , - .

If you find that methods are hundreds of lines long, you should rethink your coding style.

0
source

Source: https://habr.com/ru/post/1743027/


All Articles