aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/util/io/OutErr.java
blob: c0988e60cdb2617db905f49d2ef231a972c1e332 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.lib.util.io;

import java.io.Closeable;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;

/**
 * A pair of output streams to be used for redirecting the output and error
 * streams of a subprocess.
 */
public class OutErr implements Closeable {

  private final OutputStream out;
  private final OutputStream err;

  public static final OutErr SYSTEM_OUT_ERR = create(System.out, System.err);

  /**
   * Creates a new OutErr instance from the specified output and error streams.
   */
  public static OutErr create(OutputStream out, OutputStream err) {
    return new OutErr(out, err);
  }

  protected OutErr(OutputStream out, OutputStream err) {
    this.out = out;
    this.err = err;
  }

  @Override
  public void close() throws IOException {
    out.close();
    if (out != err) {
      err.close();
    }
  }

  /**
   * This method redirects {@link System#out} / {@link System#err} into
   * {@code this} object. After calling this method, writing to
   * {@link System#out} or {@link System#err} will result in
   * {@code "System.out: " + message} or {@code "System.err: " + message}
   * being written to the OutputStreams of {@code this} instance.
   *
   * Note: This method affects global variables.
   */
  public void addSystemOutErrAsSource() {
    System.setOut(new PrintStream(new LinePrefixingOutputStream("System.out: ", getOutputStream()),
                                  /*autoflush=*/false));
    System.setErr(new PrintStream(new LinePrefixingOutputStream("System.err: ", getErrorStream()),
                                  /*autoflush=*/false));
  }

  /**
   * Creates a new OutErr instance from the specified stream.
   * Writes to either the output or err of the new OutErr are written
   * to outputStream, synchronized.
   */
  public static OutErr createSynchronizedFunnel(final OutputStream outputStream) {
    OutputStream syncOut = new OutputStream() {

      @Override
      public synchronized void write(int b) throws IOException {
        outputStream.write(b);
      }

      @Override
      public synchronized void write(byte b[]) throws IOException {
        outputStream.write(b);
      }

      @Override
      public synchronized  void write(byte b[], int off, int len) throws IOException {
        outputStream.write(b, off, len);
      }

      @Override
      public synchronized void flush() throws IOException {
        outputStream.flush();
      }

      @Override
      public synchronized void close() throws IOException {
        outputStream.close();
      }
    };

    return create(syncOut, syncOut);
  }

  public OutputStream getOutputStream() {
    return out;
  }

  public OutputStream getErrorStream() {
    return err;
  }

  /**
   * Writes the specified string to the output stream, and flushes.
   */
  public void printOut(String s) {
    PrintWriter writer = new PrintWriter(out, true);
    writer.print(s);
    writer.flush();
  }

  public void printOutLn(String s) {
    printOut(s + "\n");
  }

  /**
   * Writes the specified string to the error stream, and flushes.
   */
  public void printErr(String s) {
    PrintWriter writer = new PrintWriter(err, true);
    writer.print(s);
    writer.flush();
  }

  public void printErrLn(String s) {
    printErr(s + "\n");
  }

}