001/*
002 * Copyright (c) 2024 QOS.ch Sarl (Switzerland)
003 * All rights reserved.
004 *
005 * Permission is hereby granted, free  of charge, to any person obtaining
006 * a  copy  of this  software  and  associated  documentation files  (the
007 * "Software"), to  deal in  the Software without  restriction, including
008 * without limitation  the rights to  use, copy, modify,  merge, publish,
009 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
010 * permit persons to whom the Software  is furnished to do so, subject to
011 * the following conditions:
012 *
013 * The  above  copyright  notice  and  this permission  notice  shall  be
014 * included in all copies or substantial portions of the Software.
015 *
016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
018 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
021 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023 *
024 *
025 *
026 */
027
028package ch.qos.logback.tyler.base.util;
029
030import java.io.PrintStream;
031import java.util.ArrayList;
032import java.util.List;
033
034public class StringPrintStream extends PrintStream {
035
036    PrintStream other;
037    boolean duplicate = false;
038
039    public List<String> stringList = new ArrayList<String>();
040
041    public StringPrintStream(PrintStream ps) {
042        this(ps, false);
043    }
044
045    public StringPrintStream(PrintStream ps, boolean duplicate) {
046        super(ps);
047        other = ps;
048        this.duplicate = duplicate;
049    }
050
051    public void print(String s) {
052        if (duplicate)
053            other.print(s);
054        stringList.add(s);
055    }
056
057    public void println(String s) {
058        if (duplicate)
059            other.println(s);
060        stringList.add(s);
061    }
062
063    public void println(Object o) {
064        if (duplicate)
065            other.println(o.toString());
066        stringList.add(o.toString());
067    }
068}