KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > eclipse > EclipseLogger


1 package org.hibernate.eclipse;
2
3 import java.util.StringTokenizer JavaDoc;
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 /**
11  * Non static implementation of the Logger in WST.
12  *
13  * Small convenience class to log messages to plugin's log file and also, if
14  * desired, the console. This class should only be used by classes in this
15  * plugin. Other plugins should make their own copy, with appropriate ID.
16  */

17 public class EclipseLogger {
18     
19     public static final int ERROR = IStatus.ERROR; // 4
20
public static final int ERROR_DEBUG = 200 + ERROR;
21     public static final int INFO = IStatus.INFO; // 1
22
public static final int INFO_DEBUG = 200 + INFO;
23     public static final int OK = IStatus.OK; // 0
24
public static final int OK_DEBUG = 200 + OK;
25
26     private static final String JavaDoc TRACEFILTER_LOCATION = "/debug/tracefilter"; //$NON-NLS-1$
27
public static final int WARNING = IStatus.WARNING; // 2
28
public static final int WARNING_DEBUG = 200 + WARNING;
29
30     private final String JavaDoc PLUGIN_ID;
31     private Bundle bundle;
32     
33     public EclipseLogger(String JavaDoc pluginid) {
34         this.PLUGIN_ID = pluginid;
35         bundle = Platform.getBundle(PLUGIN_ID);
36     }
37
38     /**
39      * Adds message to log.
40      *
41      * @param level
42      * severity level of the message (OK, INFO, WARNING, ERROR,
43      * OK_DEBUG, INFO_DEBUG, WARNING_DEBUG, ERROR_DEBUG)
44      * @param message
45      * text to add to the log
46      * @param exception
47      * exception thrown
48      */

49     protected void _log(int level, String JavaDoc message, Throwable JavaDoc 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     /**
76      * Prints message to log if category matches /debug/tracefilter option.
77      *
78      * @param message
79      * text to print
80      * @param category
81      * category of the message, to be compared with
82      * /debug/tracefilter
83      */

84     protected void _trace(String JavaDoc category, String JavaDoc message, Throwable JavaDoc exception) {
85         if (!isDebugging())
86             return;
87
88         String JavaDoc traceFilter = Platform.getDebugOption(PLUGIN_ID + TRACEFILTER_LOCATION);
89         if (traceFilter != null) {
90             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(traceFilter, ","); //$NON-NLS-1$
91
while (tokenizer.hasMoreTokens()) {
92                 String JavaDoc 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     /**
105      * @return true if the platform is debugging
106      */

107     public boolean isDebugging() {
108         return Platform.inDebugMode();
109     }
110
111     public void log(int level, String JavaDoc message) {
112         _log(level, message, null);
113     }
114
115     public void log(int level, String JavaDoc message, Throwable JavaDoc exception) {
116         _log(level, message, exception);
117     }
118
119     public void logException(String JavaDoc message, Throwable JavaDoc exception) {
120         _log(ERROR, message, exception);
121     }
122
123     public void logException(Throwable JavaDoc exception) {
124         _log(ERROR, exception.getMessage(), exception);
125     }
126
127     public void trace(String JavaDoc category, String JavaDoc message) {
128         _trace(category, message, null);
129     }
130
131     public void traceException(String JavaDoc category, String JavaDoc message, Throwable JavaDoc exception) {
132         _trace(category, message, exception);
133     }
134
135     public void traceException(String JavaDoc category, Throwable JavaDoc exception) {
136         _trace(category, exception.getMessage(), exception);
137     }
138
139     /**
140      * Only for completeness - please use the more specific log/trace methods.
141      * @param status
142      */

143     public void log(IStatus status) {
144         Platform.getLog(bundle).log(status);
145     }
146 }
147
Popular Tags