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