blob: dfb1edc0333f5f12804af058e3eba2d5846a37d5 [file] [log] [blame]
david_williams425ffe72004-12-07 21:46:39 +00001/*******************************************************************************
2 * Copyright (c) 2001, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Jens Lukowski/Innoopract - initial renaming/restructuring
11 *
12 *******************************************************************************/
13package org.eclipse.wst.sse.ui.internal;
14
15
16
17import java.util.StringTokenizer;
18
19import org.eclipse.core.runtime.IStatus;
20import org.eclipse.core.runtime.Platform;
21import org.eclipse.core.runtime.Status;
22import org.osgi.framework.Bundle;
23
24/**
25 * Small convenience class to log messages to plugin's log file and also, if
26 * desired, the console. This class should only be used by classes in this
27 * plugin. Other plugins should make their own copy, with appropriate ID.
28 */
29public class Logger {
30 private static final String PLUGIN_ID = "org.eclipse.wst.sse.ui"; //$NON-NLS-1$
31
32 public static final int ERROR = IStatus.ERROR; // 4
33 public static final int ERROR_DEBUG = 200 + ERROR;
34 public static final int INFO = IStatus.INFO; // 1
35 public static final int INFO_DEBUG = 200 + INFO;
36
37 public static final int OK = IStatus.OK; // 0
38
39 public static final int OK_DEBUG = 200 + OK;
40
41 private static final String TRACEFILTER_LOCATION = "/debug/tracefilter"; //$NON-NLS-1$
42 public static final int WARNING = IStatus.WARNING; // 2
43 public static final int WARNING_DEBUG = 200 + WARNING;
44
45 /**
46 * Adds message to log.
47 *
48 * @param level
49 * severity level of the message (OK, INFO, WARNING, ERROR,
50 * OK_DEBUG, INFO_DEBUG, WARNING_DEBUG, ERROR_DEBUG)
51 * @param message
52 * text to add to the log
53 * @param exception
54 * exception thrown
55 */
56 protected static void _log(int level, String message, Throwable exception) {
57 if (level == OK_DEBUG || level == INFO_DEBUG || level == WARNING_DEBUG || level == ERROR_DEBUG) {
58 if (!isDebugging())
59 return;
60 }
61
62 int severity = IStatus.OK;
63 switch (level) {
64 case INFO_DEBUG :
65 case INFO :
66 severity = IStatus.INFO;
67 break;
68 case WARNING_DEBUG :
69 case WARNING :
70 severity = IStatus.WARNING;
71 break;
72 case ERROR_DEBUG :
73 case ERROR :
74 severity = IStatus.ERROR;
75 }
76 message = (message != null) ? message : "null"; //$NON-NLS-1$
77 Status statusObj = new Status(severity, PLUGIN_ID, severity, message, exception);
78 Bundle bundle = Platform.getBundle(PLUGIN_ID);
79 if (bundle != null)
80 Platform.getLog(bundle).log(statusObj);
81
82 }
83
84 /**
85 * Prints message to log if category matches /debug/tracefilter option.
86 *
87 * @param message
88 * text to print
89 * @param category
90 * category of the message, to be compared with
91 * /debug/tracefilter
92 */
93 protected static void _trace(String category, String message, Throwable exception) {
94 if (isTracing(category)) {
95 message = (message != null) ? message : "null"; //$NON-NLS-1$
96 Status statusObj = new Status(IStatus.OK, PLUGIN_ID, IStatus.OK, message, exception);
97 Bundle bundle = Platform.getBundle(PLUGIN_ID);
98 if (bundle != null)
99 Platform.getLog(bundle).log(statusObj);
100
101 }
102 }
103
104 /**
105 * @return true if the platform is debugging
106 */
107 public static boolean isDebugging() {
108 return Platform.inDebugMode();
109 }
110
111 /**
112 * Determines if currently tracing a category
113 *
114 * @param category
115 * @return true if tracing category, false otherwise
116 */
117 public static boolean isTracing(String category) {
118 if (!isDebugging())
119 return false;
120
121 String traceFilter = Platform.getDebugOption(PLUGIN_ID + TRACEFILTER_LOCATION);
122 if (traceFilter != null) {
123 StringTokenizer tokenizer = new StringTokenizer(traceFilter, ","); //$NON-NLS-1$
124 while (tokenizer.hasMoreTokens()) {
125 String cat = tokenizer.nextToken().trim();
126 if (category.equals(cat)) {
127 return true;
128 }
129 }
130 }
131 return false;
132 }
133
134 public static void log(int level, String message) {
135 _log(level, message, null);
136 }
137
138 public static void log(int level, String message, Throwable exception) {
139 _log(level, message, exception);
140 }
141
142 public static void logException(String message, Throwable exception) {
143 _log(ERROR, message, exception);
144 }
145
146 public static void logException(Throwable exception) {
147 _log(ERROR, exception.getMessage(), exception);
148 }
149
150 public static void trace(String category, String message) {
151 _trace(category, message, null);
152 }
153
154 public static void traceException(String category, String message, Throwable exception) {
155 _trace(category, message, exception);
156 }
157
158 public static void traceException(String category, Throwable exception) {
159 _trace(category, exception.getMessage(), exception);
160 }
161}