View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2021, 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.classic.testUtil;
15  
16  import java.io.PrintStream;
17  import java.util.ArrayList;
18  import java.util.Collections;
19  import java.util.List;
20  
21  /**
22   * This class is duplicated in slf4j-api-testsjar since 2.0.0-alpha4.
23   * 
24   * Use the copy in since 2.0.0-alpha4-tests.jar once it is released.
25   * 
26   * @author Ceki
27   *
28   */
29  public class StringPrintStream extends PrintStream {
30  
31      public static final String LINE_SEP = System.getProperty("line.separator");
32      PrintStream other;
33      boolean duplicate = false;
34  
35      public List<String> stringList = Collections.synchronizedList(new ArrayList<String>());
36  
37      public StringPrintStream(PrintStream ps, boolean duplicate) {
38          super(ps);
39          other = ps;
40          this.duplicate = duplicate;
41      }
42  
43      public StringPrintStream(PrintStream ps) {
44          this(ps, false);
45      }
46  
47      public void print(String s) {
48          if (duplicate)
49              other.print(s);
50          stringList.add(s);
51      }
52  
53      public void println(String s) {
54          if (duplicate)
55              other.println(s);
56          stringList.add(s);
57      }
58  
59      public void println(Object o) {
60          if (duplicate)
61              other.println(o);
62          stringList.add(o.toString());
63      }
64  }