1 17 18 package org.objectweb.jac.util; 19 20 import java.io.FileOutputStream ; 21 import java.io.PrintStream ; 22 import java.text.DateFormat ; 23 import java.text.SimpleDateFormat ; 24 import java.util.Date ; 25 import java.util.Hashtable ; 26 import java.util.Map ; 27 import java.util.Set ; 28 29 40 41 public final class Log{ 42 43 static Hashtable levels = new Hashtable (); 45 46 static PrintStream out = System.out; 47 48 static String logHeader = ""; 49 50 static Date date = new Date (); 51 static DateFormat dateFormat = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss.SSS "); 52 53 59 public static void setFileName(String name, String header) { 60 try { 61 logHeader = header; 62 out = new PrintStream (new FileOutputStream (name)); 63 } catch(Exception e) { 64 out = System.out; 65 e.printStackTrace(); 66 } 67 } 68 69 76 77 public static void trace(String category, int level, String message) { 78 if (!(category!=null && levels.containsKey(category))) { 79 levels.put(category, new Integer (0)); 80 } 81 if (level<=((Integer )levels.get(category)).intValue()) { 82 date.setTime(System.currentTimeMillis()); 83 out.print(dateFormat.format(date)); 84 out.print(logHeader); 85 out.print(category); 86 out.print(": "); 87 out.println(message); 88 } 89 } 90 91 98 99 public static void trace(String category, int level, Throwable exception) { 100 if (category!=null && levels.containsKey(category)) { 101 if (level<=((Integer )levels.get(category)).intValue()) { 102 out.println(logHeader+category+": StackTrace"); 103 exception.printStackTrace(out); 104 } 105 } else { 106 levels.put(category, new Integer (0)); 107 } 108 } 109 110 111 117 public static void stack(String category, int level) { 118 if (category!=null && levels.containsKey(category)) { 119 if (level<=((Integer )levels.get(category)).intValue()) { 120 out.println(logHeader+category+": StackTrace"); 121 new Exception ().printStackTrace(out); 122 } 123 } else { 124 levels.put(category, new Integer (0)); 125 } 126 } 127 128 public static void stack(String category) { 129 stack(category,1); 130 } 131 132 138 139 public static void trace(String category, String message) { 140 Log.trace(category, 1, message); 141 } 142 143 149 150 public static void trace(String category, Throwable exception) { 151 Log.trace(category, 1, exception); 152 } 153 154 158 159 public static void error(String message) { 160 out.println(logHeader+"ERROR: "+message); 161 } 162 163 169 public static void warning(String message, int level) { 170 if (level<=1) { 171 out.println(logHeader+"WARNING: "+message); 172 } 173 } 174 175 180 public static void warning(String message) { 181 warning(message, 1); 182 } 183 184 190 public static void warning(String category, String message) { 191 warning(category+": "+message, 1); 192 } 193 194 200 public static void warning(String category, int level, String message) { 201 warning(category+": "+message, level); 202 } 203 204 211 212 public static void setLevel(String category, int level) { 213 levels.put(category, new Integer (level)); 214 } 215 216 219 public static Map getLevels() { 220 return levels; 221 } 222 223 public static Set getCategories(Object substance) { 224 return levels.keySet(); 225 } 226 227 public static String dump() { 228 return Strings.hex(levels)+" : "+levels; 229 } 230 } 231 | Popular Tags |