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

patterntesting.runtime.log.AbstractLogger 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: AbstractLogger.java,v 1.4 2014/01/04 21:56:42 oboehm Exp $
 *
 * Copyright (c) 2013 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 07.09.2013 by oliver ([email protected])
 */

package patterntesting.runtime.log;

import java.io.*;

import org.apache.commons.io.IOUtils;
import org.slf4j.*;

import patterntesting.runtime.io.BetterFileOutputStream;

/**
 * This is a common super class for all classes which do a kind of logging.
 * It provides an {@link OutputStream} and guarantees that this stream is
 * closed at least at shutdown.
 *
 * @author oliver
 * @since 1.3.1 (07.09.2013)
 */
public abstract class AbstractLogger extends Thread {

    private static Logger log = LoggerFactory.getLogger(AbstractLogger.class);
    private final OutputStream logStream;

    /**
     * Instantiates a new abstract logger.
     *
     * @param ostream the ostream
     */
    protected AbstractLogger(final OutputStream ostream) {
        this.logStream = ostream;
        Runtime.getRuntime().addShutdownHook(this);
        log.debug("{} is registered as shutdown hook for closing {}.", this, this.logStream);
    }

    /**
     * Creates a file in the temp directory. If this was not successful a file
     * prefix + suffix is returned as result.
     *
     * @param prefix the prefix
     * @param suffix the suffix
     * @return the file
     */
    protected static File createTempLogFile(final String prefix, final String suffix) {
        try {
            return File.createTempFile(prefix, suffix);
        } catch (IOException ioe) {
            log.info("Cannot create temporary log file.", ioe);
            return new File(prefix + suffix);
        }
    }

    /**
     * Gets the stream for the given log file. As fallback stdout
     * will be used.
     *
     * @param logFile the log file
     * @return the stream for
     */
    protected static OutputStream getStreamFor(final File logFile) {
        if (logFile.exists()) {
            if (logFile.delete()) {
                log.info("Old log file \"{}\" was deleted.", logFile);
            } else {
                log.warn("Old log file \"{}\" could not be deleted!", logFile);
            }
        }
        try {
            return new BetterFileOutputStream(logFile, true);
        } catch (FileNotFoundException fnfe) {
            log.info("Will log to stdout because cannot log to \"{}\" ({}).", logFile,
                    fnfe.getMessage());
            return System.out;
        }
    }

    /**
     * Gets the log stream.
     *
     * @return the logStream
     */
    protected final OutputStream getLogStream() {
        return logStream;
    }

    /**
     * This method is called at shutdown to close the open stream.
     * Otherwise the closing "" tag in the generated file
     * would be missing (it is generated by the used XStream library).
     *
     * @see java.lang.Thread#run()
     */
    @Override
    public void run() {
        IOUtils.closeQuietly(this.logStream);
        log.debug("{} is closed.", this.logStream);
    }

    /**
     * Closes the stream with the logged objects.
     */
    public void close() {
        IOUtils.closeQuietly(this.logStream);
        Runtime.getRuntime().removeShutdownHook(this);
        log.debug("{} is closed and {} removed as shutdown hook.", this.logStream, this);
    }

    /**
     * To string.
     *
     * @return the string
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return this.getClass().getSimpleName() + "@" + Integer.toString(this.hashCode(), Character.MAX_RADIX);
    }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy