1 22 23 24 package com.mchange.v2.log; 25 26 import java.util.*; 27 28 public final class MLevel 29 { 30 public final static MLevel ALL; 31 public final static MLevel CONFIG; 32 public final static MLevel FINE; 33 public final static MLevel FINER; 34 public final static MLevel FINEST; 35 public final static MLevel INFO; 36 public final static MLevel OFF; 37 public final static MLevel SEVERE; 38 public final static MLevel WARNING; 39 40 private final static Map integersToMLevels; 41 private final static Map namesToMLevels; 42 43 public static MLevel fromIntValue(int intval) 44 { return (MLevel) integersToMLevels.get( new Integer ( intval ) ); } 45 46 public static MLevel fromSeverity(String name) 47 { return (MLevel) namesToMLevels.get( name ); } 48 49 static 50 { 51 Class lvlClass; 52 boolean jdk14api; try 54 { 55 lvlClass = Class.forName( "java.util.logging.Level" ); 56 jdk14api = true; 57 } 58 catch (ClassNotFoundException e ) 59 { 60 lvlClass = null; 61 jdk14api = false; 62 } 63 64 MLevel all; 65 MLevel config; 66 MLevel fine; 67 MLevel finer; 68 MLevel finest; 69 MLevel info; 70 MLevel off; 71 MLevel severe; 72 MLevel warning; 73 74 try 75 { 76 all = new MLevel( (jdk14api ? lvlClass.getField("ALL").get(null) : null), Integer.MIN_VALUE, "ALL" ); 78 config = new MLevel( (jdk14api ? lvlClass.getField("CONFIG").get(null) : null), 700, "CONFIG" ); 79 fine = new MLevel( (jdk14api ? lvlClass.getField("FINE").get(null) : null), 500, "FINE" ); 80 finer = new MLevel( (jdk14api ? lvlClass.getField("FINER").get(null) : null), 400, "FINER" ); 81 finest = new MLevel( (jdk14api ? lvlClass.getField("FINEST").get(null) : null), 300, "FINEST" ); 82 info = new MLevel( (jdk14api ? lvlClass.getField("INFO").get(null) : null), 800, "INFO" ); 83 off = new MLevel( (jdk14api ? lvlClass.getField("OFF").get(null) : null), Integer.MAX_VALUE, "OFF" ); 84 severe = new MLevel( (jdk14api ? lvlClass.getField("SEVERE").get(null) : null), 900, "SEVERE" ); 85 warning = new MLevel( (jdk14api ? lvlClass.getField("WARNING").get(null) : null), 1000, "WARNING" ); 86 } 87 catch ( Exception e ) 88 { 89 e.printStackTrace(); 90 throw new InternalError ("Huh? java.util.logging.Level is here, but not its expected public fields?"); 91 } 92 93 ALL = all; 94 CONFIG = config; 95 FINE = fine; 96 FINER = finer; 97 FINEST = finest; 98 INFO = info; 99 OFF = off; 100 SEVERE = severe; 101 WARNING = warning; 102 103 Map tmp = new HashMap(); 104 tmp.put( new Integer (all.intValue()), all); 105 tmp.put( new Integer (config.intValue()), config); 106 tmp.put( new Integer (fine.intValue()), fine); 107 tmp.put( new Integer (finer.intValue()), finer); 108 tmp.put( new Integer (finest.intValue()), finest); 109 tmp.put( new Integer (info.intValue()), info); 110 tmp.put( new Integer (off.intValue()), off); 111 tmp.put( new Integer (severe.intValue()), severe); 112 tmp.put( new Integer (warning.intValue()), warning); 113 114 integersToMLevels = Collections.unmodifiableMap( tmp ); 115 116 tmp = new HashMap(); 117 tmp.put( all.getSeverity(), all); 118 tmp.put( config.getSeverity(), config); 119 tmp.put( fine.getSeverity(), fine); 120 tmp.put( finer.getSeverity(), finer); 121 tmp.put( finest.getSeverity(), finest); 122 tmp.put( info.getSeverity(), info); 123 tmp.put( off.getSeverity(), off); 124 tmp.put( severe.getSeverity(), severe); 125 tmp.put( warning.getSeverity(), warning); 126 127 namesToMLevels = Collections.unmodifiableMap( tmp ); 128 } 129 130 Object level; 131 int intval; 132 String lvlstring; 133 134 public int intValue() 135 { return intval; } 136 137 public Object asJdk14Level() 138 { return level; } 139 140 public String getSeverity() 141 { return lvlstring; } 142 143 public String toString() 144 { return this.getClass().getName() + this.getLineHeader(); } 145 146 public String getLineHeader() 147 { return "[" + lvlstring + ']';} 148 149 private MLevel(Object level, int intval, String lvlstring) 150 { 151 this.level = level; 152 this.intval = intval; 153 this.lvlstring = lvlstring; 154 } 155 } | Popular Tags |