001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.lib.log;
020:
021: import java.io.PrintWriter;
022: import java.io.StringWriter;
023:
024: /**
025: * A simple implementation of the {@link Log} interface. Writes
026: * output to stdout.
027: *
028: * @author Patrick Linskey
029: */
030: public abstract class AbstractLog implements Log {
031:
032: /**
033: * Check to see if the specified logging level is enabled.
034: */
035: protected abstract boolean isEnabled(short level);
036:
037: /**
038: * Send the specified log message to the handler.
039: *
040: * @param level the level of the log message
041: * @param message the message to send
042: * @param t the Throwable, or null if this is not an error
043: */
044: protected abstract void log(short level, String message, Throwable t);
045:
046: public boolean isTraceEnabled() {
047: return isEnabled(TRACE);
048: }
049:
050: public boolean isInfoEnabled() {
051: return isEnabled(INFO);
052: }
053:
054: public boolean isWarnEnabled() {
055: return isEnabled(WARN);
056: }
057:
058: public boolean isErrorEnabled() {
059: return isEnabled(ERROR);
060: }
061:
062: public boolean isFatalEnabled() {
063: return isEnabled(FATAL);
064: }
065:
066: public void trace(Object message) {
067: trace(message, throwableParam(message, null));
068: }
069:
070: public void trace(Object message, Throwable t) {
071: if (isTraceEnabled())
072: log(TRACE, toString(message), throwableParam(message, t));
073: }
074:
075: public void info(Object message) {
076: info(message, throwableParam(message, null));
077: }
078:
079: public void info(Object message, Throwable t) {
080: if (isInfoEnabled())
081: log(INFO, toString(message), throwableParam(message, t));
082: }
083:
084: public void warn(Object message) {
085: warn(message, throwableParam(message, null));
086: }
087:
088: public void warn(Object message, Throwable t) {
089: if (isWarnEnabled())
090: log(WARN, toString(message), throwableParam(message, t));
091: }
092:
093: public void error(Object message) {
094: error(message, throwableParam(message, null));
095: }
096:
097: public void error(Object message, Throwable t) {
098: if (isErrorEnabled())
099: log(ERROR, toString(message), throwableParam(message, t));
100: }
101:
102: public void fatal(Object message) {
103: fatal(message, throwableParam(message, null));
104: }
105:
106: public void fatal(Object message, Throwable t) {
107: if (isFatalEnabled())
108: log(FATAL, toString(message), throwableParam(message, t));
109: }
110:
111: /**
112: * Utility method to obtain a stack trace as a String.
113: */
114: protected static String getStackTrace(Throwable t) {
115: StringWriter sw = new StringWriter();
116: PrintWriter pw = new PrintWriter(sw, true);
117: t.printStackTrace(pw);
118: pw.close();
119: return sw.toString();
120: }
121:
122: /**
123: * If either given instance is a throwable, return it.
124: */
125: private static Throwable throwableParam(Object message, Throwable t) {
126: if (t != null)
127: return t;
128: if (message instanceof Throwable)
129: return (Throwable) message;
130:
131: return null;
132: }
133:
134: /**
135: * Efficiently turn the given object into a string.
136: */
137: private static String toString(Object o) {
138: return (o == null) ? "null" : o.toString();
139: }
140: }
|