Hi,

> 
> Is it possible to override global functions such as trace( ) ?� For
example, sometimes I want the trace( ) to tell me where they are
called from, but it's kinda a pain to have to import
getQualifiedClassName and sprinkle code with stuff like
(getQualifiedClassName(this) + ": " + ...); all over the place.
> 

you can override the global trace function at the class level

----
package foobar
{
   public class MyClass
   {

     public function MyClass()
     {
     trace( "hello world" ); //output "[ hello world ]"
     }

     protected function trace( ...args ):void
     {
            args.unshift( "[" );
            args.push( "]" );
            var msg:String = args.join(" ");
            public::trace( msg ); //thanks namespaces
     }
   }

}
----


if you're using a debug version of flash

you can use a StackTrace trick to have more infos in your overrided trace

----
     protected function trace( ...args ):void
     {
            var stackTrace: String;
            try
            {
                throw new Error();
            }
            catch ( e:Error )
            {
                stackTrace = e.getStackTrace();
            }

            stackTrace = stackTrace.split( "\n" )[2];

            args.unshift( getTimer() + " - [" );
            args.push( "] - " + stackTrace );
            var msg:String = args.join(" ");
            public::trace( msg );
     } 
----
will output something like
123 - [ hello world ] -         at
foobar::MyClass/MyClass()[/tmp/foobar/MyClass.as:12] 



alos you can use flash.trace.Trace and you will really trace
everything and maybe more than you wish
(undocumented in Flash but documented in the Tamarin project
http://hg.mozilla.org/tamarin-central/index.cgi/file/e774dfe22b39/extensions/Trace.as)

by default
----
import flash.trace.Trace
Trace.setLevel( Trace.METHODS_AND_LINES_WITH_ARGS, Trace.OFF );
----

with your own function listener
----
public function traceListener( line:String, index:int, method:Sring,
args:String ):void
{
    trace( "@"+index + " - " + line );
    trace( method+"("+args+")" );

}

import flash.trace.Trace
Trace.setListener( traceListener );
Trace.setLevel( Trace.METHODS_AND_LINES_WITH_ARGS, Trace.LISTENER ); 
----

enjoy :)

zwetan





Reply via email to