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
26
27
28
29
30
31
32
33
34
35
36
37
38 package com.gargoylesoftware.base.trace;
39
40 import com.gargoylesoftware.base.util.DetailedNullPointerException;
41 import java.io.PrintStream;
42
43 /***
44 * A controller object for the tracing mechanism.
45 *
46 *@version $Revision: 1.5 $
47 *@author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
48 */
49 public class TraceController {
50
51 private PrintStream systemOut_;
52 private PrintStream systemErr_;
53
54 private TraceChannel defaultChannel_ = Trace.out;
55
56
57 /***
58 * Instantiate one
59 */
60 TraceController() {
61 }
62
63
64 /***
65 * Shutdown the tracing thread and flush the buffers. Any print calls after
66 * this method has been called will be written immediately on the thread
67 * that made the call.
68 *
69 *@param enabled true if buffering is to be enabled.
70 */
71 public void setBufferingEnabled( final boolean enabled ) {
72 Trace.getDispatcher().setBufferingEnabled( enabled );
73 }
74
75
76 /***
77 * Return true if buffering is enabled.
78 *
79 *@return true if buffering is enabled.
80 */
81 public boolean isBufferingEnabled() {
82 return Trace.getDispatcher().isBufferingEnabled();
83 }
84
85
86 /***
87 * Specify whether or not System.out should be redirected to print through
88 * Trace.println
89 *
90 *@param redirected true if System.out should be redirected.
91 */
92 public synchronized void setOutRedirected( final boolean redirected ) {
93 if( redirected == isOutRedirected() ) {
94
95 return;
96 }
97
98 if( redirected ) {
99 systemOut_ = System.out;
100 System.setOut( new PrintStream( new TraceOutputStream() ) );
101 }
102 else {
103 System.setOut( systemOut_ );
104 systemOut_ = null;
105 }
106
107 }
108
109
110 /***
111 * Return true if System.out has been redirected to print through
112 * Trace.println
113 *
114 *@return true if System.out has been redirected.
115 */
116 public boolean isOutRedirected() {
117 return systemOut_ != null;
118 }
119
120
121 /***
122 * Return the real stream that corresponds to the console for System.out.
123 * If System.out has been redirected then this method will return the
124 * original value.
125 *
126 *@return the real System.out
127 */
128 public PrintStream getRealSystemOut() {
129 final PrintStream result;
130 if( systemOut_ == null ) {
131 result = System.out;
132 }
133 else {
134 result = systemOut_;
135 }
136 return result;
137 }
138
139
140 /***
141 * Specify whether or not System.err should be redirected to print through
142 * Trace.println
143 *
144 *@param redirected true if System.err should be redirected.
145 */
146 public synchronized void setErrRedirected( final boolean redirected ) {
147 if( redirected == isErrRedirected() ) {
148
149 return;
150 }
151
152 if( redirected ) {
153 systemErr_ = System.err;
154 System.setErr( new PrintStream( new TraceOutputStream() ) );
155 }
156 else {
157 System.setErr( systemErr_ );
158 systemErr_ = null;
159 }
160
161 }
162
163
164 /***
165 * Return true if System.err has been redirected to print through
166 * Trace.println
167 *
168 *@return true if System.err has been redirected.
169 */
170 public boolean isErrRedirected() {
171 return systemErr_ != null;
172 }
173
174
175 /***
176 * Return the real stream that corresponds to the console for System.err.
177 * If System.err has been redirected then this method will return the
178 * original value.
179 *
180 *@return the real System.err
181 */
182 public PrintStream getRealSystemErr() {
183 final PrintStream result;
184 if( systemErr_ == null ) {
185 result = System.err;
186 }
187 else {
188 result = systemErr_;
189 }
190 return result;
191 }
192
193
194 /***
195 * Close down the debugging facilities in preparation for application
196 * shutdown. This will disable the buffering and turn off redirections for
197 * System.out and System.err.
198 */
199 public void close() {
200 setBufferingEnabled( false );
201 setErrRedirected( false );
202 setOutRedirected( false );
203 }
204
205
206 /***
207 * Set the default channel. The default is used when a channel is not
208 * specified in a call to Trace.print(), Trace.println() or
209 * Trace.printStackTrace()
210 *
211 *@param channel the new channel.
212 */
213 public void setDefaultChannel( final TraceChannel channel ) {
214 assertNotNull( "channel", channel );
215 defaultChannel_ = channel;
216 }
217
218
219 /***
220 * Return the default channel
221 *
222 *@return the default channel.
223 */
224 public TraceChannel getDefaultChannel() {
225 return defaultChannel_;
226 }
227
228
229 /***
230 * Verify that the specified value is not null. If it is then throw an
231 * exception
232 *
233 *@param fieldName The name of the field to check
234 *@param fieldValue The value of the field to check
235 *@exception DetailedNullPointerException If fieldValue is null
236 */
237 protected final void assertNotNull( final String fieldName, final Object fieldValue )
238 throws DetailedNullPointerException {
239
240 if( fieldValue == null ) {
241 throw new DetailedNullPointerException( fieldName );
242 }
243 }
244 }
245