View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.joran.spi;
15  
16  import java.io.IOException;
17  import java.io.OutputStream;
18  
19  /**
20   * The set of console output targets.
21   * 
22   * @author Ruediger Dohna
23   * @author Ceki Gülcü
24   * @author Tom SH Liu
25   * @author David Roussel
26   * 
27   * @sse LOGBACK-136
28   */
29  public enum ConsoleTarget {
30  
31      SystemOut("System.out", new OutputStream() {
32          @Override
33          public void write(int b) throws IOException {
34              System.out.write(b);
35          }
36  
37          @Override
38          public void write(byte b[]) throws IOException {
39              System.out.write(b);
40          }
41  
42          @Override
43          public void write(byte b[], int off, int len) throws IOException {
44              System.out.write(b, off, len);
45          }
46  
47          @Override
48          public void flush() throws IOException {
49              System.out.flush();
50          }
51      }),
52  
53      SystemErr("System.err", new OutputStream() {
54          @Override
55          public void write(int b) throws IOException {
56              System.err.write(b);
57          }
58  
59          @Override
60          public void write(byte b[]) throws IOException {
61              System.err.write(b);
62          }
63  
64          @Override
65          public void write(byte b[], int off, int len) throws IOException {
66              System.err.write(b, off, len);
67          }
68  
69          @Override
70          public void flush() throws IOException {
71              System.err.flush();
72          }
73      });
74  
75      public static ConsoleTarget findByName(String name) {
76          for (ConsoleTarget target : ConsoleTarget.values()) {
77              if (target.name.equalsIgnoreCase(name)) {
78                  return target;
79              }
80          }
81          return null;
82      }
83  
84      private final String name;
85      private final OutputStream stream;
86  
87      private ConsoleTarget(String name, OutputStream stream) {
88          this.name = name;
89          this.stream = stream;
90      }
91  
92      public String getName() {
93          return name;
94      }
95  
96      public OutputStream getStream() {
97          return stream;
98      }
99  
100     @Override
101     public String toString() {
102         return name;
103     }
104 }