1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.slf4j.helpers;
26
27 import java.io.PrintStream;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
31
32 import org.junit.After;
33 import org.junit.Before;
34 import org.slf4j.LoggerFactoryFriend;
35 import org.slf4j.impl.SimpleLogger;
36
37 public class SimpleLoggerMultithreadedInitializationTest extends MultithreadedInitializationTest {
38
39
40
41
42
43
44
45 static int NUM_LINES_IN_SLF4J_REPLAY_WARNING = 3;
46 private final PrintStream oldErr = System.err;
47 final String loggerName = this.getClass().getName();
48 StringPrintStream sps = new StringPrintStream(oldErr, true);
49
50 @Before
51 public void setup() {
52 System.out.println("THREAD_COUNT=" + THREAD_COUNT);
53 System.setErr(sps);
54 System.setProperty(SimpleLogger.LOG_FILE_KEY, "System.err");
55 LoggerFactoryFriend.reset();
56 }
57
58 @After
59 public void tearDown() throws Exception {
60 LoggerFactoryFriend.reset();
61 System.clearProperty(SimpleLogger.LOG_FILE_KEY);
62 System.setErr(oldErr);
63 }
64
65 @Override
66 protected long getRecordedEventCount() {
67 return sps.stringList.size();
68 };
69
70 @Override
71 protected int extraLogEvents() {
72 return NUM_LINES_IN_SLF4J_REPLAY_WARNING;
73 }
74
75 static class StringPrintStream extends PrintStream {
76
77 public static final String LINE_SEP = System.getProperty("line.separator");
78 PrintStream other;
79 boolean duplicate = false;
80
81 List<String> stringList = Collections.synchronizedList(new ArrayList<String>());
82
83 public StringPrintStream(PrintStream ps, boolean duplicate) {
84 super(ps);
85 other = ps;
86 this.duplicate = duplicate;
87 }
88
89 public StringPrintStream(PrintStream ps) {
90 this(ps, false);
91 }
92
93 public void print(String s) {
94 if (duplicate)
95 other.print(s);
96 stringList.add(s);
97 }
98
99 public void println(String s) {
100 if (duplicate)
101 other.println(s);
102 stringList.add(s);
103 }
104
105 public void println(Object o) {
106 if (duplicate)
107 other.println(o);
108 stringList.add(o.toString());
109 }
110 }
111
112 }