001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs;
020:
021: import java.io.IOException;
022:
023: import org.apache.tools.ant.Project;
024: import org.apache.tools.ant.ProjectComponent;
025: import org.apache.tools.ant.Task;
026: import org.apache.tools.ant.util.LineOrientedOutputStream;
027:
028: /**
029: * Logs each line written to this stream to the log system of ant.
030: *
031: * Tries to be smart about line separators.<br>
032: *
033: * @since Ant 1.2
034: */
035: public class LogOutputStream extends LineOrientedOutputStream {
036:
037: private ProjectComponent pc;
038: private int level = Project.MSG_INFO;
039:
040: /**
041: * Creates a new instance of this class.
042: *
043: * @param task the task for whom to log
044: * @param level loglevel used to log data written to this stream.
045: */
046: public LogOutputStream(Task task, int level) {
047: this ((ProjectComponent) task, level);
048: }
049:
050: /**
051: * Creates a new instance of this class.
052: *
053: * @param pc the project component for whom to log
054: * @param level loglevel used to log data written to this stream.
055: * @since Ant 1.6.3
056: */
057: public LogOutputStream(ProjectComponent pc, int level) {
058: this .pc = pc;
059: this .level = level;
060: }
061:
062: /**
063: * Converts the buffer to a string and sends it to <code>processLine</code>
064: */
065: protected void processBuffer() {
066: try {
067: super .processBuffer();
068: } catch (IOException e) {
069: // impossible since *our* processLine doesn't throw an IOException
070: throw new RuntimeException(
071: "Impossible IOException caught: " + e);
072: }
073: }
074:
075: /**
076: * Logs a line to the log system of ant.
077: *
078: * @param line the line to log.
079: */
080: protected void processLine(String line) {
081: processLine(line, level);
082: }
083:
084: /**
085: * Logs a line to the log system of ant.
086: *
087: * @param line the line to log.
088: * @param level the logging level to use.
089: */
090: protected void processLine(String line, int level) {
091: pc.log(line, level);
092: }
093:
094: /**
095: * Get the level.
096: * @return the log level.
097: */
098: public int getMessageLevel() {
099: return level;
100: }
101:
102: }
|