1 package org.hibernate.eclipse; 2 3 import java.util.StringTokenizer ; 4 5 import org.eclipse.core.runtime.IStatus; 6 import org.eclipse.core.runtime.Platform; 7 import org.eclipse.core.runtime.Status; 8 import org.osgi.framework.Bundle; 9 10 17 public class EclipseLogger { 18 19 public static final int ERROR = IStatus.ERROR; public static final int ERROR_DEBUG = 200 + ERROR; 21 public static final int INFO = IStatus.INFO; public static final int INFO_DEBUG = 200 + INFO; 23 public static final int OK = IStatus.OK; public static final int OK_DEBUG = 200 + OK; 25 26 private static final String TRACEFILTER_LOCATION = "/debug/tracefilter"; public static final int WARNING = IStatus.WARNING; public static final int WARNING_DEBUG = 200 + WARNING; 29 30 private final String PLUGIN_ID; 31 private Bundle bundle; 32 33 public EclipseLogger(String pluginid) { 34 this.PLUGIN_ID = pluginid; 35 bundle = Platform.getBundle(PLUGIN_ID); 36 } 37 38 49 protected void _log(int level, String message, Throwable exception) { 50 if (level == OK_DEBUG || level == INFO_DEBUG || level == WARNING_DEBUG || level == ERROR_DEBUG) { 51 if (!isDebugging()) 52 return; 53 } 54 55 int severity = IStatus.OK; 56 switch (level) { 57 case INFO_DEBUG : 58 case INFO : 59 severity = IStatus.INFO; 60 break; 61 case WARNING_DEBUG : 62 case WARNING : 63 severity = IStatus.WARNING; 64 break; 65 case ERROR_DEBUG : 66 case ERROR : 67 severity = IStatus.ERROR; 68 } 69 Status statusObj = new Status(severity, PLUGIN_ID, severity, message, exception); 70 71 if (bundle != null) 72 Platform.getLog(bundle).log(statusObj); 73 } 74 75 84 protected void _trace(String category, String message, Throwable exception) { 85 if (!isDebugging()) 86 return; 87 88 String traceFilter = Platform.getDebugOption(PLUGIN_ID + TRACEFILTER_LOCATION); 89 if (traceFilter != null) { 90 StringTokenizer tokenizer = new StringTokenizer (traceFilter, ","); while (tokenizer.hasMoreTokens()) { 92 String cat = tokenizer.nextToken().trim(); 93 if (category.equals(cat)) { 94 Status statusObj = new Status(IStatus.OK, PLUGIN_ID, IStatus.OK, message, exception); 95 Bundle bundle = Platform.getBundle(PLUGIN_ID); 96 if (bundle != null) 97 Platform.getLog(bundle).log(statusObj); 98 return; 99 } 100 } 101 } 102 } 103 104 107 public boolean isDebugging() { 108 return Platform.inDebugMode(); 109 } 110 111 public void log(int level, String message) { 112 _log(level, message, null); 113 } 114 115 public void log(int level, String message, Throwable exception) { 116 _log(level, message, exception); 117 } 118 119 public void logException(String message, Throwable exception) { 120 _log(ERROR, message, exception); 121 } 122 123 public void logException(Throwable exception) { 124 _log(ERROR, exception.getMessage(), exception); 125 } 126 127 public void trace(String category, String message) { 128 _trace(category, message, null); 129 } 130 131 public void traceException(String category, String message, Throwable exception) { 132 _trace(category, message, exception); 133 } 134 135 public void traceException(String category, Throwable exception) { 136 _trace(category, exception.getMessage(), exception); 137 } 138 139 143 public void log(IStatus status) { 144 Platform.getLog(bundle).log(status); 145 } 146 } 147 | Popular Tags |