Trace & compilation flags

Hi !

I’m trying to customize the use of trace() by having a specific trace/log function by class that I could activate or deactivate withy a compilation flag:

class MyController01
    macro static inline function log(v) {
#if debug_ctrl01
        return macro trace(${v});
#end
    }
    // ...
    public function someAction(){
         log('some action called');
    }
}

This almost works but I lose the line number information of where the log function was called in the trace output:

 LOG  haxe/myapp/ctrl/MyController01.hx:35: some action called

line 35 here is the line of return macro trace(${v}); and I would like the output to display instead the line of the original log('some action called'); call…

How could I do that without ?

My goal is just to have a trace function that does exactly what it currently does by default with the difference that I can customize by haxe source file which ones I want to activate with a simple compilation tag…

You can use return macro @:pos(haxe.macro.Context.currentPos()) trace(${v}); in your macro to forward the position

2 Likes

Works like a charm, thank you buddy !