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:
043: package org.jfree.util;
044:
045: /**
046: * A log context.
047: *
048: * @author Thomas Morgner
049: */
050: public 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: * A convenience method for logging a 'debug' message.
106: *
107: * @param message the message.
108: */
109: public void debug(final Object message) {
110: log(LogTarget.DEBUG, message);
111: }
112:
113: /**
114: * A convenience method for logging a 'debug' message.
115: *
116: * @param message the message.
117: * @param e the exception.
118: */
119: public void debug(final Object message, final Exception e) {
120: log(LogTarget.DEBUG, message, e);
121: }
122:
123: /**
124: * A convenience method for logging an 'info' message.
125: *
126: * @param message the message.
127: */
128: public void info(final Object message) {
129: log(LogTarget.INFO, message);
130: }
131:
132: /**
133: * A convenience method for logging an 'info' message.
134: *
135: * @param message the message.
136: * @param e the exception.
137: */
138: public void info(final Object message, final Exception e) {
139: log(LogTarget.INFO, message, e);
140: }
141:
142: /**
143: * A convenience method for logging a 'warning' message.
144: *
145: * @param message the message.
146: */
147: public void warn(final Object message) {
148: log(LogTarget.WARN, message);
149: }
150:
151: /**
152: * A convenience method for logging a 'warning' message.
153: *
154: * @param message the message.
155: * @param e the exception.
156: */
157: public void warn(final Object message, final Exception e) {
158: log(LogTarget.WARN, message, e);
159: }
160:
161: /**
162: * A convenience method for logging an 'error' message.
163: *
164: * @param message the message.
165: */
166: public void error(final Object message) {
167: log(LogTarget.ERROR, message);
168: }
169:
170: /**
171: * A convenience method for logging an 'error' message.
172: *
173: * @param message the message.
174: * @param e the exception.
175: */
176: public void error(final Object message, final Exception e) {
177: log(LogTarget.ERROR, message, e);
178: }
179:
180: /**
181: * Logs a message to the main log stream. All attached log targets will also
182: * receive this message. If the given log-level is higher than the given debug-level
183: * in the main config file, no logging will be done.
184: *
185: * @param level log level of the message.
186: * @param message text to be logged.
187: */
188: public void log(final int level, final Object message) {
189: if (this .contextPrefix != null) {
190: Log.getInstance().doLog(
191: level,
192: new Log.SimpleMessage(this .contextPrefix, ":",
193: message));
194: } else {
195: Log.getInstance().doLog(level, message);
196: }
197: }
198:
199: /**
200: * Logs a message to the main log stream. All attached logTargets will also
201: * receive this message. If the given log-level is higher than the given debug-level
202: * in the main config file, no logging will be done.
203: * <p/>
204: * The exception's stacktrace will be appended to the log-stream
205: *
206: * @param level log level of the message.
207: * @param message text to be logged.
208: * @param e the exception, which should be logged.
209: */
210: public void log(final int level, final Object message,
211: final Exception e) {
212: if (this .contextPrefix != null) {
213: Log.getInstance().doLog(
214: level,
215: new Log.SimpleMessage(this .contextPrefix, ":",
216: message), e);
217: } else {
218: Log.getInstance().doLog(level, message, e);
219: }
220: }
221:
222: /**
223: * Tests this object for equality with an arbitrary object.
224: *
225: * @param o the object to test against (<code>null</code> permitted).
226: *
227: * @return A boolean.
228: */
229: public boolean equals(final Object o) {
230: if (this == o) {
231: return true;
232: }
233: if (!(o instanceof LogContext)) {
234: return false;
235: }
236:
237: final LogContext logContext = (LogContext) o;
238:
239: if (this .contextPrefix != null) {
240: if (!this .contextPrefix.equals(logContext.contextPrefix)) {
241: return false;
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
259: .hashCode() : 0);
260: }
261: }
|