1 7 8 package com.sun.jmx.trace; 9 10 import java.io.FileOutputStream ; 11 import java.io.IOException ; 12 import java.io.PrintWriter ; 13 14 32 public class TraceImplementation implements TraceDestination 33 { 34 private PrintWriter out; 37 38 private int level; 41 42 static TraceImplementation newDestination(int level) 43 { 44 try { 45 final TraceImplementation impl = new TraceImplementation(); 46 impl.level = level; 47 return impl; 48 } catch (IOException x) { 49 return null; 50 } 51 } 52 53 62 public static void init() throws IOException 63 { 64 Trace.setDestination(new TraceImplementation()); 65 } 66 67 77 public static void init(int level) throws IOException 78 { 79 final TraceImplementation impl = new TraceImplementation(); 80 impl.level = level; 81 Trace.setDestination(impl); 82 } 83 84 90 public TraceImplementation() throws IOException 91 { 92 String filename; 93 if ((filename = System.getProperty("com.sun.jmx.trace.file")) != null) 94 { 95 this.out = new PrintWriter (new FileOutputStream (filename), true); 98 } 99 else 100 { 101 this.out = new PrintWriter (System.err, true); 104 } 105 106 String level; 107 if ((level = System.getProperty("com.sun.jmx.trace.level")) != null) 108 { 109 if (level.equals("DEBUG")) 112 { 113 this.level = TraceTags.LEVEL_DEBUG; 114 } 115 else if (level.equals("TRACE")) 116 { 117 this.level = TraceTags.LEVEL_TRACE; 118 } 119 else 120 { 121 this.level = TraceTags.LEVEL_ERROR; 122 } 123 } 124 else 125 { 126 this.level = TraceTags.LEVEL_ERROR; 129 } 130 } 131 132 139 public boolean isSelected(int level, int type) 140 { 141 return (level <= this.level); 142 } 143 144 147 public boolean send(int level, 148 int type, 149 String className, 150 String methodName, 151 String info) 152 { 153 if (isSelected(level, type)) 154 { 155 out.println(((className!=null)?"Class: " + className:"")+ 156 ((methodName!=null)?"\nMethod: " + methodName:"") + 157 "\n\tlevel: " + getLevel(level) + 158 "\n\ttype: " + getType(type) + 159 "\n\tmessage: " + info); 160 return true; 162 } 163 return false; 164 } 165 166 169 public boolean send(int level, 170 int type, 171 String className, 172 String methodName, 173 Throwable exception) 174 { 175 final boolean result = send(level, type, className, methodName, 176 exception.toString()); 177 if (result) 178 exception.printStackTrace(out); 179 180 return result; 181 } 182 183 188 public void reset() throws IOException 189 { 190 191 } 192 193 197 private static String getType(int type) { 198 199 switch (type) { 200 201 case TraceTags.INFO_MBEANSERVER: 202 return "INFO_MBEANSERVER"; 203 204 case TraceTags.INFO_ADAPTOR_SNMP: 205 return "INFO_ADAPTOR_SNMP"; 206 207 case TraceTags.INFO_SNMP: 208 return "INFO_SNMP"; 209 210 case TraceTags.INFO_MLET: 211 return "INFO_MLET"; 212 213 case TraceTags.INFO_MONITOR: 214 return "INFO_MONITOR"; 215 216 case TraceTags.INFO_TIMER: 217 return "INFO_TIMER"; 218 219 case TraceTags.INFO_MISC: 220 return "INFO_MISC"; 221 222 case TraceTags.INFO_NOTIFICATION: 223 return "INFO_NOTIFICATION"; 224 225 case TraceTags.INFO_RELATION: 226 return "INFO_RELATION"; 227 228 case TraceTags.INFO_MODELMBEAN: 229 return "INFO_MODELMBEAN"; 230 231 default: 232 return "UNKNOWN_TRACE_TYPE"; 233 } 234 } 235 236 240 private static String getLevel(int level) { 241 242 switch (level) { 243 244 case TraceTags.LEVEL_ERROR: 245 return "LEVEL_ERROR"; 246 247 case TraceTags.LEVEL_TRACE: 248 return "LEVEL_TRACE"; 249 250 case TraceTags.LEVEL_DEBUG: 251 return "LEVEL_DEBUG"; 252 253 default : 254 return "UNKNOWN_TRACE_LEVEL"; 255 } 256 } 257 } 258 | Popular Tags |