1 /* 2 * Copyright (c) 2003-2008 by Cosylab d. d. 3 * 4 * This file is part of Java-Common. 5 * 6 * Java-Common is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * Java-Common is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with Java-Common. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package com.cosylab.util; 21 22 /** 23 * This class generalizes the debug output by providing additional 24 * functionality to the System.out.println output, such as enabling, disabling 25 * debug or indenting the output.<br> 26 * All methods and fields are declared as static to simplify use.<br> 27 * Indentation can be used to perform stack traces of the methods order of execution.<br> 28 * This class is intended to duplicate the C/C++ macro IFDEF functionality. 29 * All debug routines will only be included in the compiled classes if 30 * debugging is enabled via the ENABLE_DEBUG flag. If this flag is enabled, 31 * most compilers, though not neccessary all, will simply ignore any 32 * references to the Debug class. ENABLE_DEBUG flag cannot be changed during 33 * runtime. 34 * 35 * @author <a href="mailto:ales.pucelj@cosylab.com">Ales Pucelj</a> 36 * @version $id$ 37 */ 38 public final class Debug 39 { 40 // Global flag to turn debug on or off 41 42 // Global flag to turn debug on or off 43 public static final boolean ENABLE_DEBUG = false; 44 45 // Ammount of indent 46 private static int indent = 0; 47 48 // Maximum number of predefined indents (represented as Strings) 49 private static final int MAX_INDENTS = 10; 50 51 // Indent size (default = 2 whitespaces) 52 private static final String INDENTATION = " "; 53 54 // Cache of precalculated indents 55 private static final String[] indents = new String[MAX_INDENTS]; 56 57 static { 58 if (ENABLE_DEBUG) { 59 for (int i = 0; i < MAX_INDENTS; i++) { 60 indents[i] = ""; 61 62 for (int j = 0; j < i; j++) { 63 indents[i] += INDENTATION; 64 } 65 } 66 } 67 } 68 69 /** 70 * Returns a string that consists of nINDENTSIZE whitespaces. 71 * 72 * @param n n Indent ammount. 73 * 74 * @return String Indentation string. 75 */ 76 private static final String getIndentation(int n) 77 { 78 if (ENABLE_DEBUG) { 79 String result = ""; 80 81 while (n >= MAX_INDENTS) { 82 result = result + indents[MAX_INDENTS - 1]; 83 n -= MAX_INDENTS; 84 } 85 86 return result + indents[n]; 87 } 88 89 return ""; 90 } 91 92 private static synchronized final void dump(final String s) 93 { 94 System.out.print(getIndentation(indent)); 95 System.out.println(s); 96 } 97 98 /** 99 * Outputs the debug string if debug output is enabled. 100 * 101 * @param s String to output. 102 */ 103 public static final void out(final String s) 104 { 105 if (ENABLE_DEBUG) { 106 dump(s); 107 } 108 } 109 110 /** 111 * Outputs the exception message if debug output is enabled. 112 * 113 * @param throwable Exception to display. 114 */ 115 public static final void out(final Throwable throwable) 116 { 117 if (ENABLE_DEBUG) { 118 dump(throwable.toString()); 119 } 120 } 121 122 /** 123 * Outputs the debug string and class name if debug output is enabled. 124 * Output format will be in the form of "source.getClass().getName()": "s" 125 * 126 * @param source Instance of object that generated this output. 127 * @param s String to output. 128 */ 129 public static final void out(final Object source, final String s) 130 { 131 if (ENABLE_DEBUG) { 132 dump(source+": "+s); 133 } 134 } 135 136 /** 137 * Creates indented debug output. Each call to start() method will increase 138 * the indentation by predefined ammount and each call to the end method 139 * will decrease it. 140 * 141 * @param s String to output. 142 */ 143 public static synchronized final void start(final String s) 144 { 145 if (ENABLE_DEBUG) { 146 dump(s); 147 148 indent++; 149 } 150 } 151 152 /** 153 * Creates indented debug output. Each call to start() method will increase 154 * the indentation by predefined ammount and each call to the end method 155 * will decrease it. 156 * 157 * @param s String to output. 158 */ 159 public static synchronized final void end(final String s) 160 { 161 if (ENABLE_DEBUG) { 162 indent--; 163 164 if (indent < 0) { 165 indent = 0; 166 } 167 168 dump(s); 169 } 170 } 171 172 /** 173 * Run test applet. 174 * 175 * @param args command line parameters 176 */ 177 public static void main(String[] args) 178 { 179 Debug.out("Debug has occured"); 180 181 Debug.out("Hello world", "Debug happens"); 182 183 Debug.start("So it begins"); 184 185 Debug.out("Indented statement"); 186 187 Debug.end("And so it ends"); 188 } 189 } 190 191 /* __oOo__ */