| 1 package org.grlea.log; 2 3 import java.util.Collections ; 4 import java.util.HashMap ; 5 import java.util.Map ; 6 7 10 22 31 public final class 32 DebugLevel 33 { 34 38 public static final DebugLevel L1_FATAL = new DebugLevel(1, "Fatal"); 39 40 44 public static final DebugLevel L2_ERROR = new DebugLevel(2, "Error"); 45 46 50 public static final DebugLevel L3_WARN = new DebugLevel(3, "Warn"); 51 52 55 public static final DebugLevel L4_INFO = new DebugLevel(4, "Info"); 56 57 62 public static final DebugLevel L5_DEBUG = new DebugLevel(5, "Debug"); 63 64 72 public static final DebugLevel L6_VERBOSE = new DebugLevel(6, "Verbose"); 73 74 78 public static final DebugLevel L7_LUDICROUS = new DebugLevel(7, "Ludicrous"); 79 80 84 static final DebugLevel FAKE_TRACE = new DebugLevel(Integer.MAX_VALUE, "Trace"); 85 86 90 private static final Map levelsByName; 91 92 static 93 { 94 HashMap levels = new HashMap (10, 0.8F); 95 levels.put("fatal", L1_FATAL); 96 levels.put("error", L2_ERROR); 97 levels.put("warn", L3_WARN); 98 levels.put("info", L4_INFO); 99 levels.put("debug", L5_DEBUG); 100 levels.put("verbose", L6_VERBOSE); 101 levels.put("ludicrous", L7_LUDICROUS); 102 103 levelsByName = Collections.unmodifiableMap(levels); 104 } 105 106 109 private final int level; 110 111 114 private final String name; 115 116 124 private 125 DebugLevel(int level, String name) 126 { 127 this.level = level; 128 this.name = name; 129 } 130 131 140 boolean 141 shouldLog(DebugLevel logLevel) 142 { 143 return logLevel.level <= this.level; 144 } 145 146 154 public static DebugLevel 155 fromInt(int level) 156 { 157 switch (level) 158 { 159 case 1: return L1_FATAL; 160 case 2: return L2_ERROR; 161 case 3: return L3_WARN; 162 case 4: return L4_INFO; 163 case 5: return L5_DEBUG; 164 case 6: return L6_VERBOSE; 165 case 7: return L7_LUDICROUS; 166 167 default: 168 if (level < 1) 169 return L1_FATAL; 170 else 171 return L7_LUDICROUS; 172 } 173 } 174 175 188 static DebugLevel 189 fromName(String name) 190 throws IllegalArgumentException  191 { 192 if (name == null) 193 throw new IllegalArgumentException ("name cannot be null."); 194 195 DebugLevel level = (DebugLevel) levelsByName.get(name.toLowerCase()); 196 if (level == null) 197 throw new IllegalArgumentException ("Unrecognised DebugLevel name: '" + name + "'"); 198 199 return level; 200 } 201 202 205 public int 206 hashCode() 207 { 208 return level; 209 } 210 211 215 public boolean 216 equals(Object o) 217 { 218 if (o == this) 219 return true; 220 221 if (o == null || !(o.getClass().equals(this.getClass()))) 222 return false; 223 224 return this.level == ((DebugLevel) o).level; 225 } 226 227 230 public String  231 toString() 232 { 233 return name; 234 } 235 } | Popular Tags |