1
+ /**
2
+ * Copyright (c) 2004-2021 QOS.ch
3
+ * All rights reserved.
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files (the
7
+ * "Software"), to deal in the Software without restriction, including
8
+ * without limitation the rights to use, copy, modify, merge, publish,
9
+ * distribute, sublicense, and/or sell copies of the Software, and to
10
+ * permit persons to whom the Software is furnished to do so, subject to
11
+ * the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ *
24
+ */
25
+ package org .slf4j .simple .multiThreadedExecution ;
26
+
27
+ import java .io .PrintStream ;
28
+ import java .util .ArrayList ;
29
+ import java .util .Collections ;
30
+ import java .util .List ;
31
+ import java .util .regex .Pattern ;
32
+
33
+ public class StateCheckingPrintStream extends PrintStream {
34
+
35
+ enum State {
36
+ INITIAL , UNKNOWN , HELLO , THROWABLE , AT1 , AT2 , OTHER ;
37
+ }
38
+
39
+ PrintStream other ;
40
+
41
+ List <String > stringList = Collections .synchronizedList (new ArrayList <String >());
42
+
43
+ State currentState = State .INITIAL ;
44
+
45
+ public StateCheckingPrintStream (PrintStream ps ) {
46
+ super (ps );
47
+ }
48
+
49
+ public void print (String s ) {
50
+ }
51
+
52
+ public void println (String s ) {
53
+
54
+ State next = computeState (s );
55
+ //System.out.println(next + " " + s);
56
+ switch (currentState ) {
57
+ case INITIAL :
58
+ currentState = next ;
59
+ break ;
60
+
61
+ case UNKNOWN :
62
+ currentState = next ;
63
+ break ;
64
+
65
+ case OTHER :
66
+ if (next == State .UNKNOWN ) {
67
+ currentState = State .UNKNOWN ;
68
+ return ;
69
+ }
70
+
71
+ if (next != State .OTHER && next != State .HELLO ) {
72
+ throw badState (s , currentState , next );
73
+ }
74
+ currentState = next ;
75
+ break ;
76
+
77
+ case HELLO :
78
+ if (next != State .THROWABLE ) {
79
+ throw badState (s , currentState , next );
80
+ }
81
+ currentState = next ;
82
+ break ;
83
+ case THROWABLE :
84
+ if (next != State .AT1 ) {
85
+ throw badState (s , currentState , next );
86
+ }
87
+ currentState = next ;
88
+ break ;
89
+
90
+ case AT1 :
91
+ if (next != State .AT2 ) {
92
+ throw badState (s , currentState , next );
93
+ }
94
+ currentState = next ;
95
+ break ;
96
+
97
+ case AT2 :
98
+ currentState = next ;
99
+ break ;
100
+ default :
101
+ throw new IllegalStateException ("Unreachable code" );
102
+ }
103
+
104
+ stringList .add (s );
105
+ }
106
+
107
+ private IllegalStateException badState (String s , State currentState2 , State next ) {
108
+ return new IllegalStateException ("Unexpected state " + next + " for current state " + currentState2 + " for " + s );
109
+
110
+ }
111
+
112
+ String OTHER_PATTERN_STR = ".*Other \\ d{1,5}" ;
113
+ String HELLO_PATTERN_STR = ".*Hello \\ d{1,5}" ;
114
+ String THROWABLE_PATTERN_STR = "java.lang.Throwable: i=\\ d{1,5}" ;
115
+ String AT1_PATTERN_STR = "\\ s*at " + this .getClass ().getPackage ().getName () + ".*" ;
116
+ String AT2_PATTERN_STR = "\\ s*at " + ".*Thread.java.*" ;
117
+
118
+ Pattern PATTERN_OTHER = Pattern .compile (OTHER_PATTERN_STR );
119
+ Pattern PATTERN_HELLO = Pattern .compile (HELLO_PATTERN_STR );
120
+ Pattern PATTERN_THROWABLE = Pattern .compile (THROWABLE_PATTERN_STR );
121
+ Pattern PATTERN_AT1 = Pattern .compile (AT1_PATTERN_STR );
122
+ Pattern PATTERN_AT2 = Pattern .compile (AT2_PATTERN_STR );
123
+
124
+ private State computeState (String s ) {
125
+
126
+ if (PATTERN_OTHER .matcher (s ).matches ()) {
127
+ return State .OTHER ;
128
+ } else if (PATTERN_HELLO .matcher (s ).matches ()) {
129
+ return State .HELLO ;
130
+ } else if (PATTERN_THROWABLE .matcher (s ).matches ()) {
131
+ return State .THROWABLE ;
132
+ } else if (PATTERN_AT1 .matcher (s ).matches ()) {
133
+ return State .AT1 ;
134
+ } else if (PATTERN_AT2 .matcher (s ).matches ()) {
135
+ return State .AT2 ;
136
+ } else {
137
+ return State .UNKNOWN ;
138
+ }
139
+ }
140
+
141
+ public void println (Object o ) {
142
+ println (o .toString ());
143
+ }
144
+ }
0 commit comments