1 5 package de.uka.ipd.coverage.utils; 6 7 import org.eclipse.core.runtime.IStatus; 8 import org.eclipse.core.runtime.Status; 9 10 import de.uka.ipd.coverage.CoverageConfiguration; 11 import de.uka.ipd.coverage.plugin.CoveragePlugin; 12 13 16 public class Logger { 17 18 private static final int DEBUG = 0; 19 private static final int INFO = 5; 20 private static final int WARNING = 10; 21 private static final int ERROR = 15; 22 23 private static Logger instance; 24 25 30 private int fileLogLevel = Integer.MIN_VALUE; 31 private String prefix; 32 33 34 public static Logger getInstance() { 35 if (instance == null) { 36 instance = new Logger(null); 37 } 38 return instance; 39 } 40 41 public Logger(Object clazz) { 42 if (clazz != null) { 43 this.prefix = "[" + clazz.getClass().getName() + "] : "; } else { 45 this.prefix = "[ANONYMOUS] : "; } 47 } 48 49 private int getFileLogLevel() { 50 if (fileLogLevel < DEBUG) { 51 fileLogLevel = CoverageConfiguration.getDefaultLogLevel(); 52 } 53 return fileLogLevel; 54 } 55 56 public void debug(String message) { 57 if (getFileLogLevel() <= DEBUG) { 58 CoveragePlugin.getDefault().getLog().log( 59 new LogStatus(IStatus.OK, constructOutput(message))); 60 } 61 } 62 63 public void info(String message) { 64 if (getFileLogLevel() <= INFO) { 65 CoveragePlugin.getDefault().getLog().log( 66 new LogStatus(IStatus.INFO, constructOutput(message))); 67 } 68 } 69 70 public void warning(String message) { 71 if (getFileLogLevel() <= WARNING) { 72 CoveragePlugin.getDefault().getLog().log( 73 new LogStatus(IStatus.WARNING, constructOutput(message))); 74 } 75 } 76 77 public void log(Exception e) { 78 CoveragePlugin.getDefault().getLog().log( 79 new Status(IStatus.ERROR, CoveragePlugin.PLUGIN_ID, 80 IStatus.OK, e.getMessage(), e)); 81 } 82 83 public void error(String message) { 84 if (getFileLogLevel() <= ERROR) { 85 CoveragePlugin.getDefault().getLog().log( 86 new LogStatus(IStatus.ERROR, constructOutput(message))); 87 } 88 } 89 90 private String constructOutput(String message) { 91 return prefix + message; 92 } 93 94 95 private class LogStatus extends Status { 96 public LogStatus(int severity, String message) { 97 super(severity, CoveragePlugin.PLUGIN_ID, IStatus.OK, message, null); 98 } 99 } 100 101 public void fatal(String message) { 102 CoveragePlugin.getDefault().getLog().log( 103 new LogStatus(IStatus.CANCEL, constructOutput(message))); 104 } 105 } 106 107 108 | Popular Tags |