    001/* ========================================================================
    002 * JCommon : a free general purpose class library for the Java(tm) platform
    003 * ========================================================================
    004 *
    005 * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
    006 *
    007 * Project Info:  http://www.jfree.org/jcommon/index.html
    008 *
    009 * This library is free software; you can redistribute it and/or modify it
    010 * under the terms of the GNU Lesser General Public License as published by
    011 * the Free Software Foundation; either version 2.1 of the License, or
    012 * (at your option) any later version.
    013 *
    014 * This library is distributed in the hope that it will be useful, but
    015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    017 * License for more details.
    018 *
    019 * You should have received a copy of the GNU Lesser General Public
    020 * License along with this library; if not, write to the Free Software
    021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
    022 * USA.
    023 *
    024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
    025 * in the United States and other countries.]
    026 *
    027 * ---------------
    028 * LogContext.java
    029 * ---------------
    030 * (C)opyright 2004, by Thomas Morgner and Contributors.
    031 *
    032 * Original Author:  Thomas Morgner;
    033 * Contributor(s):   David Gilbert (for Object Refinery Limited);
    034 *
    035 * $Id: LogContext.java,v 1.3 2005/10/18 13:24:19 mungady Exp $
    036 *
    037 * Changes
    038 * -------
    039 * 26-Apr-2004 : Initial version (TM);
    040 *
    041 */
    042
    043package org.jfree.util;
    044
    045/**
    046 * A log context.
    047 *
    048 * @author Thomas Morgner
    049 */
    050public class LogContext {
    051
    052    /** The prefix string. */
    053    private String contextPrefix;
    054
    055    /**
    056     * Creates a new log context.
    057     *
    058     * @param contextPrefix  the prefix.
    059     */
    060    public LogContext(final String contextPrefix) {
    061        this.contextPrefix = contextPrefix;
    062    }
    063
    064    /**
    065     * Returns true, if the log level allows debug messages to be
    066     * printed.
    067     *
    068     * @return true, if messages with an log level of DEBUG are allowed.
    069     */
    070    public boolean isDebugEnabled() {
    071        return Log.isDebugEnabled();
    072    }
    073
    074    /**
    075     * Returns true, if the log level allows informational
    076     * messages to be printed.
    077     *
    078     * @return true, if messages with an log level of INFO are allowed.
    079     */
    080    public boolean isInfoEnabled() {
    081        return Log.isInfoEnabled();
    082    }
    083
    084    /**
    085     * Returns true, if the log level allows warning messages to be
    086     * printed.
    087     *
    088     * @return true, if messages with an log level of WARN are allowed.
    089     */
    090    public boolean isWarningEnabled() {
    091        return Log.isWarningEnabled();
    092    }
    093
    094    /**
    095     * Returns true, if the log level allows error messages to be
    096     * printed.
    097     *
    098     * @return true, if messages with an log level of ERROR are allowed.
    099     */
    100    public boolean isErrorEnabled() {
    101        return Log.isErrorEnabled();
    102    }
    103
    104
    105    /**
    106     * A convenience method for logging a 'debug' message.
    107     *
    108     * @param message the message.
    109     */
    110    public void debug(final Object message) {
    111        log(LogTarget.DEBUG, message);
    112    }
    113
    114    /**
    115     * A convenience method for logging a 'debug' message.
    116     *
    117     * @param message the message.
    118     * @param e       the exception.
    119     */
    120    public void debug(final Object message, final Exception e) {
    121        log(LogTarget.DEBUG, message, e);
    122    }
    123
    124    /**
    125     * A convenience method for logging an 'info' message.
    126     *
    127     * @param message the message.
    128     */
    129    public void info(final Object message) {
    130        log(LogTarget.INFO, message);
    131    }
    132
    133    /**
    134     * A convenience method for logging an 'info' message.
    135     *
    136     * @param message the message.
    137     * @param e       the exception.
    138     */
    139    public void info(final Object message, final Exception e) {
    140        log(LogTarget.INFO, message, e);
    141    }
    142
    143    /**
    144     * A convenience method for logging a 'warning' message.
    145     *
    146     * @param message the message.
    147     */
    148    public void warn(final Object message) {
    149        log(LogTarget.WARN, message);
    150    }
    151
    152    /**
    153     * A convenience method for logging a 'warning' message.
    154     *
    155     * @param message the message.
    156     * @param e       the exception.
    157     */
    158    public void warn(final Object message, final Exception e) {
    159        log(LogTarget.WARN, message, e);
    160    }
    161
    162    /**
    163     * A convenience method for logging an 'error' message.
    164     *
    165     * @param message the message.
    166     */
    167    public void error(final Object message) {
    168        log(LogTarget.ERROR, message);
    169    }
    170
    171    /**
    172     * A convenience method for logging an 'error' message.
    173     *
    174     * @param message the message.
    175     * @param e       the exception.
    176     */
    177    public void error(final Object message, final Exception e) {
    178        log(LogTarget.ERROR, message, e);
    179    }
    180
    181    /**
    182     * Logs a message to the main log stream.  All attached log targets will also
    183     * receive this message. If the given log-level is higher than the given debug-level
    184     * in the main config file, no logging will be done.
    185     *
    186     * @param level   log level of the message.
    187     * @param message text to be logged.
    188     */
    189    public void log(final int level, final Object message) {
    190        if (this.contextPrefix != null) {
    191            Log.getInstance().doLog(level, new Log.SimpleMessage(this.contextPrefix, ":", message));
    192        }
    193        else {
    194            Log.getInstance().doLog(level, message);
    195        }
    196    }
    197
    198    /**
    199     * Logs a message to the main log stream. All attached logTargets will also
    200     * receive this message. If the given log-level is higher than the given debug-level
    201     * in the main config file, no logging will be done.
    202     * <p/>
    203     * The exception's stacktrace will be appended to the log-stream
    204     *
    205     * @param level   log level of the message.
    206     * @param message text to be logged.
    207     * @param e       the exception, which should be logged.
    208     */
    209    public void log(final int level, final Object message, final Exception e) {
    210        if (this.contextPrefix != null) {
    211            Log.getInstance().doLog(
    212                level, new Log.SimpleMessage(this.contextPrefix, ":", message), e
    213            );
    214        }
    215        else {
    216            Log.getInstance().doLog(level, message, e);
    217        }
    218    }
    219
    220    /**
    221     * Tests this object for equality with an arbitrary object.
    222     *
    223     * @param o  the object to test against (<code>null</code> permitted).
    224     *
    225     * @return A boolean.
    226     */
    227    public boolean equals(final Object o) {
    228        if (this == o) {
    229            return true;
    230        }
    231        if (!(o instanceof LogContext)) {
    232            return false;
    233        }
    234
    235        final LogContext logContext = (LogContext) o;
    236
    237        if (this.contextPrefix != null)
    238        {
    239            if (!this.contextPrefix.equals(logContext.contextPrefix)) {
    240                return false;
    241            }
    242        }
    243        else {
    244            if (logContext.contextPrefix != null) {
    245                return false;
    246            }
    247        }
    248
    249        return true;
    250    }
    251
    252    /**
    253     * Returns a hashcode.
    254     *
    255     * @return The hashcode.
    256     */
    257    public int hashCode() {
    258        return (this.contextPrefix != null ? this.contextPrefix.hashCode() : 0);
    259    }
    260}