Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8d80f22d2a23cd857a15f7bfe72f7f178105349a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*******************************************************************************
 * Copyright (c) 2009, 2015 Atos Origin, CEA, Christian W. Damus, and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Atos Origin - initial API and implementation
 *     Christian W. Damus (CEA) - bug 422257
 *     Christian W. Damus - bug 465416
 *
 *******************************************************************************/
package org.eclipse.papyrus.infra.core.log;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;

/**
 * A Log Helper.
 *
 * @author tszadel
 *
 */
public class LogHelper {

	/** The plugin Id. */
	private String pluginId;

	/** The plugin related to that helper. */
	private Plugin activator;

	private boolean tracing;
	private Map<String, Boolean> traceOptions;

	/**
	 * Default Constructor.
	 * The associated plugin can be set later.
	 * If no plugin is set, use java log.
	 */
	public LogHelper() {
	}

	/**
	 * Constructor.
	 *
	 * @param activator
	 *            The activator.
	 */
	public LogHelper(Plugin activator) {
		setPlugin(activator);
	}

	/**
	 * Set the associated plugin.
	 * This plugin log will be used as log.
	 *
	 * @param activator
	 */
	public void setPlugin(Plugin activator) {
		this.pluginId = activator.getBundle().getSymbolicName();
		this.activator = activator;

		this.tracing = Boolean.valueOf(Platform.getDebugOption(String.format("%s/debug", pluginId))); //$NON-NLS-1$
		if (tracing) {
			this.traceOptions = new ConcurrentHashMap<String, Boolean>(32, 0.75f, 4);
		}
	}

	/**
	 * Log an informative message into the Eclipse log file
	 *
	 * @param message
	 *            the message to log
	 */
	public void info(String message) {
		log(message, IStatus.INFO);
	}

	/**
	 * Log a debug message into the Eclipse log file
	 *
	 * @param message
	 *            the message to log
	 */
	public void debug(String message) {
		if (isDebugEnabled()) {
			log("[DEBUG] " + message, IStatus.INFO);
		}
	}

	/**
	 * Test if the platform is in debug mode.
	 *
	 * @return True if the platform is in debug mode.
	 */
	public boolean isDebugEnabled() {
		if (activator != null) {
			return Platform.inDebugMode();
		}

		return false;
	}

	/**
	 * Queries whether the specified tracing {@code option} is enabled by the user.
	 * 
	 * @param option
	 *            a tracing option, without the <tt>{@literal <bundle-id>/debug/}</tt> path prefix
	 * 
	 * @return whether the tracing {@code option} is enabled
	 * 
	 * @see #trace(String, String)
	 */
	public boolean isTraceEnabled(String option) {
		if (tracing) {
			final String key = String.format("%s/debug/%s", pluginId, option); //$NON-NLS-1$
			Boolean result;

			synchronized (traceOptions) {
				result = traceOptions.get(key);
				if (result == null) {
					result = Boolean.valueOf(Platform.getDebugOption(key));
					traceOptions.put(key, result);
				}
			}

			return result;
		}

		return false;
	}

	/**
	 * Prints the specified trace {@code message}, if the {@code option} is enabled by the user.
	 * 
	 * @param option
	 *            the tracing option, without the <tt>{@literal <bundle-id>/debug/}</tt> path prefix
	 * @param message
	 *            the message to print
	 * 
	 * @see #isTraceEnabled(String)
	 */
	public void trace(String option, String message) {
		if (isTraceEnabled(option)) {
			System.out.printf("[TRACE:%s] %s%n", option, message); //$NON-NLS-1$
		}
	}

	/**
	 * Log a message with given level into the Eclipse log file
	 *
	 * @param message
	 *            the message to log
	 * @param level
	 *            the message priority
	 */
	private void log(String message, int level) {
		log(new Status(level, pluginId, message));
	}

	/**
	 *
	 * @param status
	 */
	public void log(IStatus status) {

		if (activator == null) {
			// TODO Do log with java ?
		} else {
			activator.getLog().log(status);
		}
	}

	/**
	 * Log a warning message.
	 *
	 * @param e
	 *            the exception to log
	 */
	public void warn(String message) {
		log(message, IStatus.WARNING);
	}


	/**
	 * Log an exception into the Eclipse log file
	 *
	 * @param e
	 *            the exception to log
	 */
	public void error(Throwable e) {
		error("Unexpected Error", e);
	}

	/**
	 * Log an exception into the Eclipse log file
	 *
	 * @param message
	 *            the message
	 * @param e
	 *            the exception to log
	 */
	public void error(String message, Throwable e) {

		Throwable t = e;
		if (e instanceof InvocationTargetException) {
			t = ((InvocationTargetException) e).getTargetException();
		}

		IStatus status;
		if (t instanceof CoreException) {
			status = ((CoreException) t).getStatus();
		} else {
			status = new Status(IStatus.ERROR, pluginId, message, e);
		}

		log(status);
	}

	/**
	 * Obtains the stack-trace description of the caller of the calling method (that is, the method that
	 * called the method using this helper method). Useful for logging warning messages etc.
	 *
	 * @return the caller of my caller, or a placeholder in case the JVM cannot provide the necessary
	 *         stack information (which is a documented possibility)
	 */
	public String getCallerMethod() {
		StackTraceElement[] stack = new Exception().fillInStackTrace().getStackTrace();
		return ((stack == null) || (stack.length < 3)) ? "<unknown caller>" : stack[2].toString();
	}
}

Back to the top