1 30 31 package jbet; 32 import java.io.*; 33 import java.util.*; 34 import java.lang.reflect.Field ; 35 36 45 46 public class Logger { 47 48 static final int ALL = 0; 50 static final int DEBUG = 10; 52 static final int INFO = 20; 53 static final int WARN = 30; 54 static final int ERROR = 40; 55 static final int FATAL = 50; 56 57 public static int str2level(String s) { 58 if (s.equalsIgnoreCase("all")) 59 return ALL; 60 if (s.equalsIgnoreCase("debug")) 61 return DEBUG; 62 if (s.equalsIgnoreCase("info")) 63 return INFO; 64 if (s.equalsIgnoreCase("warn")) 65 return WARN; 66 if (s.equalsIgnoreCase("error")) 67 return ERROR; 68 if (s.equalsIgnoreCase("fatal")) 69 return FATAL; 70 return -1; 71 } 72 73 public static String level2str (int s) 74 { 75 if (s >= FATAL) 76 return "fatal"; 77 else if (s >= ERROR) 78 return "error"; 79 else if (s >= WARN) 80 return "warn"; 81 else if (s >= INFO) 82 return "info"; 83 else if (s >= DEBUG) 84 return "debug"; 85 else 86 return "all"; 87 } 88 89 PrintStream out; int loglevel; HashSet facilities; 93 95 99 Logger(PrintStream out, int level) { 100 this.out = out; 101 loglevel = level; 102 facilities = new HashSet(); 103 } 104 105 109 public void setLevel(int l) { 110 loglevel = l; 111 } 112 113 public void addFacility (String f) { 114 facilities.add (f); 115 } 116 117 121 public void setOut(PrintStream out) { 122 this.out = out; 123 } 124 125 public void outline(String s, int lev, String facility, String cmd) { 126 if (filter (lev, facility)) { 127 StringBuffer sb = new StringBuffer (); 128 129 if (cmd != null) { 130 sb.append (cmd); 131 sb.append (":"); 132 } 133 if (facility != null) { 134 sb.append (facility); 135 sb.append (":"); 136 } 137 sb.append (" "); 138 sb.append (s); 139 out.println(sb.toString()); 140 } 141 } 142 143 static String class2facil(String cname) { 144 145 Class cr; 146 try { 147 cr = Class.forName (cname); 148 Field f = cr.getField ("JbetLogFacility"); 149 return f.get (null).toString(); 150 } 151 catch (Exception e) { 152 153 int i = cname.indexOf("$"); 154 if (i != -1) 155 return class2facil (cname.substring(0,i)); 156 else 157 return null; 158 } 159 } 160 161 static String getfacil() { 162 StackTraceElement [] st = new Exception ().getStackTrace(); 163 String cname; 164 for (int i = 0; i < st.length; i++) { 165 cname = st[i].getClassName(); 166 if (!cname.equals("jbet.Logger") && 167 !cname.startsWith("jbet.Logger$") && 168 !cname.startsWith("java.") && 169 !cname.equals("jbet.LineWriter") && 170 !cname.startsWith("jbet.LineWriter$")) { 171 172 String fac = class2facil (cname); 173 if (fac != null) 174 return fac; 175 } 176 } 177 return null; 178 } 179 180 static String getcmd() { 181 StackTraceElement [] st = new Exception ().getStackTrace(); 182 String cname; 183 184 for (int i = 0; i < st.length; i++) { 185 cname = st[i].getClassName(); 186 187 Class cr; 188 189 try { 190 cr = Class.forName (cname); 191 } catch (Exception e) { 192 continue; 193 } 194 if (Command.class.isAssignableFrom (cr)) { 195 if (cname.startsWith ("jbet.cmd.")) { 196 i = cname.indexOf("$", 9); 197 if (i != -1) 198 return cname.substring(9,i-9); 199 else 200 return cname.substring (9); 201 } 202 else 203 return cname; 204 } 205 } 206 207 return "<none>"; 208 } 209 210 public LineWriter getStream(final int level) { 211 return new LineWriter() { 212 String currfacility; 213 String currcmd; 214 215 protected void doflush() {} 216 protected void doclose() {} 217 protected void outline(String s) { 218 Logger.this.outline(s,level,currfacility,currcmd); 219 } 220 protected void dowrite(char[]buf, int off, int len) { 221 if (startofline) { 222 currfacility = Logger.getfacil(); 223 currcmd = Logger.getcmd(); 224 } 225 226 super.dowrite(buf, off, len); 227 } 228 }; 229 } 230 231 private boolean filter(int level, String fac) { 232 return level >= loglevel || facilities.contains (fac); 233 } 234 } 235 236 237 238 239 240 241 | Popular Tags |