001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.joran.spi;
015
016import java.io.IOException;
017import java.io.OutputStream;
018
019/**
020 * The set of console output targets.
021 * 
022 * @author Ruediger Dohna
023 * @author Ceki Gülcü
024 * @author Tom SH Liu
025 * @author David Roussel
026 * 
027 * @sse LOGBACK-136
028 */
029public enum ConsoleTarget {
030
031    SystemOut("System.out", new OutputStream() {
032        @Override
033        public void write(int b) throws IOException {
034            System.out.write(b);
035        }
036
037        @Override
038        public void write(byte b[]) throws IOException {
039            System.out.write(b);
040        }
041
042        @Override
043        public void write(byte b[], int off, int len) throws IOException {
044            System.out.write(b, off, len);
045        }
046
047        @Override
048        public void flush() throws IOException {
049            System.out.flush();
050        }
051    }),
052
053    SystemErr("System.err", new OutputStream() {
054        @Override
055        public void write(int b) throws IOException {
056            System.err.write(b);
057        }
058
059        @Override
060        public void write(byte b[]) throws IOException {
061            System.err.write(b);
062        }
063
064        @Override
065        public void write(byte b[], int off, int len) throws IOException {
066            System.err.write(b, off, len);
067        }
068
069        @Override
070        public void flush() throws IOException {
071            System.err.flush();
072        }
073    });
074
075    public static ConsoleTarget findByName(String name) {
076        for (ConsoleTarget target : ConsoleTarget.values()) {
077            if (target.name.equalsIgnoreCase(name)) {
078                return target;
079            }
080        }
081        return null;
082    }
083
084    private final String name;
085    private final OutputStream stream;
086
087    private ConsoleTarget(String name, OutputStream stream) {
088        this.name = name;
089        this.stream = stream;
090    }
091
092    public String getName() {
093        return name;
094    }
095
096    public OutputStream getStream() {
097        return stream;
098    }
099
100    @Override
101    public String toString() {
102        return name;
103    }
104}