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

patterntesting.runtime.log.LogRecorder Maven / Gradle / Ivy

Go to download

PatternTesting Runtime (patterntesting-rt) is the runtime component for the PatternTesting framework. It provides the annotations and base classes for the PatternTesting testing framework (e.g. patterntesting-check, patterntesting-concurrent or patterntesting-exception) but can be also used standalone for classpath monitoring or profiling. It uses AOP and AspectJ to perform this feat.

There is a newer version: 2.4.0
Show newest version
/**
 * $Id: LogRecorder.java,v 1.8 2014/04/05 10:13:13 oboehm Exp $
 *
 * Copyright (c) 2008 by Oliver Boehm
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * (c)reated 09.10.2008 by oliver ([email protected])
 */
package patterntesting.runtime.log;

import java.util.*;

import org.slf4j.*;

import patterntesting.annotation.check.runtime.NullArgsAllowed;
import patterntesting.runtime.util.Converter;

/**
 * The Class LogRecorder which is able to record log messages.
 *
 * @author oliver
 * @since 09.10.2008
 * @version $Revision: 1.8 $
 */
public final class LogRecorder implements Logger {

	private final List objects = new ArrayList();
	private final List exceptions = new ArrayList();
	private int n = 0;

	/**
	 * Debug.
	 *
	 * @param arg0 the arg0
	 * @see Logger#debug(String)
	 */
	public void debug(final String arg0) {
		this.record(arg0);
	}

	/**
	 * Debug.
	 *
	 * @param arg0 the arg0
	 * @param arg1 the arg1
	 * @see Logger#debug(String, Throwable)
	 */
	public void debug(final String arg0, final Throwable arg1) {
		this.record(arg0, arg1);
	}

	/**
	 * Error.
	 *
	 * @param arg0 the arg0
	 * @see Logger#error(String)
	 */
	public void error(final String arg0) {
		this.record(arg0);
	}

	/**
	 * Error.
	 *
	 * @param arg0 the arg0
	 * @param arg1 the arg1
	 * @see Logger#error(String, Throwable)
	 */
	public void error(final String arg0, final Throwable arg1) {
		this.record(arg0, arg1);
	}

	/**
	 * Info.
	 *
	 * @param arg0 the arg0
	 * @see Logger#info(String)
	 */
	public void info(final String arg0) {
		this.record(arg0);
	}

	/**
	 * Info.
	 *
	 * @param arg0 the arg0
	 * @param arg1 the arg1
	 * @see Logger#info(String, Throwable)
	 */
	public void info(final String arg0, final Throwable arg1) {
		this.record(arg0, arg1);
	}

	/**
	 * Checks if is debug enabled.
	 *
	 * @return true, if is debug enabled
	 * @see Logger#isDebugEnabled()
	 */
	public boolean isDebugEnabled() {
		return false;
	}

	/**
	 * Checks if is error enabled.
	 *
	 * @return true, if is error enabled
	 * @see Logger#isErrorEnabled()
	 */
	public boolean isErrorEnabled() {
		return false;
	}

	/**
	 * Checks if is info enabled.
	 *
	 * @return true, if is info enabled
	 * @see Logger#isInfoEnabled()
	 */
	public boolean isInfoEnabled() {
		return false;
	}

	/**
	 * Checks if is trace enabled.
	 *
	 * @return true, if is trace enabled
	 * @see Logger#isTraceEnabled()
	 */
	public boolean isTraceEnabled() {
		return false;
	}

	/**
	 * Checks if is warn enabled.
	 *
	 * @return true, if is warn enabled
	 * @see Logger#isWarnEnabled()
	 */
	public boolean isWarnEnabled() {
		return false;
	}

	/**
	 * Trace.
	 *
	 * @param arg0 the arg0
	 * @see Logger#trace(String)
	 */
	public void trace(final String arg0) {
		this.record(arg0);
	}

	/**
	 * Trace.
	 *
	 * @param arg0 the arg0
	 * @param arg1 the arg1
	 * @see Logger#trace(String, Throwable)
	 */
	public void trace(final String arg0, final Throwable arg1) {
		this.record(arg0, arg1);
	}

	/**
	 * Warn.
	 *
	 * @param arg0 the arg0
	 * @see Logger#warn(String)
	 */
	public void warn(final String arg0) {
		this.record(arg0);
	}

	/**
	 * Warn.
	 *
	 * @param arg0 the arg0
	 * @param arg1 the arg1
	 * @see Logger#warn(String, Throwable)
	 */
	public void warn(final String arg0, final Throwable arg1) {
		this.record(arg0, arg1);
	}

	/**
	 * Record.
	 *
	 * @param obj the obj
	 */
	private void record(final String obj) {
	    this.objects.add(obj);
	}

    /**
     * Record.
     *
     * @param obj the obj
     * @param t the t
     */
    @NullArgsAllowed
    private synchronized void record(final String msg, final Object obj) {
        this.objects.add(msg + ", " + obj);
    }

    /**
     * Record.
     *
     * @param obj the obj
     * @param t the t
     */
    @NullArgsAllowed
    private synchronized void record(final String msg, final Object[] objs) {
        this.objects.add(msg + ", " + Converter.toString(objs));
    }

    /**
     * Record.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     */
    @NullArgsAllowed
    private synchronized void record(final Marker arg0, final String arg1) {
        this.objects.add(arg0 + ": " + arg1);
    }

    /**
     * Record.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     */
    @NullArgsAllowed
    private synchronized void record(final String arg0, final Object arg1, final Object arg2) {
        this.objects.add(arg0 + ", " + arg1 + ", " + arg2);
    }

    /**
     * Record.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     */
    @NullArgsAllowed
    private synchronized void record(final Marker arg0, final String arg1, final Object arg2) {
        this.objects.add(arg0 + ": " + arg1 + ", " + arg2);
    }

    /**
     * Record.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @param arg3 the arg3
     */
    @NullArgsAllowed
    private synchronized void record(final Marker arg0, final String arg1, final Object arg2, final Object arg3) {
        this.objects.add(arg0 + ": " + arg1 + ", " + arg2 + ", " + arg3);
    }

    /**
     * Record.
     *
     * @param obj the obj
     * @param t the t
     */
    @NullArgsAllowed
    private synchronized void record(final String obj, final Throwable t) {
        this.objects.add(obj);
        this.exceptions.add(t);
        this.n++;
    }

    /**
     * Record.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     */
    @NullArgsAllowed
    private synchronized void record(final Marker arg0, final String arg1, final Throwable arg2) {
        this.record(arg0 + ": " + arg1, arg2);
    }

	/**
	 * Gets the text.
	 *
	 * @return the text
	 */
	public String getText() {
	    StringBuilder text = new StringBuilder();
		for (Object obj : this.objects) {
		    text.append(obj);
		}
		return text.toString().trim();
	}

	/**
	 * Gets the record.
	 *
	 * @return the record
	 */
	public String getRecord() {
		return getText();
	}

	/**
	 * Gets the number of records.
	 *
	 * @return the number of records
	 */
	public int getNumberOfRecords() {
		return n;
	}

    /**
     * To string.
     *
     * @return the string
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        String classname = this.getClass().getSimpleName();
        if (n == 1) {
            return classname + "(\"" + getRecord() + "\")";
        } else {
            return classname + " with " + n + " records";
        }
    }



    /////   new methods after switch from commons-logging to SLF4J   //////////

    /**
     * Debug.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @see org.slf4j.Logger#debug(java.lang.String, java.lang.Object)
     */
    public void debug(final String arg0, final Object arg1) {
        this.record(arg0, arg1);
    }

    /**
     * Debug.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @see org.slf4j.Logger#debug(java.lang.String, java.lang.Object[])
     */
    public void debug(final String arg0, final Object... arg1) {
        this.record(arg0, arg1);
    }

    /**
     * Debug.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @see org.slf4j.Logger#debug(org.slf4j.Marker, java.lang.String)
     */
    public void debug(final Marker arg0, final String arg1) {
        this.record(arg0, arg1);
    }

    /**
     * Debug.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @see org.slf4j.Logger#debug(java.lang.String, java.lang.Object, java.lang.Object)
     */
    public void debug(final String arg0, final Object arg1, final Object arg2) {
        this.record(arg0, arg1, arg2);
    }

    /**
     * Debug.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @see org.slf4j.Logger#debug(org.slf4j.Marker, java.lang.String, java.lang.Object)
     */
    public void debug(final Marker arg0, final String arg1, final Object arg2) {
        this.record(arg0, arg1, arg2);
    }

    /**
     * Debug.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @see org.slf4j.Logger#debug(org.slf4j.Marker, java.lang.String, java.lang.Object[])
     */
    public void debug(final Marker arg0, final String arg1, final Object... arg2) {
        this.record(arg0, arg1, arg2);
    }

    /**
     * Debug.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @see org.slf4j.Logger#debug(org.slf4j.Marker, java.lang.String, java.lang.Throwable)
     */
    public void debug(final Marker arg0, final String arg1, final Throwable arg2) {
        this.record(arg0, arg1, arg2);
    }

    /**
     * Debug.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @param arg3 the arg3
     * @see org.slf4j.Logger#debug(org.slf4j.Marker, java.lang.String, java.lang.Object, java.lang.Object)
     */
    public void debug(final Marker arg0, final String arg1, final Object arg2, final Object arg3) {
        this.record(arg0, arg1, arg2, arg3);
    }

    /**
     * Error.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @see org.slf4j.Logger#error(java.lang.String, java.lang.Object)
     */
    public void error(final String arg0, final Object arg1) {
        this.record(arg0, arg1);
    }

    /**
     * Error.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @see org.slf4j.Logger#error(java.lang.String, java.lang.Object[])
     */
    public void error(final String arg0, final Object... arg1) {
        this.record(arg0, arg1);
    }

    /**
     * Error.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @see org.slf4j.Logger#error(org.slf4j.Marker, java.lang.String)
     */
    public void error(final Marker arg0, final String arg1) {
        this.record(arg0, arg1);
    }

    /**
     * Error.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @see org.slf4j.Logger#error(java.lang.String, java.lang.Object, java.lang.Object)
     */
    public void error(final String arg0, final Object arg1, final Object arg2) {
        this.record(arg0, arg1, arg2);
    }

    /**
     * Error.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @see org.slf4j.Logger#error(org.slf4j.Marker, java.lang.String, java.lang.Object)
     */
    public void error(final Marker arg0, final String arg1, final Object arg2) {
        this.record(arg0, arg1, arg2);
    }

    /**
     * Error.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @see org.slf4j.Logger#error(org.slf4j.Marker, java.lang.String, java.lang.Object[])
     */
    public void error(final Marker arg0, final String arg1, final Object... arg2) {
        this.record(arg0, arg1, arg2);
    }

    /**
     * Error.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @see org.slf4j.Logger#error(org.slf4j.Marker, java.lang.String, java.lang.Throwable)
     */
    public void error(final Marker arg0, final String arg1, final Throwable arg2) {
        this.record(arg0, arg1, arg2);
    }

    /**
     * Error.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @param arg3 the arg3
     * @see org.slf4j.Logger#error(org.slf4j.Marker, java.lang.String, java.lang.Object, java.lang.Object)
     */
    public void error(final Marker arg0, final String arg1, final Object arg2, final Object arg3) {
        this.record(arg0, arg1, arg2, arg3);
    }

    /**
     * Gets the name.
     *
     * @return the name
     * @see org.slf4j.Logger#getName()
     */
    public String getName() {
        return "LogRecorder";
    }

    /**
     * Info.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @see org.slf4j.Logger#info(java.lang.String, java.lang.Object)
     */
    public void info(final String arg0, final Object arg1) {
        this.record(arg0, arg1);
    }

    /**
     * Info.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @see org.slf4j.Logger#info(java.lang.String, java.lang.Object[])
     */
    public void info(final String arg0, final Object... arg1) {
        this.record(arg0, arg1);
    }

    /**
     * Info.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @see org.slf4j.Logger#info(org.slf4j.Marker, java.lang.String)
     */
    public void info(final Marker arg0, final String arg1) {
        this.record(arg0, arg1);
    }

    /**
     * Info.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @see org.slf4j.Logger#info(java.lang.String, java.lang.Object, java.lang.Object)
     */
    public void info(final String arg0, final Object arg1, final Object arg2) {
        this.record(arg0, arg1, arg2);
    }

    /**
     * Info.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @see org.slf4j.Logger#info(org.slf4j.Marker, java.lang.String, java.lang.Object)
     */
    public void info(final Marker arg0, final String arg1, final Object arg2) {
        this.record(arg0, arg1, arg2);
    }

    /**
     * Info.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @see org.slf4j.Logger#info(org.slf4j.Marker, java.lang.String, java.lang.Object[])
     */
    public void info(final Marker arg0, final String arg1, final Object... arg2) {
        this.record(arg0, arg1, arg2);
    }

    /**
     * Info.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @see org.slf4j.Logger#info(org.slf4j.Marker, java.lang.String, java.lang.Throwable)
     */
    public void info(final Marker arg0, final String arg1, final Throwable arg2) {
        this.record(arg0, arg1, arg2);
    }

    /**
     * Info.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @param arg3 the arg3
     * @see org.slf4j.Logger#info(org.slf4j.Marker, java.lang.String, java.lang.Object, java.lang.Object)
     */
    public void info(final Marker arg0, final String arg1, final Object arg2, final Object arg3) {
        this.record(arg0, arg1, arg2, arg3);
    }

    /**
     * Checks if is debug enabled.
     *
     * @param arg0 the arg0
     * @return true, if is debug enabled
     * @see org.slf4j.Logger#isDebugEnabled(org.slf4j.Marker)
     */
    public boolean isDebugEnabled(final Marker arg0) {
        return false;
    }

    /**
     * Checks if is error enabled.
     *
     * @param arg0 the arg0
     * @return true, if is error enabled
     * @see org.slf4j.Logger#isErrorEnabled(org.slf4j.Marker)
     */
    public boolean isErrorEnabled(final Marker arg0) {
        return false;
    }

    /**
     * Checks if is info enabled.
     *
     * @param arg0 the arg0
     * @return true, if is info enabled
     * @see org.slf4j.Logger#isInfoEnabled(org.slf4j.Marker)
     */
    public boolean isInfoEnabled(final Marker arg0) {
        return false;
    }

    /**
     * Checks if is trace enabled.
     *
     * @param arg0 the arg0
     * @return true, if is trace enabled
     * @see org.slf4j.Logger#isTraceEnabled(org.slf4j.Marker)
     */
    public boolean isTraceEnabled(final Marker arg0) {
        return false;
    }

    /**
     * Checks if is warn enabled.
     *
     * @param arg0 the arg0
     * @return true, if is warn enabled
     * @see org.slf4j.Logger#isWarnEnabled(org.slf4j.Marker)
     */
    public boolean isWarnEnabled(final Marker arg0) {
        return false;
    }

    /**
     * Trace.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @see org.slf4j.Logger#trace(java.lang.String, java.lang.Object)
     */
    public void trace(final String arg0, final Object arg1) {
        this.record(arg0, arg1);
    }

    /**
     * Trace.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @see org.slf4j.Logger#trace(java.lang.String, java.lang.Object[])
     */
    public void trace(final String arg0, final Object... arg1) {
        this.record(arg0, arg1);
    }

    /**
     * Trace.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @see org.slf4j.Logger#trace(org.slf4j.Marker, java.lang.String)
     */
    public void trace(final Marker arg0, final String arg1) {
        this.record(arg0, arg1);
    }

    /**
     * Trace.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @see org.slf4j.Logger#trace(java.lang.String, java.lang.Object, java.lang.Object)
     */
    public void trace(final String arg0, final Object arg1, final Object arg2) {
        this.record(arg0, arg1, arg2);
    }

    /**
     * Trace.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @see org.slf4j.Logger#trace(org.slf4j.Marker, java.lang.String, java.lang.Object)
     */
    public void trace(final Marker arg0, final String arg1, final Object arg2) {
        this.record(arg0, arg1, arg2);
    }

    /**
     * Trace.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @see org.slf4j.Logger#trace(org.slf4j.Marker, java.lang.String, java.lang.Object[])
     */
    public void trace(final Marker arg0, final String arg1, final Object... arg2) {
        this.record(arg0, arg1, arg2);
    }

    /**
     * Trace.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @see org.slf4j.Logger#trace(org.slf4j.Marker, java.lang.String, java.lang.Throwable)
     */
    public void trace(final Marker arg0, final String arg1, final Throwable arg2) {
        this.record(arg0, arg1, arg2);
    }

    /**
     * Trace.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @param arg3 the arg3
     * @see org.slf4j.Logger#trace(org.slf4j.Marker, java.lang.String, java.lang.Object, java.lang.Object)
     */
    public void trace(final Marker arg0, final String arg1, final Object arg2, final Object arg3) {
        this.record(arg0, arg1, arg2, arg3);
    }

    /**
     * Warn.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @see org.slf4j.Logger#warn(java.lang.String, java.lang.Object)
     */
    public void warn(final String arg0, final Object arg1) {
        this.record(arg0, arg1);
    }

    /**
     * Warn.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @see org.slf4j.Logger#warn(java.lang.String, java.lang.Object[])
     */
    public void warn(final String arg0, final Object... arg1) {
        this.record(arg0, arg1);
    }

    /**
     * Warn.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @see org.slf4j.Logger#warn(org.slf4j.Marker, java.lang.String)
     */
    public void warn(final Marker arg0, final String arg1) {
        this.record(arg0, arg1);
    }

    /**
     * Warn.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @see org.slf4j.Logger#warn(java.lang.String, java.lang.Object, java.lang.Object)
     */
    public void warn(final String arg0, final Object arg1, final Object arg2) {
        this.record(arg0, arg1, arg2);
    }

    /**
     * Warn.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @see org.slf4j.Logger#warn(org.slf4j.Marker, java.lang.String, java.lang.Object)
     */
    public void warn(final Marker arg0, final String arg1, final Object arg2) {
        this.record(arg0, arg1, arg2);
    }

    /**
     * Warn.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @see org.slf4j.Logger#warn(org.slf4j.Marker, java.lang.String, java.lang.Object[])
     */
    public void warn(final Marker arg0, final String arg1, final Object... arg2) {
        this.record(arg0, arg1, arg2);
    }

    /**
     * Warn.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @see org.slf4j.Logger#warn(org.slf4j.Marker, java.lang.String, java.lang.Throwable)
     */
    public void warn(final Marker arg0, final String arg1, final Throwable arg2) {
        this.record(arg0, arg1, arg2);
    }

    /**
     * Warn.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @param arg3 the arg3
     * @see org.slf4j.Logger#warn(org.slf4j.Marker, java.lang.String, java.lang.Object, java.lang.Object)
     */
    public void warn(final Marker arg0, final String arg1, final Object arg2, final Object arg3) {
        this.record(arg0, arg1, arg2, arg3);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy