Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 285300
Collapse All | Expand All

(-)supplement/src/org/eclipse/osgi/framework/debug/EclipseDebugTrace.java (-48 / +44 lines)
Lines 20-101 Link Here
20
20
21
/**
21
/**
22
 * The DebugTrace implementation for Eclipse.
22
 * The DebugTrace implementation for Eclipse.
23
 * <p>
24
 * Clients may extend this class.
25
 * </p>
26
 * @since 3.5
27
 */
23
 */
28
public class EclipseDebugTrace implements DebugTrace {
24
class EclipseDebugTrace implements DebugTrace {
29
25
30
	/** The system property used to specify size a trace file can grow before it is rotated */
26
	/** The system property used to specify size a trace file can grow before it is rotated */
31
	public static final String PROP_TRACE_SIZE_MAX = "eclipse.trace.size.max"; //$NON-NLS-1$
27
	private static final String PROP_TRACE_SIZE_MAX = "eclipse.trace.size.max"; //$NON-NLS-1$
32
	/** The system property used to specify the maximum number of backup trace files to use */
28
	/** The system property used to specify the maximum number of backup trace files to use */
33
	public static final String PROP_TRACE_FILE_MAX = "eclipse.trace.backup.max"; //$NON-NLS-1$
29
	private static final String PROP_TRACE_FILE_MAX = "eclipse.trace.backup.max"; //$NON-NLS-1$
34
	/** The trace message for a thread stack dump */
30
	/** The trace message for a thread stack dump */
35
	protected final static String MESSAGE_THREAD_DUMP = "Thread Stack dump: "; //$NON-NLS-1$
31
	private final static String MESSAGE_THREAD_DUMP = "Thread Stack dump: "; //$NON-NLS-1$
36
	/** The trace message for a method completing with a return value */
32
	/** The trace message for a method completing with a return value */
37
	protected final static String MESSAGE_EXIT_METHOD_WITH_RESULTS = "Exiting method with result: "; //$NON-NLS-1$
33
	private final static String MESSAGE_EXIT_METHOD_WITH_RESULTS = "Exiting method with result: "; //$NON-NLS-1$
38
	/** The trace message for a method completing with no return value */
34
	/** The trace message for a method completing with no return value */
39
	protected final static String MESSAGE_EXIT_METHOD_NO_RESULTS = "Exiting method with a void return"; //$NON-NLS-1$
35
	private final static String MESSAGE_EXIT_METHOD_NO_RESULTS = "Exiting method with a void return"; //$NON-NLS-1$
40
	/** The trace message for a method starting with a set of arguments */
36
	/** The trace message for a method starting with a set of arguments */
41
	protected final static String MESSAGE_ENTER_METHOD_WITH_PARAMS = "Entering method with parameters: ("; //$NON-NLS-1$
37
	private final static String MESSAGE_ENTER_METHOD_WITH_PARAMS = "Entering method with parameters: ("; //$NON-NLS-1$
42
	/** The trace message for a method starting with no arguments */
38
	/** The trace message for a method starting with no arguments */
43
	protected final static String MESSAGE_ENTER_METHOD_NO_PARAMS = "Entering method with no parameters"; //$NON-NLS-1$
39
	private final static String MESSAGE_ENTER_METHOD_NO_PARAMS = "Entering method with no parameters"; //$NON-NLS-1$
44
	/** The version attribute written to the header of the trace file */
40
	/** The version attribute written to the header of the trace file */
45
	protected final static String TRACE_FILE_VERSION_COMMENT = "version: "; //$NON-NLS-1$
41
	private final static String TRACE_FILE_VERSION_COMMENT = "version: "; //$NON-NLS-1$
46
	/** The version value written to the header of the trace file */
42
	/** The version value written to the header of the trace file */
47
	protected final static String TRACE_FILE_VERSION = "1.0"; //$NON-NLS-1$
43
	private final static String TRACE_FILE_VERSION = "1.0"; //$NON-NLS-1$
48
	/** The new session identifier to be written whenever a new session starts */
44
	/** The new session identifier to be written whenever a new session starts */
49
	protected final static String TRACE_NEW_SESSION = "!SESSION "; //$NON-NLS-1$
45
	private final static String TRACE_NEW_SESSION = "!SESSION "; //$NON-NLS-1$
50
	/** The date attribute written to the header of the trace file to show when this file was created */
46
	/** The date attribute written to the header of the trace file to show when this file was created */
51
	protected final static String TRACE_FILE_DATE = "Time of creation: "; //$NON-NLS-1$
47
	private final static String TRACE_FILE_DATE = "Time of creation: "; //$NON-NLS-1$
52
	/** Trace date formatter using the pattern: yyyy-MM-dd HH:mm:ss.SSS  */
48
	/** Trace date formatter using the pattern: yyyy-MM-dd HH:mm:ss.SSS  */
53
	protected final static SimpleDateFormat TRACE_FILE_DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); //$NON-NLS-1$
49
	private final static SimpleDateFormat TRACE_FILE_DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); //$NON-NLS-1$
54
	/** The comment character used by the trace file */
50
	/** The comment character used by the trace file */
55
	protected final static String TRACE_COMMENT = "#"; //$NON-NLS-1$
51
	private final static String TRACE_COMMENT = "#"; //$NON-NLS-1$
56
	/** The delimiter used to separate trace elements such as the time stamp, message, etc */
52
	/** The delimiter used to separate trace elements such as the time stamp, message, etc */
57
	protected final static String TRACE_ELEMENT_DELIMITER = "|"; //$NON-NLS-1$
53
	private final static String TRACE_ELEMENT_DELIMITER = "|"; //$NON-NLS-1$
58
	/** OS-specific line separator */
54
	/** OS-specific line separator */
59
	protected static final String LINE_SEPARATOR;
55
	private static final String LINE_SEPARATOR;
60
	static {
56
	static {
61
		String s = System.getProperty("line.separator"); //$NON-NLS-1$
57
		String s = System.getProperty("line.separator"); //$NON-NLS-1$
62
		LINE_SEPARATOR = s == null ? "\n" : s; //$NON-NLS-1$
58
		LINE_SEPARATOR = s == null ? "\n" : s; //$NON-NLS-1$
63
	}
59
	}
64
	/** The value written to the trace file if a null object is being traced */
60
	/** The value written to the trace file if a null object is being traced */
65
	public final static String NULL_VALUE = "<null>"; //$NON-NLS-1$
61
	private final static String NULL_VALUE = "<null>"; //$NON-NLS-1$
66
	/**  */
62
	/**  */
67
	private final static SecureAction secureAction = (SecureAction) AccessController.doPrivileged(SecureAction.createSecureAction());
63
	private final static SecureAction secureAction = (SecureAction) AccessController.doPrivileged(SecureAction.createSecureAction());
68
	/** A lock object used to synchronize access to the trace file */
64
	/** A lock object used to synchronize access to the trace file */
69
	protected final static Object writeLock = new Object();
65
	private final static Object writeLock = new Object();
70
66
71
	/******************* Tracing file attributes **************************/
67
	/******************* Tracing file attributes **************************/
72
	/** The default size a trace file can grow before it is rotated */
68
	/** The default size a trace file can grow before it is rotated */
73
	public static final int DEFAULT_TRACE_FILE_SIZE = 1000; // The value is in KB.
69
	private static final int DEFAULT_TRACE_FILE_SIZE = 1000; // The value is in KB.
74
	/** The default number of backup trace files */
70
	/** The default number of backup trace files */
75
	public static final int DEFAULT_TRACE_FILES = 10;
71
	private static final int DEFAULT_TRACE_FILES = 10;
76
	/** The minimum size limit for trace file rotation */
72
	/** The minimum size limit for trace file rotation */
77
	public static final int DEFAULT_TRACE_FILE_MIN_SIZE = 10;
73
	private static final int DEFAULT_TRACE_FILE_MIN_SIZE = 10;
78
	/** The extension used for log files */
74
	/** The extension used for log files */
79
	public static final String TRACE_FILE_EXTENSION = ".trace"; //$NON-NLS-1$
75
	private static final String TRACE_FILE_EXTENSION = ".trace"; //$NON-NLS-1$
80
	/** The extension markup to use for backup log files*/
76
	/** The extension markup to use for backup log files*/
81
	public static final String BACKUP_MARK = ".bak_"; //$NON-NLS-1$
77
	private static final String BACKUP_MARK = ".bak_"; //$NON-NLS-1$
82
	/** The maximum size that a trace file should grow (0 = unlimited) */
78
	/** The maximum size that a trace file should grow (0 = unlimited) */
83
	protected int maxTraceFileSize = DEFAULT_TRACE_FILE_SIZE; // The value is in KB.
79
	private int maxTraceFileSize = DEFAULT_TRACE_FILE_SIZE; // The value is in KB.
84
	/** The maximum number of trace files that should be saved */
80
	/** The maximum number of trace files that should be saved */
85
	protected int maxTraceFiles = DEFAULT_TRACE_FILES;
81
	private int maxTraceFiles = DEFAULT_TRACE_FILES;
86
	/** The index of the currently backed-up trace file */
82
	/** The index of the currently backed-up trace file */
87
	protected int backupTraceFileIndex = 0;
83
	private int backupTraceFileIndex = 0;
88
84
89
	/** An optional argument to specify the name of the class used by clients to trace messages.  If no trace class is specified
85
	/** An optional argument to specify the name of the class used by clients to trace messages.  If no trace class is specified
90
	 * then the class calling this API is assumed to be the class being traced.
86
	 * then the class calling this API is assumed to be the class being traced.
91
	*/
87
	*/
92
	protected Class traceClass = null;
88
	private String traceClass = null;
93
	/** The symbolic name of the bundle being traced */
89
	/** The symbolic name of the bundle being traced */
94
	protected String bundleSymbolicName = null;
90
	private String bundleSymbolicName = null;
95
	/** A flag to determine if the message being written is done to a new file (i.e. should the header information be written) */
91
	/** A flag to determine if the message being written is done to a new file (i.e. should the header information be written) */
96
	protected static boolean newSession = true;
92
	static boolean newSession = true;
97
	/** DebugOptions are used to determine if the specified bundle symbolic name + option-path has debugging enabled */
93
	/** DebugOptions are used to determine if the specified bundle symbolic name + option-path has debugging enabled */
98
	protected DebugOptions debugOptions = null;
94
	private DebugOptions debugOptions = null;
99
95
100
	/**
96
	/**
101
	 * Construct a new EclipseDebugTrace for the specified bundle symbolic name and write messages to the specified
97
	 * Construct a new EclipseDebugTrace for the specified bundle symbolic name and write messages to the specified
Lines 119-125 Link Here
119
	 */
115
	 */
120
	public EclipseDebugTrace(final String bundleSymbolicName, final DebugOptions debugOptions, final Class traceClass) {
116
	public EclipseDebugTrace(final String bundleSymbolicName, final DebugOptions debugOptions, final Class traceClass) {
121
117
122
		this.traceClass = traceClass;
118
		this.traceClass = traceClass != null ? traceClass.getName() : null;
123
		this.debugOptions = debugOptions;
119
		this.debugOptions = debugOptions;
124
		this.bundleSymbolicName = bundleSymbolicName;
120
		this.bundleSymbolicName = bundleSymbolicName;
125
		readLogProperties();
121
		readLogProperties();
Lines 131-137 Link Here
131
	 * @param optionPath The <i>option-path</i>
127
	 * @param optionPath The <i>option-path</i>
132
	 * @return Returns true if debugging is enabled for the specified option-path on this bundle; Otherwise false.
128
	 * @return Returns true if debugging is enabled for the specified option-path on this bundle; Otherwise false.
133
	 */
129
	 */
134
	protected final boolean isDebuggingEnabled(final String optionPath) {
130
	private final boolean isDebuggingEnabled(final String optionPath) {
135
		if (optionPath == null)
131
		if (optionPath == null)
136
			return true;
132
			return true;
137
		boolean debugEnabled = false;
133
		boolean debugEnabled = false;
Lines 279-285 Link Here
279
	 *            The array of StackTraceElement objects
275
	 *            The array of StackTraceElement objects
280
	 * @return A String of the stack dump produced by the list of elements
276
	 * @return A String of the stack dump produced by the list of elements
281
	 */
277
	 */
282
	protected final String convertStackTraceElementsToString(final StackTraceElement[] elements) {
278
	private final String convertStackTraceElementsToString(final StackTraceElement[] elements) {
283
279
284
		final StringBuffer buffer = new StringBuffer();
280
		final StringBuffer buffer = new StringBuffer();
285
		if (elements != null) {
281
		if (elements != null) {
Lines 303-309 Link Here
303
	 * 
299
	 * 
304
	 * @param entry The FrameworkTraceEntry to write to the log file.
300
	 * @param entry The FrameworkTraceEntry to write to the log file.
305
	 */
301
	 */
306
	protected void writeRecord(final FrameworkDebugTraceEntry entry) {
302
	private void writeRecord(final FrameworkDebugTraceEntry entry) {
307
303
308
		if (entry != null) {
304
		if (entry != null) {
309
			synchronized (EclipseDebugTrace.writeLock) {
305
			synchronized (EclipseDebugTrace.writeLock) {
Lines 338-344 Link Here
338
	/**
334
	/**
339
	 * Reads the PROP_TRACE_SIZE_MAX and PROP_TRACE_FILE_MAX properties.
335
	 * Reads the PROP_TRACE_SIZE_MAX and PROP_TRACE_FILE_MAX properties.
340
	 */
336
	 */
341
	protected void readLogProperties() {
337
	private void readLogProperties() {
342
338
343
		String newMaxTraceFileSize = secureAction.getProperty(PROP_TRACE_SIZE_MAX);
339
		String newMaxTraceFileSize = secureAction.getProperty(PROP_TRACE_SIZE_MAX);
344
		if (newMaxTraceFileSize != null) {
340
		if (newMaxTraceFileSize != null) {
Lines 366-372 Link Here
366
	 * @param traceFile The tracing file
362
	 * @param traceFile The tracing file
367
	 * @return false if an error occurred trying to rotate the trace file
363
	 * @return false if an error occurred trying to rotate the trace file
368
	 */
364
	 */
369
	protected boolean checkTraceFileSize(final File traceFile) {
365
	private boolean checkTraceFileSize(final File traceFile) {
370
366
371
		// 0 file size means there is no size limit
367
		// 0 file size means there is no size limit
372
		boolean isBackupOK = true;
368
		boolean isBackupOK = true;
Lines 433-439 Link Here
433
	 * @param comment the comment to be written to the trace file
429
	 * @param comment the comment to be written to the trace file
434
	 * @throws IOException If an error occurs while writing the comment
430
	 * @throws IOException If an error occurs while writing the comment
435
	 */
431
	 */
436
	protected void writeComment(final Writer traceWriter, final String comment) throws IOException {
432
	private void writeComment(final Writer traceWriter, final String comment) throws IOException {
437
433
438
		StringBuffer commentText = new StringBuffer(EclipseDebugTrace.TRACE_COMMENT);
434
		StringBuffer commentText = new StringBuffer(EclipseDebugTrace.TRACE_COMMENT);
439
		commentText.append(" "); //$NON-NLS-1$
435
		commentText.append(" "); //$NON-NLS-1$
Lines 447-453 Link Here
447
	 * 
443
	 * 
448
	 * @return A formatted time stamp based on the {@link EclipseDebugTrace#TRACE_FILE_DATE_FORMATTER} formatter
444
	 * @return A formatted time stamp based on the {@link EclipseDebugTrace#TRACE_FILE_DATE_FORMATTER} formatter
449
	 */
445
	 */
450
	protected final String getFormattedDate() {
446
	private final String getFormattedDate() {
451
447
452
		return this.getFormattedDate(System.currentTimeMillis());
448
		return this.getFormattedDate(System.currentTimeMillis());
453
	}
449
	}
Lines 457-463 Link Here
457
	 * 
453
	 * 
458
	 * @return A formatted time stamp based on the {@link EclipseDebugTrace#TRACE_FILE_DATE_FORMATTER} formatter
454
	 * @return A formatted time stamp based on the {@link EclipseDebugTrace#TRACE_FILE_DATE_FORMATTER} formatter
459
	 */
455
	 */
460
	protected final String getFormattedDate(long timestamp) {
456
	private final String getFormattedDate(long timestamp) {
461
457
462
		return EclipseDebugTrace.TRACE_FILE_DATE_FORMATTER.format(new Date(timestamp));
458
		return EclipseDebugTrace.TRACE_FILE_DATE_FORMATTER.format(new Date(timestamp));
463
	}
459
	}
Lines 468-474 Link Here
468
	 * @param traceWriter the trace writer
464
	 * @param traceWriter the trace writer
469
	 * @throws IOException If an error occurs while writing this session information 
465
	 * @throws IOException If an error occurs while writing this session information 
470
	 */
466
	 */
471
	protected void writeSession(final Writer traceWriter) throws IOException {
467
	private void writeSession(final Writer traceWriter) throws IOException {
472
468
473
		writeComment(traceWriter, EclipseDebugTrace.TRACE_NEW_SESSION + this.getFormattedDate());
469
		writeComment(traceWriter, EclipseDebugTrace.TRACE_NEW_SESSION + this.getFormattedDate());
474
		writeComment(traceWriter, EclipseDebugTrace.TRACE_FILE_VERSION_COMMENT + EclipseDebugTrace.TRACE_FILE_VERSION);
470
		writeComment(traceWriter, EclipseDebugTrace.TRACE_FILE_VERSION_COMMENT + EclipseDebugTrace.TRACE_FILE_VERSION);
Lines 488-494 Link Here
488
	 * @param entry The trace entry object to write to the trace file
484
	 * @param entry The trace entry object to write to the trace file
489
	 * @throws IOException If an error occurs while writing this message
485
	 * @throws IOException If an error occurs while writing this message
490
	 */
486
	 */
491
	protected void writeMessage(final Writer traceWriter, final FrameworkDebugTraceEntry entry) throws IOException {
487
	private void writeMessage(final Writer traceWriter, final FrameworkDebugTraceEntry entry) throws IOException {
492
488
493
		// format the trace entry
489
		// format the trace entry
494
		StringBuffer message = new StringBuffer(entry.getThreadName());
490
		StringBuffer message = new StringBuffer(entry.getThreadName());
Lines 537-543 Link Here
537
	 * @param output an OutputStream to use for the Writer
533
	 * @param output an OutputStream to use for the Writer
538
	 * @return A Writer for the given OutputStream
534
	 * @return A Writer for the given OutputStream
539
	 */
535
	 */
540
	protected Writer logForStream(OutputStream output) {
536
	private Writer logForStream(OutputStream output) {
541
537
542
		try {
538
		try {
543
			return new BufferedWriter(new OutputStreamWriter(output, "UTF-8")); //$NON-NLS-1$
539
			return new BufferedWriter(new OutputStreamWriter(output, "UTF-8")); //$NON-NLS-1$
Lines 553-559 Link Here
553
	 * @param traceFile The tracing file
549
	 * @param traceFile The tracing file
554
	 * @return Returns a new Writer object  
550
	 * @return Returns a new Writer object  
555
	 */
551
	 */
556
	protected Writer openWriter(final File traceFile) {
552
	private Writer openWriter(final File traceFile) {
557
553
558
		Writer traceWriter = null;
554
		Writer traceWriter = null;
559
		if (traceFile != null) {
555
		if (traceFile != null) {
Lines 573-579 Link Here
573
	 * 
569
	 * 
574
	 * @param traceWriter The trace writer
570
	 * @param traceWriter The trace writer
575
	 */
571
	 */
576
	protected void closeWriter(Writer traceWriter) {
572
	private void closeWriter(Writer traceWriter) {
577
573
578
		if (traceWriter != null) {
574
		if (traceWriter != null) {
579
			try {
575
			try {
(-)supplement/src/org/eclipse/osgi/framework/debug/FrameworkDebugTraceEntry.java (-3 / +3 lines)
Lines 75-81 Link Here
75
	 * @param traceClass
75
	 * @param traceClass
76
	 *            The class that calls the trace API
76
	 *            The class that calls the trace API
77
	 */
77
	 */
78
	public FrameworkDebugTraceEntry(final String bundleSymbolicName, final String optionPath, final String message, final Class traceClass) {
78
	public FrameworkDebugTraceEntry(final String bundleSymbolicName, final String optionPath, final String message, final String traceClass) {
79
		this(bundleSymbolicName, optionPath, message, null, traceClass);
79
		this(bundleSymbolicName, optionPath, message, null, traceClass);
80
	}
80
	}
81
81
Lines 93-99 Link Here
93
	 * @param traceClass
93
	 * @param traceClass
94
	 *            The class that calls the trace API 
94
	 *            The class that calls the trace API 
95
	 */
95
	 */
96
	public FrameworkDebugTraceEntry(String bundleSymbolicName, final String optionPath, final String message, final Throwable error, final Class traceClass) {
96
	public FrameworkDebugTraceEntry(String bundleSymbolicName, final String optionPath, final String message, final Throwable error, final String traceClass) {
97
		threadName = Thread.currentThread().getName();
97
		threadName = Thread.currentThread().getName();
98
		if (optionPath == null) {
98
		if (optionPath == null) {
99
			this.optionPath = FrameworkDebugTraceEntry.DEFAULT_OPTION_PATH;
99
			this.optionPath = FrameworkDebugTraceEntry.DEFAULT_OPTION_PATH;
Lines 122-128 Link Here
122
				 * stack element is not that class, then we assume this stack element
122
				 * stack element is not that class, then we assume this stack element
123
				 * is the caller of the trace API. 
123
				 * is the caller of the trace API. 
124
				 */
124
				 */
125
				if ((traceClass == null) || !fullClassName.equals(traceClass.getName())) {
125
				if ((traceClass == null) || !fullClassName.equals(traceClass)) {
126
					determineClassName = stackElements[i].getClassName();
126
					determineClassName = stackElements[i].getClassName();
127
					determineMethodName = stackElements[i].getMethodName();
127
					determineMethodName = stackElements[i].getMethodName();
128
					determineLineNumber = stackElements[i].getLineNumber();
128
					determineLineNumber = stackElements[i].getLineNumber();
(-)src/org/eclipse/osgi/tests/debugoptions/DebugOptionsTestCase.java (-5 / +97 lines)
Lines 11-27 Link Here
11
11
12
package org.eclipse.osgi.tests.debugoptions;
12
package org.eclipse.osgi.tests.debugoptions;
13
13
14
import java.io.*;
14
import java.util.*;
15
import java.util.*;
15
import java.util.Map.Entry;
16
import java.util.Map.Entry;
16
import junit.framework.*;
17
import junit.framework.Test;
18
import junit.framework.TestSuite;
19
import org.eclipse.core.tests.harness.CoreTest;
17
import org.eclipse.osgi.framework.debug.FrameworkDebugTraceEntry;
20
import org.eclipse.osgi.framework.debug.FrameworkDebugTraceEntry;
18
import org.eclipse.osgi.service.debug.DebugOptions;
21
import org.eclipse.osgi.service.debug.*;
19
import org.eclipse.osgi.service.debug.DebugOptionsListener;
20
import org.eclipse.osgi.tests.OSGiTestsActivator;
22
import org.eclipse.osgi.tests.OSGiTestsActivator;
21
import org.osgi.framework.ServiceReference;
23
import org.osgi.framework.ServiceReference;
22
import org.osgi.framework.ServiceRegistration;
24
import org.osgi.framework.ServiceRegistration;
23
25
24
public class DebugOptionsTestCase extends TestCase {
26
public class DebugOptionsTestCase extends CoreTest {
25
	public static Test suite() {
27
	public static Test suite() {
26
		return new TestSuite(DebugOptionsTestCase.class);
28
		return new TestSuite(DebugOptionsTestCase.class);
27
	}
29
	}
Lines 105-111 Link Here
105
			String bundleName = OSGiTestsActivator.getContext().getBundle().getSymbolicName();
107
			String bundleName = OSGiTestsActivator.getContext().getBundle().getSymbolicName();
106
			String optionPath = "/debug"; //$NON-NLS-1$
108
			String optionPath = "/debug"; //$NON-NLS-1$
107
			String message = "Test message"; //$NON-NLS-1$
109
			String message = "Test message"; //$NON-NLS-1$
108
			Class tracingClass = this.getClass();
110
			String tracingClass = this.getClass().getName();
109
			return new FrameworkDebugTraceEntry(bundleName, optionPath, message, tracingClass);
111
			return new FrameworkDebugTraceEntry(bundleName, optionPath, message, tracingClass);
110
		}
112
		}
111
	}
113
	}
Lines 176-181 Link Here
176
		anotherReg.unregister();
178
		anotherReg.unregister();
177
	}
179
	}
178
180
181
	public void testTraceFile01() {
182
		if (debugOptions.isDebugEnabled())
183
			return; // cannot test
184
		debugOptions.setDebugEnabled(true);
185
		assertTrue("Debug is not enabled", debugOptions.isDebugEnabled()); //$NON-NLS-1$
186
		debugOptions.setOption(getName() + "/debug", "true"); //$NON-NLS-1$ //$NON-NLS-2$
187
		File traceFile = OSGiTestsActivator.getContext().getDataFile(getName() + ".trace"); //$NON-NLS-1$
188
		debugOptions.setFile(traceFile);
189
190
		DebugTrace wrapped = debugOptions.newDebugTrace(getName(), TestDebugTrace.class);
191
		TestDebugTrace debugTrace = new TestDebugTrace(wrapped);
192
		debugTrace.trace("/debug", "testing 1"); //$NON-NLS-1$ //$NON-NLS-2$
193
		debugTrace.trace("/debug", "testing 2"); //$NON-NLS-1$ //$NON-NLS-2$
194
		debugTrace.trace("/notset", "testing 3"); //$NON-NLS-1$ //$NON-NLS-2$
195
		String[][] traceOutput = readTraceFile(traceFile);
196
		assertEquals("Wrong number of trace entries", 2, traceOutput.length); //$NON-NLS-1$
197
		assertEquals("Wrong entry length", 8, traceOutput[0].length); //$NON-NLS-1$
198
		assertEquals("Wrong message", "testing 1", traceOutput[0][7]); //$NON-NLS-1$ //$NON-NLS-2$
199
		assertEquals("Wrong entry length", 8, traceOutput[1].length); //$NON-NLS-1$
200
		assertEquals("Wrong message", "testing 2", traceOutput[1][7]); //$NON-NLS-1$ //$NON-NLS-2$
201
	}
202
203
	private String[][] readTraceFile(File traceFile) {
204
		ArrayList result = new ArrayList();
205
		BufferedReader br = null;
206
		try {
207
			br = new BufferedReader(new FileReader(traceFile));
208
			for (String line = br.readLine(); line != null; line = br.readLine()) {
209
				if (line.startsWith("#")) //$NON-NLS-1$
210
					continue;
211
				StringTokenizer st = new StringTokenizer(line, "|"); //$NON-NLS-1$
212
				int count = st.countTokens();
213
				String[] entry = new String[count];
214
				for (int i = 0; i < entry.length; i++)
215
					entry[i] = st.nextToken().trim();
216
				result.add(entry);
217
			}
218
		} catch (IOException e) {
219
			fail("Failed to read trace file", e); //$NON-NLS-1$
220
		} finally {
221
			if (br != null)
222
				try {
223
					br.close();
224
				} catch (IOException e) {
225
					// nothing;
226
				}
227
		}
228
		return (String[][]) result.toArray(new String[result.size()][]);
229
	}
230
231
	static class TestDebugTrace implements DebugTrace {
232
		private final DebugTrace wrapped;
233
234
		public TestDebugTrace(DebugTrace wrapped) {
235
			this.wrapped = wrapped;
236
		}
237
238
		public void trace(String option, String message) {
239
			wrapped.trace(option, message);
240
		}
241
242
		public void trace(String option, String message, Throwable error) {
243
			wrapped.trace(option, message, error);
244
		}
245
246
		public void traceDumpStack(String option) {
247
			wrapped.traceDumpStack(option);
248
		}
249
250
		public void traceEntry(String option) {
251
			wrapped.traceEntry(option);
252
		}
253
254
		public void traceEntry(String option, Object methodArgument) {
255
			wrapped.traceEntry(option, methodArgument);
256
		}
257
258
		public void traceEntry(String option, Object[] methodArguments) {
259
			wrapped.traceEntry(option, methodArguments);
260
		}
261
262
		public void traceExit(String option) {
263
			wrapped.traceExit(option);
264
		}
265
266
		public void traceExit(String option, Object result) {
267
			wrapped.traceExit(option, result);
268
		}
269
	}
270
179
	class TestDebugOptionsListener implements DebugOptionsListener {
271
	class TestDebugOptionsListener implements DebugOptionsListener {
180
		boolean called = false;
272
		boolean called = false;
181
		String incorrectValue;
273
		String incorrectValue;

Return to bug 285300