All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.enonic.xp.trace.Tracer Maven / Gradle / Ivy

The newest version!
package com.enonic.xp.trace;

import java.util.concurrent.Callable;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Supplier;

public final class Tracer
{
    private static final Tracer INSTANCE = new Tracer();

    private final ThreadLocal current;

    private TraceManager manager;

    private Tracer()
    {
        this.current = new ThreadLocal<>();
        this.manager = null;
    }

    public static boolean isEnabled()
    {
        return INSTANCE.manager != null;
    }

    public static Trace current()
    {
        return INSTANCE.current.get();
    }

    public static void withCurrent( final Consumer consumer )
    {
        final Trace trace = current();
        if ( trace != null )
        {
            consumer.accept( trace );
        }
    }

    public static void trace( final Trace trace, final Runnable runnable )
    {
        trace( trace, () -> {
            runnable.run();
            return null;
        } );
    }

    public static  T trace( final Trace trace, final TraceRunnable runnable )
    {
        try
        {
            return traceEx( trace, runnable::run );
        }
        catch ( final RuntimeException e )
        {
            throw e;
        }
        catch ( final Exception e )
        {
            throw new RuntimeException( e );
        }
    }

    public static  T traceEx( final Trace trace, final Callable callable )
        throws Exception
    {
        final Trace current = current();

        try
        {
            setCurrent( trace );
            startTrace( trace );
            return callable.call();
        }
        finally
        {
            endTrace( trace );
            setCurrent( current );
        }
    }

    public static Trace newTrace( final String name )
    {
        if ( !isEnabled() )
        {
            return null;
        }

        return INSTANCE.manager.newTrace( name, current() );
    }

    public static void trace( final String name, final Runnable runnable )
    {
        trace( newTrace( name ), runnable );
    }

    public static  T trace( final String name, final TraceRunnable runnable )
    {
        return trace( newTrace( name ), runnable );
    }

    public static  T trace( final String name, final Consumer before, final Supplier main, final BiConsumer after )
    {
        final Trace trace = newTrace( name );

        if ( trace != null )
        {
            before.accept( trace );
            final Trace current = current();

            try
            {
                setCurrent( trace );
                startTrace( trace );
                final T result = main.get();
                after.accept( trace, result );
                return result;
            }
            finally
            {
                endTrace( trace );
                setCurrent( current );
            }
        }
        else
        {
            return main.get();
        }
    }

    public static  T trace( final String name, final Consumer before, final Supplier main )
    {
        return trace( name, before, main, ( trace, t ) -> {
        } );
    }

    public static  T trace( final String name, final Consumer before, final Runnable main )
    {
        return trace( name, before, () -> {
            main.run();
            return null;
        }, ( trace, t ) -> {
        } );
    }

    public static  T traceEx( final String name, final Callable callable )
        throws Exception
    {
        return traceEx( newTrace( name ), callable );
    }

    public static void setManager( final TraceManager manager )
    {
        INSTANCE.manager = manager;
    }

    private static void setCurrent( final Trace trace )
    {
        INSTANCE.current.set( trace );
    }

    private static void startTrace( final Trace trace )
    {
        if ( trace == null )
        {
            return;
        }

        trace.start();
        if ( INSTANCE.manager != null )
        {
            INSTANCE.manager.dispatch( TraceEvent.start( trace ) );
        }
    }

    private static void endTrace( final Trace trace )
    {
        if ( trace == null )
        {
            return;
        }

        trace.end();
        if ( INSTANCE.manager != null )
        {
            INSTANCE.manager.dispatch( TraceEvent.end( trace ) );
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy