T-Racks - examples

Printer-friendly versionPrinter-friendly version
How to use the T-Racks framework

 

In the units under test (here Foobar) we get first the tracer...

private static final Tracer<?> TRACER = Tracers.getTracer("FOOBAR-TRACER");

...then we add some tracing statements, optionally with trace levels, and optionally with as many extra arguments we like...

new Start(TRACER, TraceLevel.FINER, "Hello world!", foobar.toString()){};
...
new Trace(TRACER, TraceLevel.FINE){};
...
new End(TRACER, TraceLevel.FINER){};

...which is the same as writing this:

TRACER.trace(new Start(){}, TraceLevel.FINER, "Hello world!", foobar.toString());
...
TRACER.trace(new Trace(){}, TraceLevel.FINE);
...
TRACER.trace(new End(){}, TraceLevel.FINER);

As you can see we instantiate objects from inner anonymous classes which extend the abstract types Start, Trace and End. This supplies T-Racks automatically with several tracing data and has a way better performance (like any regular instruction) than other ways to get the same information (like stack traces or reflections). It just looks somewhat uggly, but you will get used to it   ;-)

We can also derive our own types from Trace. For example to integrate the trace level, something like:

new FineTrace(TRACER){};

We can already run the unit test and we get a tracing.

Another point is how to assign newborn threads to a track. The safest is to get the track...

TracingEntry track = TracingContext.getInstance().getCurrentTrack(Thread.currentThread());

... and pass it to the new thread, where we set it as the current track:

TracingContext.getInstance().setCurrentTrack(Thread.currentThread(), track);

T-Racks provides also easier ways: T-Racks can "guess" the track. To support its "guessing" we can mark a trace as fork to indicate that a new thread will be born next, out of the current one:

new Trace(TRACER){}.markAsFork();

Same way we can denote the start of a new track:

new Trace(TRACER){}.startNewTrack();

In case we have want to add traces to tracks in places like proxies, AOP, instrumentation code or reflection code, it will come in handy that we can create a trace directly as a simple bean that we pass to the tracer:

TRACER.trace(new TraceData(tracedClass, tracedConstructor, tracedMethod));

We can even create an entire TracingEntry bean with all data if we gathered these data otherwise.

 

There is much more we can do with T-Racks - discover it!

 

 

Pages