001/**
002 * Copyright (c) 2004-2021 QOS.ch
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 */
025package org.slf4j.helpers;
026
027import java.io.PrintStream;
028import java.util.ArrayList;
029import java.util.Collections;
030import java.util.List;
031
032/**
033 * 
034 *  Copied from org.slfj.helpers. 
035 *  
036 *  Currently it is not possible to use test-jar from tests running on the module-path.
037 * 
038 * @author ceki
039 *
040 */
041public class StringPrintStream extends PrintStream {
042
043    public static final String LINE_SEP = System.getProperty("line.separator");
044    PrintStream other;
045    boolean duplicate = false;
046
047    public List<String> stringList = Collections.synchronizedList(new ArrayList<>());
048
049    public StringPrintStream(PrintStream ps, boolean duplicate) {
050        super(ps);
051        other = ps;
052        this.duplicate = duplicate;
053    }
054
055    public StringPrintStream(PrintStream ps) {
056        this(ps, false);
057    }
058
059    public void print(String s) {
060        if (duplicate)
061            other.print(s);
062        stringList.add(s);
063    }
064
065    public void println(String s) {
066        if (duplicate)
067            other.println(s);
068        stringList.add(s);
069    }
070
071    public void println(Object o) {
072        if (duplicate)
073            other.println(o);
074        stringList.add(o.toString());
075    }
076}