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: * PrintStreamLogTarget.java
029: * -------------------------
030: * (C)opyright 2003-2005, by Thomas Morgner and Contributors.
031: *
032: * Original Author: Thomas Morgner;
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * $Id: PrintStreamLogTarget.java,v 1.6 2005/10/18 13:24:19 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 02-Dec-2003 : Initial version
040: * 11-Feb-2004 : Added missing Javadocs (DG);
041: *
042: */
043:
044: package org.jfree.util;
045:
046: import java.io.PrintStream;
047: import java.io.Serializable;
048:
049: /**
050: * A log target that sends output to a {@link PrintStream}.
051: *
052: * @author Thomas Morgner
053: */
054: public class PrintStreamLogTarget implements LogTarget, Serializable {
055:
056: /** For serialization. */
057: private static final long serialVersionUID = 6510564403264504688L;
058:
059: /** The printstream we use .. */
060: private PrintStream printStream;
061:
062: /**
063: * The default constructor. Initializes this target with the system.out
064: * stream.
065: * <p>
066: * All {@link org.jfree.util.LogTarget} implementations need a default
067: * constructor.
068: */
069: public PrintStreamLogTarget() {
070: this (System.out);
071: }
072:
073: /**
074: * The default constructor. Initializes this target with the given stream.
075: * <p>
076: * @param printStream the print stream that is used to write the content.
077: */
078: public PrintStreamLogTarget(final PrintStream printStream) {
079: if (printStream == null) {
080: throw new NullPointerException();
081: }
082: this .printStream = printStream;
083: }
084:
085: /**
086: * Logs a message to the main log stream. All attached logStreams will also
087: * receive this message. If the given log-level is higher than the given
088: * debug-level in the main config file, no logging will be done.
089: *
090: * @param level log level of the message.
091: * @param message text to be logged.
092: */
093: public void log(int level, final Object message) {
094: if (level > 3) {
095: level = 3;
096: }
097: this .printStream.print(LEVELS[level]);
098: this .printStream.println(message);
099: if (level < 3) {
100: System.out.flush();
101: }
102: }
103:
104: /**
105: * logs an message to the main-log stream. All attached logStreams will also
106: * receive this message. If the given log-level is higher than the given
107: * debug-level in the main config file, no logging will be done.
108: *
109: * The exception's stacktrace will be appended to the log-stream
110: *
111: * @param level log level of the message.
112: * @param message text to be logged.
113: * @param e the exception, which should be logged.
114: */
115: public void log(int level, final Object message, final Exception e) {
116: if (level > 3) {
117: level = 3;
118: }
119: this .printStream.print(LEVELS[level]);
120: this .printStream.println(message);
121: e.printStackTrace(this .printStream);
122: if (level < 3) {
123: System.out.flush();
124: }
125: }
126: }
|