001: package org.apache.dvsl;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.apache.tools.ant.Project;
023: import org.apache.tools.ant.Task;
024:
025: import org.apache.velocity.runtime.RuntimeServices;
026:
027: import org.apache.velocity.runtime.log.LogChute;
028:
029: /**
030: * Implementation of a logger to output messages via an Ant Task's log
031: * method. Velocity log levels are mapped to corresponding log levels
032: * defined in Ant's logging API. The end result is messages will only
033: * be output if Ant log level is high enough.
034: *
035: * @author <a href="mailto:billb@progress.com">Bill Burton</a>
036: * @version $Id:$
037: */
038: public class AntLogChute implements LogChute {
039:
040: // Reference to the Ant Task object that initialized the Velocity Engine.
041: Task task;
042:
043: /**
044: * Initialize this logger with a reference to the calling Ant Task
045: *
046: * @param task Ant Task to use for logging. This must not be null.
047: */
048: public AntLogChute(Task task) {
049: this .task = task;
050: }
051:
052: /**
053: * Initialize the logger.
054: *
055: * @throws Exception if null was passed into the constructor
056: */
057: public void init(RuntimeServices rs) throws Exception {
058: if (task == null) {
059: throw new Exception(
060: "PANIC: "
061: + this .getClass().getName()
062: + " was instantiated with a null Ant Task reference");
063: }
064: }
065:
066: /**
067: * <p>
068: * Log Velocity messages through the Ant Task log method. The mapping of logging
069: * levels from Velocity to Ant is as follows:
070: * </p>
071: *
072: * <blockquote><pre>
073: * Velocity Level --> Ant Level
074: * LogSystem.TRACE_ID --> Project.MSG_DEBUG
075: * LogSystem.DEBUG_ID --> Project.MSG_DEBUG
076: * LogSystem.INFO_ID --> Project.MSG_VERBOSE
077: * LogSystem.WARN_ID --> Project.MSG_WARN
078: * LogSystem.ERROR_ID --> Project.MSG_ERR
079: * </pre></blockquote>
080: *
081: * @param level severity level
082: * @param message complete error message
083: * @see org.apache.velocity.runtime.log.LogChute
084: * @see org.apache.tools.ant.Task#log(String, int)
085: */
086: public void log(int level, String message) {
087: switch (level) {
088: case LogChute.TRACE_ID:
089: task
090: .log(LogChute.TRACE_PREFIX + message,
091: Project.MSG_DEBUG);
092: break;
093: case LogChute.DEBUG_ID:
094: task
095: .log(LogChute.DEBUG_PREFIX + message,
096: Project.MSG_DEBUG);
097: break;
098: case LogChute.INFO_ID:
099: task.log(LogChute.INFO_PREFIX + message,
100: Project.MSG_VERBOSE);
101: break;
102: case LogChute.WARN_ID:
103: task.log(LogChute.WARN_PREFIX + message, Project.MSG_WARN);
104: break;
105: case LogChute.ERROR_ID:
106: task.log(LogChute.ERROR_PREFIX + message, Project.MSG_ERR);
107: break;
108: default:
109: task.log(message);
110: break;
111: }
112: }
113:
114: /**
115: * <p>
116: * Log throwables through the Ant Task log method. The mapping of logging
117: * levels from Velocity to Ant is as follows:
118: * </p>
119: *
120: * <blockquote><pre>
121: * Velocity Level --> Ant Level
122: * LogSystem.TRACE_ID --> Project.MSG_DEBUG
123: * LogSystem.DEBUG_ID --> Project.MSG_DEBUG
124: * LogSystem.INFO_ID --> Project.MSG_VERBOSE
125: * LogSystem.WARN_ID --> Project.MSG_WARN
126: * LogSystem.ERROR_ID --> Project.MSG_ERR
127: * </pre></blockquote>
128: *
129: * @param level severity level
130: * @param message complete error message
131: * @param throwable the throwable object to log
132: * @see org.apache.velocity.runtime.log.LogChute
133: * @see org.apache.tools.ant.Task#log(String, int)
134: */
135: public void log(int level, String message, Throwable throwable) {
136: switch (level) {
137: case LogChute.TRACE_ID:
138: task.log(LogChute.TRACE_PREFIX + message, throwable,
139: Project.MSG_DEBUG);
140: break;
141: case LogChute.DEBUG_ID:
142: task.log(LogChute.DEBUG_PREFIX + message, throwable,
143: Project.MSG_DEBUG);
144: break;
145: case LogChute.INFO_ID:
146: task.log(LogChute.INFO_PREFIX + message, throwable,
147: Project.MSG_VERBOSE);
148: break;
149: case LogChute.WARN_ID:
150: task.log(LogChute.WARN_PREFIX + message, throwable,
151: Project.MSG_WARN);
152: break;
153: case LogChute.ERROR_ID:
154: task.log(LogChute.ERROR_PREFIX + message, throwable,
155: Project.MSG_ERR);
156: break;
157: default:
158: task.log(message);
159: break;
160: }
161: }
162:
163: public boolean isLevelEnabled(int level) {
164: return true;
165: }
166:
167: public void logVelocityMessage(int level, String message) {
168: }
169: }
|