View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, 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 Ceki Gülcü
23   * @author Tom SH Liu
24   * @author Ruediger Dohna
25   * @author David Roussel
26   */
27  public enum ConsoleTarget {
28  
29    SystemOut("System.out", new OutputStream() {
30      @Override
31      public void write(int b) throws IOException {
32        System.out.write(b);
33      }
34      @Override
35      public void write(byte b[]) throws IOException {
36        System.out.write(b);
37      }
38      @Override
39      public void write(byte b[], int off, int len) throws IOException {
40        System.out.write(b, off, len);
41      }
42      @Override
43      public void flush() throws IOException {
44        System.out.flush();
45      }
46    }),
47  
48    SystemErr("System.err", new OutputStream() {
49      @Override
50      public void write(int b) throws IOException {
51        System.err.write(b);
52      }
53      @Override
54      public void write(byte b[]) throws IOException {
55        System.err.write(b);
56      }
57      @Override
58      public void write(byte b[], int off, int len) throws IOException {
59        System.err.write(b, off, len);
60      }
61      @Override
62      public void flush() throws IOException {
63        System.err.flush();
64      }
65    });
66  
67    public static ConsoleTarget findByName(String name) {
68      for (ConsoleTarget target : ConsoleTarget.values()) {
69        if (target.name.equalsIgnoreCase(name)) {
70          return target;
71        }
72      }
73      return null;
74    }
75  
76    private final String name;
77    private final OutputStream stream;
78  
79    private ConsoleTarget(String name, OutputStream stream) {
80      this.name = name;
81      this.stream = stream;
82    }
83  
84    public String getName() {
85      return name;
86    }
87  
88    public OutputStream getStream() {
89      return stream;
90    }
91  }