1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * ident "%Z%%M% %I% %E% SMI" 23 * 24 * Copyright (c) 1999 by Sun Microsystems, Inc. 25 * All rights reserved. 26 * 27 * Debug class 28 */ 29 30 package com.sun.admin.pm.server; 31 32 import java.util.*; 33 34 /** 35 * A simple configurable debug logging class. 36 * <p> 37 * 38 * Calling member classes <b>message()</b>, <b>warning()</b>, 39 * <b>error()</b>, and <b>fatal()</b> causes a log entry to be 40 * generated if the current verbosity level is greater than or equal 41 * to the specified severity. 42 * <p> 43 * 44 * Calling <b>setDebugLevel()</b> sets the verbosity level, which is a 45 * threshold of severity below which messages will not be logged. The 46 * verbosity level can be set at any time. 47 * <p> 48 * 49 * For example, setting the verbosity level to <b>Debug.ERROR</b> 50 * means that only <b>error()</b> and <b>fatal()</b> calls will 51 * generate log entries, while setting the level to <b>WARNING</b> 52 * will log <b>warning()</b> calls as well as <b>error()</b> and 53 * <b>fatal()</b> while ignoring <b>message()</b>. 54 * <p> 55 * 56 * Setting the verbosity level to <b>ALL</b> is equivalent to setting 57 * it to <b>MESSAGE</b>; all calls are logged. The constant 58 * <b>NONE</b> suppresses logging of all calls. 59 * <p> 60 * 61 * The verbosity level can be set globally or on a class-by-class 62 * basis. Use the form of <b>setDebugLevel()</b> which takes an 63 * argument of type Object to set the level for all instances of the 64 * specified class. 65 * <p> 66 * 67 * Using the forms of <b>message()</b>, <b>warning()</b>, 68 * <b>error()</b>, and <b>fatal()</b> which accept an argument of type 69 * Object will use the verbosity value associated with the specified 70 * class. If no value has been explicitly set for the class, the 71 * global default will be used. 72 * <p> 73 * 74 * At present log messages are written only to stdout. 75 * An enhancement would be to implement an interface to the syslog facility. 76 */ 77 78 79 public class Debug { 80 81 /** 82 * Log a highest-priority message. 83 * @param String s The message to be logged. 84 */ fatal(String s)85 static public void fatal(String s) { 86 printIf(s, FATAL); 87 } 88 89 /** 90 * Log a high-priority message. 91 * @param String s The message to be logged. 92 */ error(String s)93 static public void error(String s) { 94 printIf(s, ERROR); 95 } 96 97 /** 98 * Log a medium-priority message. 99 * @param String s The message to be logged. 100 */ warning(String s)101 static public void warning(String s) { 102 printIf(s, WARNING); 103 } 104 105 /** 106 * Log a low-priority message. 107 * @param String s The message to be logged. 108 */ message(String s)109 static public void message(String s) { 110 printIf(s, MESSAGE); 111 } 112 113 114 /** 115 * Log a lowest-priority message. 116 * @param String s The message to be logged. 117 */ info(String s)118 static public void info(String s) { 119 printIf(s, INFO); 120 } 121 122 123 /** 124 * Log a highest-priority message. 125 * @param String s The message to be logged. 126 */ fatal(Object o, String s)127 static public void fatal(Object o, String s) { 128 printIf(o, s, FATAL); 129 } 130 131 /** 132 * Log a high-priority message. 133 * @param String s The message to be logged. 134 */ error(Object o, String s)135 static public void error(Object o, String s) { 136 printIf(o, s, ERROR); 137 } 138 139 /** 140 * Log a medium-priority message. 141 * @param String s The message to be logged. 142 */ warning(Object o, String s)143 static public void warning(Object o, String s) { 144 printIf(o, s, WARNING); 145 } 146 147 /** 148 * Log a low-priority message. 149 * @param String s The message to be logged. 150 */ message(Object o, String s)151 static public void message(Object o, String s) { 152 printIf(o, s, MESSAGE); 153 } 154 155 /** 156 * Log a lowest-priority message. 157 * @param String s The message to be logged. 158 */ info(Object o, String s)159 static public void info(Object o, String s) { 160 printIf(o, s, INFO); 161 } 162 163 /** 164 * Set the verbosity level to the specified severity. 165 * @param String s The message to be logged. 166 */ setDebugLevel(int lvl)167 static public void setDebugLevel(int lvl) { 168 if (lvl < ALL || lvl > NONE) 169 return; 170 171 globalDebugLevel = lvl; 172 } 173 174 /** 175 * Set the verbosity level to the specified severity. 176 * @param String s The message to be logged. 177 */ setDebugLevel(Object o, int lvl)178 static public void setDebugLevel(Object o, int lvl) { 179 if (lvl < ALL || lvl > NONE) 180 return; 181 182 classDB.put(o.getClass(), new Integer(lvl)); 183 184 /* 185 * System.out.println("Debug: class " + o.getClass().getName() + 186 * " level = " + classDB.get(o.getClass())); 187 */ 188 } 189 setDebugLevel(String classname, int lvl)190 static public void setDebugLevel(String classname, int lvl) { 191 if (lvl < ALL || lvl > NONE) 192 return; 193 194 try { 195 classDB.put(Class.forName(classname), new Integer(lvl)); 196 } catch (Exception x) { 197 System.out.println("setDebugLevel: " + x); 198 } 199 } 200 201 printIf(String s, int lvl)202 private static void printIf(String s, int lvl) { 203 if (lvl < globalDebugLevel) 204 return; 205 debugPrint(s); 206 } 207 printIf(Object o, String s, int lvl)208 private static void printIf(Object o, String s, int lvl) { 209 if (lvl < getLevelForClass(o)) 210 return; 211 debugPrint(s); 212 } 213 214 215 /* 216 * get debug level for o's class, if already there 217 * otherwise create an entry for o and set it to the global level 218 */ getLevelForClass(Object o)219 private synchronized static int getLevelForClass(Object o) { 220 int lvl = globalDebugLevel; 221 Object g; 222 if ((g = classDB.get(o.getClass())) != null) 223 lvl = ((Integer) g).intValue(); 224 else 225 classDB.put(o.getClass(), new Integer(lvl)); 226 227 /* 228 * System.out.println("Debug: getLevelForClass " + 229 * o.getClass().getName() + 230 * " = " + lvl); 231 */ 232 233 return lvl; 234 } 235 236 // here is where we could hide syslog or file destination... debugPrint(String s)237 private static void debugPrint(String s) { 238 System.out.println(s); // for now 239 } 240 241 Object theInstance = null; 242 Debug(Object o)243 public Debug(Object o) { 244 theInstance = o; 245 } 246 SetDebugLevel(int lvl)247 public void SetDebugLevel(int lvl) { 248 if (lvl < ALL || lvl > NONE) 249 return; 250 setDebugLevel(theInstance, lvl); 251 } 252 Fatal(String s)253 public void Fatal(String s) { 254 fatal(theInstance, s); 255 } 256 Warning(String s)257 public void Warning(String s) { 258 warning(theInstance, s); 259 } 260 Error(String s)261 public void Error(String s) { 262 error(theInstance, s); 263 } 264 Message(String s)265 public void Message(String s) { 266 message(theInstance, s); 267 } 268 Info(String s)269 public void Info(String s) { 270 info(theInstance, s); 271 } 272 273 /* 274 * Verbosity level to suppress all messages. 275 */ 276 static public final int NONE = 6; 277 278 /* 279 * Verbosity level to log only highest-priority messages. 280 */ 281 static public final int FATAL = 5; 282 283 /* 284 * Verbosity level to log high- and highest-priority messages. 285 */ 286 static public final int ERROR = 4; 287 288 /* 289 * Verbosity level to log medium-, high-, and highest-priority messages. 290 */ 291 static public final int WARNING = 3; 292 293 /* 294 * Verbosity level to log low-, medium-, high-, and 295 * highest-priority messages. 296 */ 297 static public final int MESSAGE = 2; 298 299 /* 300 * Verbosity level to log lowest-, low-, medium-, high-, and 301 * highest-priority messages. 302 */ 303 static public final int INFO = 1; 304 305 306 /* 307 * Verbosity level to log all messages. 308 */ 309 static public final int ALL = 0; 310 311 private static int globalDebugLevel = FATAL; 312 private static Hashtable classDB = new Hashtable(); 313 314 } 315