View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.logging;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.FilterOutputStream;
23  import java.io.IOException;
24  import java.io.PrintStream;
25  import java.util.function.Consumer;
26  
27  public class LoggingOutputStream extends FilterOutputStream {
28  
29      static final byte[] LINE_SEP = System.lineSeparator().getBytes();
30  
31      final EolBaos buf;
32      final Consumer<String> consumer;
33  
34      public LoggingOutputStream(Consumer<String> consumer) {
35          this(new EolBaos(), consumer);
36      }
37  
38      LoggingOutputStream(EolBaos out, Consumer<String> consumer) {
39          super(out);
40          this.buf = out;
41          this.consumer = consumer;
42      }
43  
44      public PrintStream printStream() {
45          return new LoggingPrintStream(this);
46      }
47  
48      @Override
49      public void write(int b) throws IOException {
50          super.write(b);
51          if (buf.isEol()) {
52              String line = new String(buf.toByteArray(), 0, buf.size() - LINE_SEP.length);
53              ProjectBuildLogAppender.updateMdc();
54              consumer.accept(line);
55              buf.reset();
56          }
57      }
58  
59      public void forceFlush() {
60          if (buf.size() > 0) {
61              String line = new String(buf.toByteArray(), 0, buf.size());
62              ProjectBuildLogAppender.updateMdc();
63              consumer.accept(line);
64              buf.reset();
65          }
66      }
67  
68      static class EolBaos extends ByteArrayOutputStream {
69          boolean isEol() {
70              if (count >= LINE_SEP.length) {
71                  for (int i = 0; i < LINE_SEP.length; i++) {
72                      if (buf[count - LINE_SEP.length + i] != LINE_SEP[i]) {
73                          return false;
74                      }
75                  }
76                  return true;
77              }
78              return false;
79          }
80      }
81  
82      public static class LoggingPrintStream extends PrintStream {
83          public LoggingPrintStream(LoggingOutputStream out) {
84              super(out, true);
85          }
86  
87          public void forceFlush() {
88              ((LoggingOutputStream) out).forceFlush();
89          }
90      }
91  
92      public static void forceFlush(PrintStream ps) {
93          if (ps instanceof LoggingPrintStream) {
94              ((LoggingPrintStream) ps).forceFlush();
95          }
96      }
97  }