Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 94b0c110f93a423bc8e9a072959cbd3a779a61b6 (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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*******************************************************************************
 * Copyright (c) 2011, 2012 Wind River Systems, Inc. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.runtime.tracing;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.tcf.te.runtime.activator.CoreBundleActivator;

/**
 * Helper class to handle tracing using the platforms debug capabilities.
 */
public class TraceHandler {
	/**
	 * The bundle identifier.
	 */
	private final String identifier;

	/**
	 * The tracer instance.
	 */
	private Tracer tracer = null;

	/**
	 * The tracer is responsible for writing the trace message to the desired
	 * output media.
	 */
	protected static class Tracer {

		/**
		 * The bundle identifier.
		 */
		private final String fIdentifier;

		/**
		 * The qualifier for the default "<bundle identifier>/debugmode"
		 * tracing slot.
		 */
		private final String fDebugModeQualifier;

		/**
		 * Constructor.
		 *
		 * @param identifier The bundle identifier. Must not be <code>null</code>.
		 */
		public Tracer(String identifier) {
			Assert.isNotNull(identifier);
			fIdentifier = identifier;

			// Initialize the debug mode qualifier
			fDebugModeQualifier = fIdentifier + "/debugmode"; //$NON-NLS-1$
		}

		/**
		 * Returns the value of the debug mode tracing slot.
		 * <p>
		 * If not set, or the value is not an {@link Integer}, the method returns <code>0</code>.
		 *
		 * @return The debug mode value.
		 */
		protected int getDebugMode() {
			try {
				String mode = Platform.getDebugOption(fDebugModeQualifier);
				if (mode != null && Integer.decode(mode).intValue() > 0) {
					return Integer.decode(mode).intValue();
				}
			} catch (NumberFormatException e) { /* ignored on purpose */ }

			return 0;
		}

		/**
		 * Check if the specified trace slot is enabled.
		 *
		 * @param slotId The name of the slot.
		 * @return <code>true</code> if the slot is defined and enabled, <code>false</code> otherwise.
		 */
		protected boolean isSlotEnabled(String slotId) {
			return fIdentifier != null ? Boolean.parseBoolean(Platform.getDebugOption(fIdentifier + "/" + slotId)) : false; //$NON-NLS-1$
		}

		/**
		 * Check if tracing is enabled for given mode and slot.
		 *
		 * @param debugMode The debug mode for the current debug.
		 * @param slotId The name of the slot.
		 *
		 * @return <code>true</code> if the debug should be written, <code>false</code> otherwise.
		 */
		protected final boolean isEnabled(int debugMode, String slotId) {
			return getDebugMode() < 0 ||
			(debugMode <= getDebugMode() &&
					(slotId == null || slotId.trim().length() == 0 || isSlotEnabled(slotId)));
		}

		/**
		 * Format the trace message.
		 *
		 * @param message The trace message.
		 * @param debugMode The debug mode.
		 * @param slotId The name of the slot.
		 * @param severity The severity. See {@link IStatus} for valid severity values.
		 * @param clazz The class that calls this tracer.
		 *
		 * @see IStatus
		 */
		protected String getFormattedDebugMessage(String message, int debugMode, String slotId, int severity, Object clazz) {
			StringBuffer debug = new StringBuffer();
			if (slotId != null || clazz != null) {
				if (clazz != null) {
					String name = clazz instanceof Class<?> ? ((Class<?>)clazz).getSimpleName() : clazz.getClass().getSimpleName();
					debug.append(name.trim().length() > 0 ? name.trim() : clazz instanceof Class<?> ? ((Class<?>)clazz).getName() : clazz.getClass().getName());
				}
				if (slotId != null) {
					debug.append(" at "); //$NON-NLS-1$
					debug.append(slotId);
				}
				if (debugMode >= 0) {
					debug.append(" (Mode "); //$NON-NLS-1$
					debug.append(debugMode);
					debug.append(')');
				}
				debug.append('\n');
				debug.append('\t');
			}
			debug.append(message);

			return debug.toString();
		}

		/**
		 * Write the trace message.
		 *
		 * @param message The trace message.
		 * @param debugMode The debug mode.
		 * @param slotId The name of the slot.
		 * @param severity The severity. See {@link IStatus} for valid severity values.
		 * @param clazz The class that calls this tracer.
		 *
		 * @see IStatus
		 */
		protected void write(String message, int debugMode, String slotId, int severity, Object clazz) {
			String formattedMessage = getFormattedDebugMessage(message, debugMode, slotId, severity, clazz);
			if (severity == IStatus.ERROR || severity == IStatus.WARNING) {
				System.err.println(formattedMessage);
			}
			else {
				System.out.println(formattedMessage);
			}
		}

		/**
		 * Trace the given message with the given debug mode and slot.
		 *
		 * @param message The trace message.
		 * @param debugMode The debug mode.
		 * @param slotId The name of the slot.
		 * @param severity The severity. See {@link IStatus} for valid severity values.
		 * @param clazz The class that calls this tracer.
		 *
		 * @see IStatus
		 */
		public final void trace(String message, int debugMode, String slotId, int severity, Object clazz) {
			if (isEnabled(debugMode, slotId)) {
				write(message, debugMode, slotId, severity, clazz);
			}
		}
	}

	/**
	 * Constructor.
	 * <p>
	 * Initializes the tracing handler with the given bundle identifier.
	 *
	 * @param identifier The bundle identifier or <code>null</code>.
	 */
	public TraceHandler(String identifier) {
		this.identifier = identifier != null ? identifier : CoreBundleActivator.getUniqueIdentifier();
		Assert.isNotNull(this.identifier);
	}

	/**
	 * Returns the identifier.
	 */
	protected final String getIdentifier() {
		return identifier;
	}

	/**
	 * Returns the tracer instance. Create a new tracer instance
	 * on first invocation.
	 *
	 * @return The tracer instance.
	 */
	protected Tracer getTracer() {
		if (tracer == null) {
			tracer = new Tracer(identifier);
		}
		return tracer;
	}

	/**
	 * Return the current debug mode.
	 */
	public final int getDebugMode() {
		return getTracer().getDebugMode();
	}

	/**
	 * Check whether a trace slot is enabled. The debug mode defaults
	 * to 0.
	 *
	 * @param slotId The name of the slot.
	 *
	 * @return <code>true</code> if the slot is enabled, <code>false</code> otherwise.
	 */
	public final boolean isSlotEnabled(String slotId) {
		return isSlotEnabled(0, slotId);
	}

	/**
	 * Check whether a trace slot is enabled with the given debug mode.
	 *
	 * @param debugMode The debug mode
	 * @param slotId The name of the slot.
	 *
	 * @return <code>true</code> if the slot is enabled, <code>false</code> otherwise.
	 */
	public final boolean isSlotEnabled(int debugMode, String slotId) {
		return getTracer().isEnabled(debugMode, slotId);
	}

	/**
	 * Trace the given message.
	 * <p>
	 * The message severity will be {@link IStatus#INFO} and the message will be
	 * traced unconditionally.
	 *
	 * @param message The message.
	 * @param clazz The class that calls this tracer or <code>null</code>.
	 */
	public final void trace(String message, Object clazz) {
		getTracer().trace(message, 0, null, IStatus.INFO, clazz);
	}

	/**
	 * Trace the given message.
	 * <p>
	 * The message severity will be {@link IStatus#INFO}.
	 *
	 * @param message The message.
	 * @param debugMode The minimum debug mode that has to be set to write out the message.
	 * @param clazz The class that calls this tracer or <code>null</code>.
	 */
	public final void trace(String message, int debugMode, Object clazz) {
		getTracer().trace(message, debugMode, null, IStatus.INFO, clazz);
	}

	/**
	 * Trace the given message.
	 * <p>
	 * The message severity will be {@link IStatus#INFO} and the debug mode
	 * will default to <code>0</code>.
	 *
	 * @param message The message.
	 * @param slotId The slot that has to be enabled to write out the message.
	 * @param clazz The class that calls this tracer or <code>null</code>.
	 */
	public final void trace(String message, String slotId, Object clazz) {
		getTracer().trace(message, 0, slotId, IStatus.INFO, clazz);
	}

	/**
	 * Trace the given message.
	 *
	 * @param message The message.
	 * @param debugMode The minimum debug mode that has to be set to write out the message.
	 * @param slotId The slot that has to be enabled to write out the message.
	 * @param severity The severity. See {@link IStatus} for valid severity values.
	 * @param clazz The class that calls this tracer or <code>null</code>.
	 *
	 * @see IStatus
	 */
	public final void trace(String message, int debugMode, String slotId, int severity, Object clazz) {
		getTracer().trace(message, debugMode, slotId, severity, clazz);
	}

}

Back to the top