Bug 374268 - Use the DebugTrace object to print to trace files
diff --git a/org.eclipse.jdt.debug/eval/org/eclipse/jdt/internal/debug/eval/ast/engine/ASTEvaluationEngine.java b/org.eclipse.jdt.debug/eval/org/eclipse/jdt/internal/debug/eval/ast/engine/ASTEvaluationEngine.java
index 069a2a7..b7d9425 100644
--- a/org.eclipse.jdt.debug/eval/org/eclipse/jdt/internal/debug/eval/ast/engine/ASTEvaluationEngine.java
+++ b/org.eclipse.jdt.debug/eval/org/eclipse/jdt/internal/debug/eval/ast/engine/ASTEvaluationEngine.java
@@ -177,8 +177,7 @@
 			} catch (DebugException e) {
 				buf.append(thread.toString());
 			}
-			System.out.println(buf.toString());
-			System.out.flush();
+			JDIDebugOptions.trace(buf.toString());
 			Thread.dumpStack();
 		}
 	}
@@ -646,7 +645,7 @@
 				}
 				buf.append(" Hit Breakpoints: "); //$NON-NLS-1$
 				buf.append(fHitBreakpoints);
-				System.out.println(buf.toString());
+				JDIDebugOptions.trace(buf.toString());
 			}
 			EvaluationResult result = new EvaluationResult(
 					ASTEvaluationEngine.this, fExpression.getSnippet(), fThread);
@@ -666,7 +665,7 @@
 						buf.append("\t\t"); //$NON-NLS-1$
 						buf.append(errors[i]);
 					}
-					System.out.println(buf.toString());
+					JDIDebugOptions.trace(buf.toString());
 				}
 				return;
 			}
@@ -751,7 +750,7 @@
 					StringBuffer buf = new StringBuffer();
 					buf.append("\tException: "); //$NON-NLS-1$
 					buf.append(exception.toString());
-					System.out.println(buf.toString());
+					JDIDebugOptions.trace(buf.toString());
 				}
 				if (exception instanceof DebugException) {
 					result.setException((DebugException) exception);
@@ -766,7 +765,7 @@
 						StringBuffer buf = new StringBuffer();
 						buf.append("\tResult: "); //$NON-NLS-1$
 						buf.append(value);
-						System.out.println(buf.toString());
+						JDIDebugOptions.trace(buf.toString());
 					}
 				} else {
 					result.addError(EvaluationEngineMessages.ASTEvaluationEngine_An_unknown_error_occurred_during_evaluation);
@@ -778,7 +777,7 @@
 				buf.append("\tDuration: "); //$NON-NLS-1$
 				buf.append(end - start);
 				buf.append("ms"); //$NON-NLS-1$
-				System.out.println(buf.toString());
+				JDIDebugOptions.trace(buf.toString());
 			}
 
 			evaluationFinished(result);
diff --git a/org.eclipse.jdt.debug/jdi/org/eclipse/jdi/internal/MirrorImpl.java b/org.eclipse.jdt.debug/jdi/org/eclipse/jdi/internal/MirrorImpl.java
index ecdd3f8..2e1a641 100644
--- a/org.eclipse.jdt.debug/jdi/org/eclipse/jdi/internal/MirrorImpl.java
+++ b/org.eclipse.jdt.debug/jdi/org/eclipse/jdi/internal/MirrorImpl.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2010 IBM Corporation and others.
+ * Copyright (c) 2000, 2012 IBM Corporation 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
@@ -200,7 +200,7 @@
 			buf.append("ms"); //$NON-NLS-1$
 			buf.append(" length: "); //$NON-NLS-1$
 			buf.append(reply.getLength());
-			System.out.println(buf.toString());
+			JDIDebugOptions.trace(buf.toString());
 		}
 		if (fVerboseWriter != null) {
 			fVerboseWriter.println();
diff --git a/org.eclipse.jdt.debug/model/org/eclipse/jdt/internal/debug/core/EventDispatcher.java b/org.eclipse.jdt.debug/model/org/eclipse/jdt/internal/debug/core/EventDispatcher.java
index 38c4839..8fb3c07 100644
--- a/org.eclipse.jdt.debug/model/org/eclipse/jdt/internal/debug/core/EventDispatcher.java
+++ b/org.eclipse.jdt.debug/model/org/eclipse/jdt/internal/debug/core/EventDispatcher.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2011 IBM Corporation and others.
+ * Copyright (c) 2000, 2012 IBM Corporation 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
@@ -103,14 +103,15 @@
 		}
 		if (JDIDebugOptions.DEBUG_JDI_EVENTS) {
 			EventIterator eventIter = eventSet.eventIterator();
-			System.out.print("JDI Event Set: {"); //$NON-NLS-1$
+			StringBuffer buf = new StringBuffer("JDI Event Set: {\n"); //$NON-NLS-1$
 			while (eventIter.hasNext()) {
-				System.out.print(eventIter.next());
+				buf.append(eventIter.next());
 				if (eventIter.hasNext()) {
-					System.out.print(", "); //$NON-NLS-1$
+					buf.append(", "); //$NON-NLS-1$
 				}
 			}
-			System.out.println("}"); //$NON-NLS-1$
+			buf.append("}\n"); //$NON-NLS-1$
+			JDIDebugOptions.trace(buf.toString());
 		}
 		EventIterator iter = eventSet.eventIterator();
 		IJDIEventListener[] listeners = new IJDIEventListener[eventSet.size()];
diff --git a/org.eclipse.jdt.debug/model/org/eclipse/jdt/internal/debug/core/JDIDebugOptions.java b/org.eclipse.jdt.debug/model/org/eclipse/jdt/internal/debug/core/JDIDebugOptions.java
index 3b74518..35ccbb6 100644
--- a/org.eclipse.jdt.debug/model/org/eclipse/jdt/internal/debug/core/JDIDebugOptions.java
+++ b/org.eclipse.jdt.debug/model/org/eclipse/jdt/internal/debug/core/JDIDebugOptions.java
@@ -14,6 +14,7 @@
 
 import org.eclipse.osgi.service.debug.DebugOptions;
 import org.eclipse.osgi.service.debug.DebugOptionsListener;
+import org.eclipse.osgi.service.debug.DebugTrace;
 import org.osgi.framework.BundleContext;
 
 import com.ibm.icu.text.DateFormat;
@@ -39,6 +40,12 @@
 	public static boolean DEBUG_AST_EVAL_THREAD_TRACE = false;
 
 	/**
+	 * The {@link DebugTrace} object to print to OSGi tracing
+	 * @since 3.8
+	 */
+	private static DebugTrace fgDebugTrace;
+	
+	/**
 	 * Constructor
 	 */
 	public JDIDebugOptions(BundleContext context) {
@@ -54,10 +61,35 @@
 	 * @see org.eclipse.osgi.service.debug.DebugOptionsListener#optionsChanged(org.eclipse.osgi.service.debug.DebugOptions)
 	 */
 	public void optionsChanged(DebugOptions options) {
+		fgDebugTrace = options.newDebugTrace(JDIDebugPlugin.getUniqueIdentifier());
 		DEBUG = options.getBooleanOption(DEBUG_FLAG, false);
 		DEBUG_JDI_EVENTS = DEBUG && options.getBooleanOption(DEBUG_JDI_EVENTS_FLAG, false);
 		DEBUG_JDI_REQUEST_TIMES = DEBUG && options.getBooleanOption(DEBUG_JDI_REQUEST_TIMES_FLAG, false);
 		DEBUG_AST_EVAL = DEBUG && options.getBooleanOption(DEBUG_AST_EVALUATIONS_FLAG, false);
 		DEBUG_AST_EVAL_THREAD_TRACE = DEBUG && options.getBooleanOption(DEBUG_AST_EVALUATIONS_CALLING_THREADS_FLAG, false);
 	}
+	
+	/**
+	 * Prints the given message to System.out and to the OSGi tracing (if started)
+	 * @param option the option or <code>null</code>
+	 * @param message the message to print or <code>null</code>
+	 * @param throwable the {@link Throwable} or <code>null</code>
+	 * @since 3.8
+	 */
+	public static void trace(String option, String message, Throwable throwable) {
+		System.out.println(message);
+		if(fgDebugTrace != null) {
+			fgDebugTrace.trace(option, message, throwable);
+		}
+	}
+	
+	/**
+	 * Prints the given message to System.out and to the OSGi tracing (if enabled)
+	 * 
+	 * @param message the message or <code>null</code>
+	 * @since 3.8
+	 */
+	public static void trace(String message) {
+		trace(null, message, null);
+	}
 }
diff --git a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JREContainer.java b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JREContainer.java
index a7fdcdd..30213af 100644
--- a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JREContainer.java
+++ b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JREContainer.java
@@ -257,7 +257,7 @@
 			}
 		} else {
 			if (LaunchingPlugin.DEBUG_JRE_CONTAINER) {
-				System.out.println("\tEE:\t" + id); //$NON-NLS-1$
+				LaunchingPlugin.trace("\tEE:\t" + id); //$NON-NLS-1$
 			}
 			// dynamically compute entries when bound to an EE
 			entries = computeClasspathEntries(vm, project, id);
@@ -352,14 +352,14 @@
 	 */
 	public IClasspathEntry[] getClasspathEntries() {
 		if (LaunchingPlugin.DEBUG_JRE_CONTAINER) {
-			System.out.println("<JRE_CONTAINER> getClasspathEntries() " + this.toString()); //$NON-NLS-1$
-			System.out.println("\tJRE:\t" + fVMInstall.getName()); //$NON-NLS-1$
-			System.out.println("\tPath:\t" + getPath().toString()); //$NON-NLS-1$
-			System.out.println("\tProj:\t" + fProject.getProject().getName()); //$NON-NLS-1$
+			LaunchingPlugin.trace("<JRE_CONTAINER> getClasspathEntries() " + this.toString()); //$NON-NLS-1$
+			LaunchingPlugin.trace("\tJRE:\t" + fVMInstall.getName()); //$NON-NLS-1$
+			LaunchingPlugin.trace("\tPath:\t" + getPath().toString()); //$NON-NLS-1$
+			LaunchingPlugin.trace("\tProj:\t" + fProject.getProject().getName()); //$NON-NLS-1$
 		}
 		IClasspathEntry[] entries = getClasspathEntries(fVMInstall, getPath(), fProject);
 		if (LaunchingPlugin.DEBUG_JRE_CONTAINER) {
-			System.out.println("\tResolved " + entries.length + " entries:");  //$NON-NLS-1$//$NON-NLS-2$
+			LaunchingPlugin.trace("\tResolved " + entries.length + " entries:");  //$NON-NLS-1$//$NON-NLS-2$
 		}
 		return entries;
 	}
diff --git a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JREContainerInitializer.java b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JREContainerInitializer.java
index 5da716e..4bf36eb 100644
--- a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JREContainerInitializer.java
+++ b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JREContainerInitializer.java
@@ -47,9 +47,9 @@
 	@Override
 	public void initialize(IPath containerPath, IJavaProject project) throws CoreException {
 		if (LaunchingPlugin.DEBUG_JRE_CONTAINER) {
-			System.out.println("<JRE_CONTAINER> initialize()"); //$NON-NLS-1$
-			System.out.println("\tPath: " + containerPath.toString()); //$NON-NLS-1$
-			System.out.println("\tProj: " + project.getProject().getName()); //$NON-NLS-1$
+			LaunchingPlugin.trace("<JRE_CONTAINER> initialize()"); //$NON-NLS-1$
+			LaunchingPlugin.trace("\tPath: " + containerPath.toString()); //$NON-NLS-1$
+			LaunchingPlugin.trace("\tProj: " + project.getProject().getName()); //$NON-NLS-1$
 		}
 		int size = containerPath.segmentCount();
 		if (size > 0) {
@@ -58,23 +58,23 @@
 				JREContainer container = null;
 				if (vm != null) {
 					if (LaunchingPlugin.DEBUG_JRE_CONTAINER) {
-						System.out.println("\tResolved VM: " + vm.getName()); //$NON-NLS-1$
+						LaunchingPlugin.trace("\tResolved VM: " + vm.getName()); //$NON-NLS-1$
 					}
 					container = new JREContainer(vm, containerPath, project);
 				} else {
 					if (LaunchingPlugin.DEBUG_JRE_CONTAINER) {
-						System.out.println("\t*** FAILED RESOLVE VM ***"); //$NON-NLS-1$
+						LaunchingPlugin.trace("\t*** FAILED RESOLVE VM ***"); //$NON-NLS-1$
 					}
 				}
 				JavaCore.setClasspathContainer(containerPath, new IJavaProject[] {project}, new IClasspathContainer[] {container}, null);
 			} else {
 				if (LaunchingPlugin.DEBUG_JRE_CONTAINER) {
-					System.out.println("\t*** INVALID JRE CONTAINER PATH ***"); //$NON-NLS-1$
+					LaunchingPlugin.trace("\t*** INVALID JRE CONTAINER PATH ***"); //$NON-NLS-1$
 				}	
 			}
 		} else {
 			if (LaunchingPlugin.DEBUG_JRE_CONTAINER) {
-				System.out.println("\t*** NO SEGMENTS IN CONTAINER PATH ***"); //$NON-NLS-1$
+				LaunchingPlugin.trace("\t*** NO SEGMENTS IN CONTAINER PATH ***"); //$NON-NLS-1$
 			}
 		}
 	}
@@ -95,25 +95,25 @@
 				IClasspathContainer[] containers = new JREContainer[length];
 				if (vm != null) {
 					if (LaunchingPlugin.DEBUG_JRE_CONTAINER) {
-						System.out.println("\tResolved VM: " + vm.getName()); //$NON-NLS-1$
+						LaunchingPlugin.trace("\tResolved VM: " + vm.getName()); //$NON-NLS-1$
 					}
 					for (int i=0; i<length; i++) {
 						containers[i] = new JREContainer(vm, containerPath, projects[i]);
 					}
 				} else {
 					if (LaunchingPlugin.DEBUG_JRE_CONTAINER) {
-						System.out.println("\t*** FAILED RESOLVE VM ***"); //$NON-NLS-1$
+						LaunchingPlugin.trace("\t*** FAILED RESOLVE VM ***"); //$NON-NLS-1$
 					}
 				}
 				JavaCore.setClasspathContainer(containerPath, projects, containers, null);
 			} else {
 				if (LaunchingPlugin.DEBUG_JRE_CONTAINER) {
-					System.out.println("\t*** INVALID JRE CONTAINER PATH ***"); //$NON-NLS-1$
+					LaunchingPlugin.trace("\t*** INVALID JRE CONTAINER PATH ***"); //$NON-NLS-1$
 				}	
 			}
 		} else {
 			if (LaunchingPlugin.DEBUG_JRE_CONTAINER) {
-				System.out.println("\t*** NO SEGMENTS IN CONTAINER PATH ***"); //$NON-NLS-1$
+				LaunchingPlugin.trace("\t*** NO SEGMENTS IN CONTAINER PATH ***"); //$NON-NLS-1$
 			}
 		}
 	}
@@ -131,8 +131,8 @@
 			String id = getExecutionEnvironmentId(containerPath);
 			if (id != null) {
 				if (LaunchingPlugin.DEBUG_JRE_CONTAINER) {
-					System.out.println("<JRE_CONTAINER> resolveVM(IPath)"); //$NON-NLS-1$
-					System.out.println("\tEE: " + id); //$NON-NLS-1$
+					LaunchingPlugin.trace("<JRE_CONTAINER> resolveVM(IPath)"); //$NON-NLS-1$
+					LaunchingPlugin.trace("\tEE: " + id); //$NON-NLS-1$
 				}
 				IExecutionEnvironmentsManager manager = JavaRuntime.getExecutionEnvironmentsManager();
 				IExecutionEnvironment environment = manager.getEnvironment(id);
@@ -140,7 +140,7 @@
 					vm = resolveVM(environment);
 				} else {
 					if (LaunchingPlugin.DEBUG_JRE_CONTAINER) {
-						System.out.println("\t*** NO ENVIRONMENT ***"); //$NON-NLS-1$
+						LaunchingPlugin.trace("\t*** NO ENVIRONMENT ***"); //$NON-NLS-1$
 					}
 				}
 			} else {
@@ -168,21 +168,21 @@
 	 */
 	public static IVMInstall resolveVM(IExecutionEnvironment environment) {
 		if (LaunchingPlugin.DEBUG_JRE_CONTAINER) {
-			System.out.println("<JRE_CONTAINER> resolveVM(IExecutionEnvironment)"); //$NON-NLS-1$
+			LaunchingPlugin.trace("<JRE_CONTAINER> resolveVM(IExecutionEnvironment)"); //$NON-NLS-1$
 		}
 		IVMInstall vm = environment.getDefaultVM();
 		if (vm == null) {
 			IVMInstall[] installs = environment.getCompatibleVMs();
 			// take the first strictly compatible VM if there is no default
 			if (installs.length == 0 && LaunchingPlugin.DEBUG_JRE_CONTAINER) {
-				System.out.println("\t*** NO COMPATIBLE VMS ***"); //$NON-NLS-1$
+				LaunchingPlugin.trace("\t*** NO COMPATIBLE VMS ***"); //$NON-NLS-1$
 			}
 			for (int i = 0; i < installs.length; i++) {
 				IVMInstall install = installs[i];
 				if (environment.isStrictlyCompatible(install)) {
 					vm = install;
 					if (LaunchingPlugin.DEBUG_JRE_CONTAINER) {
-						System.out.println("\tPerfect Match: " + vm.getName()); //$NON-NLS-1$
+						LaunchingPlugin.trace("\tPerfect Match: " + vm.getName()); //$NON-NLS-1$
 					}
 					break;
 				}
@@ -195,12 +195,12 @@
 			if (vm == null && installs.length > 0) {
 				vm = installs[0];
 				if (LaunchingPlugin.DEBUG_JRE_CONTAINER) {
-					System.out.println("\tFirst Match: " + vm.getName()); //$NON-NLS-1$
+					LaunchingPlugin.trace("\tFirst Match: " + vm.getName()); //$NON-NLS-1$
 				}
 			}
 		} else {
 			if (LaunchingPlugin.DEBUG_JRE_CONTAINER) {
-				System.out.println("\tUser Default VM: " + vm.getName()); //$NON-NLS-1$
+				LaunchingPlugin.trace("\tUser Default VM: " + vm.getName()); //$NON-NLS-1$
 			}
 		}
 		return vm;
diff --git a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/LaunchingPlugin.java b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/LaunchingPlugin.java
index 806cce9..7f28536 100644
--- a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/LaunchingPlugin.java
+++ b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/LaunchingPlugin.java
@@ -78,6 +78,7 @@
 import org.eclipse.jdt.launching.sourcelookup.ArchiveSourceLocation;
 import org.eclipse.osgi.service.debug.DebugOptions;
 import org.eclipse.osgi.service.debug.DebugOptionsListener;
+import org.eclipse.osgi.service.debug.DebugTrace;
 import org.eclipse.osgi.util.NLS;
 import org.osgi.framework.BundleContext;
 import org.osgi.service.prefs.BackingStoreException;
@@ -102,6 +103,12 @@
 	public static final String DEBUG_FLAG = "org.eclipse.jdt.launching/debug"; //$NON-NLS-1$
 
 	/**
+	 * The {@link DebugTrace} object to print to OSGi tracing
+	 * @since 3.8
+	 */
+	private static DebugTrace fgDebugTrace;
+	
+	/**
 	 * The id of the JDT launching plug-in (value <code>"org.eclipse.jdt.launching"</code>).
 	 */
 	public static final String ID_PLUGIN= "org.eclipse.jdt.launching"; //$NON-NLS-1$
@@ -1262,4 +1269,28 @@
 		DEBUG = options.getBooleanOption(DEBUG_FLAG, false);
 		DEBUG_JRE_CONTAINER = DEBUG && options.getBooleanOption(DEBUG_JRE_CONTAINER_FLAG, false);
 	}
+	
+	/**
+	 * Prints the given message to System.out and to the OSGi tracing (if started)
+	 * @param option the option or <code>null</code>
+	 * @param message the message to print or <code>null</code>
+	 * @param throwable the {@link Throwable} or <code>null</code>
+	 * @since 3.8
+	 */
+	public static void trace(String option, String message, Throwable throwable) {
+		System.out.println(message);
+		if(fgDebugTrace != null) {
+			fgDebugTrace.trace(option, message, throwable);
+		}
+	}
+	
+	/**
+	 * Prints the given message to System.out and to the OSGi tracing (if enabled)
+	 * 
+	 * @param message the message or <code>null</code>
+	 * @since 3.8
+	 */
+	public static void trace(String message) {
+		trace(null, message, null);
+	}
 }
\ No newline at end of file