|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| TraceController.java | 0% | 2.8% | 15.4% | 4.8% |
|
||||||||||||||
| 1 | /* | |
| 2 | * Copyright (c) 1998, 2005 Gargoyle Software Inc. All rights reserved. | |
| 3 | * | |
| 4 | * Redistribution and use in source and binary forms, with or without | |
| 5 | * modification, are permitted provided that the following conditions are met: | |
| 6 | * | |
| 7 | * 1. Redistributions of source code must retain the above copyright notice, | |
| 8 | * this list of conditions and the following disclaimer. | |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
| 10 | * this list of conditions and the following disclaimer in the documentation | |
| 11 | * and/or other materials provided with the distribution. | |
| 12 | * 3. The end-user documentation included with the redistribution, if any, must | |
| 13 | * include the following acknowledgment: | |
| 14 | * | |
| 15 | * "This product includes software developed by Gargoyle Software Inc. | |
| 16 | * (http://www.GargoyleSoftware.com/)." | |
| 17 | * | |
| 18 | * Alternately, this acknowledgment may appear in the software itself, if | |
| 19 | * and wherever such third-party acknowledgments normally appear. | |
| 20 | * 4. The name "Gargoyle Software" must not be used to endorse or promote | |
| 21 | * products derived from this software without prior written permission. | |
| 22 | * For written permission, please contact info@GargoyleSoftware.com. | |
| 23 | * 5. Products derived from this software may not be called "GSBase", nor may | |
| 24 | * "GSBase" appear in their name, without prior written permission of | |
| 25 | * Gargoyle Software Inc. | |
| 26 | * | |
| 27 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, | |
| 28 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 29 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE | |
| 30 | * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
| 31 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 32 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, | |
| 33 | * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| 34 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| 35 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, | |
| 36 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 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 | 2 | 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 | 5 | public void setBufferingEnabled( final boolean enabled ) { |
| 72 | 5 | 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 | 0 | public boolean isBufferingEnabled() { |
| 82 | 0 | 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 | 0 | public synchronized void setOutRedirected( final boolean redirected ) { |
| 93 | 0 | if( redirected == isOutRedirected() ) { |
| 94 | // nothing has changed. return immediately | |
| 95 | 0 | return; |
| 96 | } | |
| 97 | ||
| 98 | 0 | if( redirected ) { |
| 99 | 0 | systemOut_ = System.out; |
| 100 | 0 | System.setOut( new PrintStream( new TraceOutputStream() ) ); |
| 101 | } | |
| 102 | else { | |
| 103 | 0 | System.setOut( systemOut_ ); |
| 104 | 0 | 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 | 0 | public boolean isOutRedirected() { |
| 117 | 0 | 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 | 0 | public PrintStream getRealSystemOut() { |
| 129 | 0 | final PrintStream result; |
| 130 | 0 | if( systemOut_ == null ) { |
| 131 | 0 | result = System.out; |
| 132 | } | |
| 133 | else { | |
| 134 | 0 | result = systemOut_; |
| 135 | } | |
| 136 | 0 | 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 | 0 | public synchronized void setErrRedirected( final boolean redirected ) { |
| 147 | 0 | if( redirected == isErrRedirected() ) { |
| 148 | // nothing has changed. return immediately | |
| 149 | 0 | return; |
| 150 | } | |
| 151 | ||
| 152 | 0 | if( redirected ) { |
| 153 | 0 | systemErr_ = System.err; |
| 154 | 0 | System.setErr( new PrintStream( new TraceOutputStream() ) ); |
| 155 | } | |
| 156 | else { | |
| 157 | 0 | System.setErr( systemErr_ ); |
| 158 | 0 | 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 | 0 | public boolean isErrRedirected() { |
| 171 | 0 | 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 | 0 | public PrintStream getRealSystemErr() { |
| 183 | 0 | final PrintStream result; |
| 184 | 0 | if( systemErr_ == null ) { |
| 185 | 0 | result = System.err; |
| 186 | } | |
| 187 | else { | |
| 188 | 0 | result = systemErr_; |
| 189 | } | |
| 190 | 0 | 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 | 0 | public void close() { |
| 200 | 0 | setBufferingEnabled( false ); |
| 201 | 0 | setErrRedirected( false ); |
| 202 | 0 | 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 | 0 | public void setDefaultChannel( final TraceChannel channel ) { |
| 214 | 0 | assertNotNull( "channel", channel ); |
| 215 | 0 | defaultChannel_ = channel; |
| 216 | } | |
| 217 | ||
| 218 | ||
| 219 | /** | |
| 220 | * Return the default channel | |
| 221 | * | |
| 222 | *@return the default channel. | |
| 223 | */ | |
| 224 | 0 | public TraceChannel getDefaultChannel() { |
| 225 | 0 | 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 | 0 | protected final void assertNotNull( final String fieldName, final Object fieldValue ) |
| 238 | throws DetailedNullPointerException { | |
| 239 | ||
| 240 | 0 | if( fieldValue == null ) { |
| 241 | 0 | throw new DetailedNullPointerException( fieldName ); |
| 242 | } | |
| 243 | } | |
| 244 | } | |
| 245 |
|
||||||||||