| 1 28 29 package com.idaremedia.antx; 30 31 import org.apache.tools.ant.BuildEvent; 32 import org.apache.tools.ant.Project; 33 34 import com.idaremedia.antx.helpers.Tk; 35 import com.idaremedia.antx.parameters.EnumSkeleton; 36 37 47 48 public final class NoiseLevel extends EnumSkeleton 49 { 50 51 public static final int ERROR_INDEX= 0; 52 53 public static final int WARNING_INDEX= ERROR_INDEX+1; 54 55 public static final int INFO_INDEX= WARNING_INDEX+1; 56 57 public static final int VERBOSE_INDEX= INFO_INDEX+1; 58 59 public static final int DEBUG_INDEX= VERBOSE_INDEX+1; 60 61 public static final int FATAL_INDEX= DEBUG_INDEX+1; 62 63 64 public static final NoiseLevel FATAL = new NoiseLevel("fatal",FATAL_INDEX); 65 66 public static final NoiseLevel ERROR = new NoiseLevel("error",ERROR_INDEX); 67 68 public static final NoiseLevel WARNING = new NoiseLevel("warning",WARNING_INDEX); 69 70 public static final NoiseLevel INFO = new NoiseLevel("info",INFO_INDEX); 71 72 public static final NoiseLevel VERBOSE = new NoiseLevel("verbose",VERBOSE_INDEX); 73 74 public static final NoiseLevel DEBUG = new NoiseLevel("debug",DEBUG_INDEX); 75 76 77 78 81 public NoiseLevel() 82 { 83 super(); 84 } 85 86 87 91 private NoiseLevel(String v, int i) 92 { 93 super(v); 94 } 95 96 97 101 public String [] getValues() 102 { 103 return new String [] {"error","warning","info","verbose","debug","fatal"}; 104 }; 105 106 107 108 114 public static NoiseLevel from(int i) 115 { 116 switch(i) { 117 case ERROR_INDEX: 118 return ERROR; 119 case WARNING_INDEX: 120 return WARNING; 121 case INFO_INDEX: 122 return INFO; 123 case VERBOSE_INDEX: 124 return VERBOSE; 125 case DEBUG_INDEX: 126 return DEBUG; 127 case FATAL_INDEX: 128 return FATAL; 129 } 130 return null; 131 } 132 133 134 141 public static NoiseLevel from(int i, NoiseLevel dfltNL) 142 { 143 NoiseLevel nl= from(i); 144 return (nl==null) ? dfltNL : nl; 145 } 146 147 148 154 public static NoiseLevel from(String s) 155 { 156 if (s!=null && s.length()>1) { 157 s = Tk.lowercaseFrom(s); 158 if (Character.isDigit(s.charAt(0))) { 159 try { return from(Integer.parseInt(s)); } 160 catch(Exception nfx) {} 161 } else { 162 if ("default".equals(s)) { return getAntXDefault(); } 163 if (ERROR.value.equals(s)) { return ERROR; } 164 if (WARNING.value.equals(s)) { return WARNING; } 165 if (INFO.value.equals(s)) { return INFO; } 166 if (VERBOSE.value.equals(s)) { return VERBOSE; } 167 if (DEBUG.value.equals(s)) { return DEBUG; } 168 if (FATAL.value.equals(s)) { return FATAL; } 169 } 170 } 171 return null; 172 } 173 174 175 182 public static NoiseLevel from(String s, NoiseLevel dfltNL) 183 { 184 NoiseLevel nl= from(s); 185 return (nl==null) ? dfltNL : nl; 186 } 187 188 189 190 197 public static NoiseLevel fromNativeIndex(int msgLevel) 198 { 199 switch(msgLevel) { 200 case Project.MSG_ERR: 201 return ERROR; 202 case Project.MSG_WARN: 203 return WARNING; 204 case Project.MSG_INFO: 205 return INFO; 206 case Project.MSG_VERBOSE: 207 return VERBOSE; 208 case Project.MSG_DEBUG: 209 return DEBUG; 210 } 211 return null; 212 } 213 214 215 221 public static NoiseLevel from(BuildEvent e) 222 { 223 return fromNativeIndex(e.getPriority()); 224 } 225 226 227 231 public final int getNativeIndex() 232 { 233 int i= getIndex(); 234 if (i==FATAL_INDEX) { 235 return ERROR_INDEX; 236 } 237 return i; 238 } 239 240 241 245 public static final NoiseLevel getAntXDefault() 246 { 247 return NoiseLevel.INFO; 248 } 249 250 251 252 263 public static final NoiseLevel getDefault(final Project P) 264 { 265 String slvl=null; 266 if (P!=null) { 267 slvl = P.getProperty(AntX.DEFAULT_NOISELEVEL_PROP); 268 } 269 if (slvl==null) { 270 slvl = System.getProperty(AntX.DEFAULT_NOISELEVEL_PROP); 271 } 272 NoiseLevel nl = getAntXDefault(); 273 if (slvl!=null) { 274 nl= NoiseLevel.from(slvl,nl); 275 } 276 return nl; 277 } 278 279 280 281 289 public static final boolean isAsBadAs(NoiseLevel unk, NoiseLevel threshold) 290 { 291 return unk.getNativeIndex() <= threshold.getNativeIndex(); 292 } 293 294 295 296 304 public static final boolean isAsBadAs(int loglevel, NoiseLevel threshold) 305 { 306 return loglevel <= threshold.getNativeIndex(); 307 } 308 309 310 311 319 public final boolean isAsBadAs(NoiseLevel threshold) 320 { 321 return isAsBadAs(this,threshold); 322 } 323 } 324 325 326 | Popular Tags |