Index: /trunk/src/corelib/io/qprocess_os2.cpp
===================================================================
--- /trunk/src/corelib/io/qprocess_os2.cpp	(revision 698)
+++ /trunk/src/corelib/io/qprocess_os2.cpp	(revision 699)
@@ -43,5 +43,4 @@
 
 //#define QPROCESS_DEBUG
-#include "qdebug.h"
 
 #if defined(__INNOTEK_LIBC__)
@@ -52,4 +51,5 @@
 
 #if defined QPROCESS_DEBUG
+#include "qdebug.h"
 #include "qstring.h"
 #include <ctype.h>
@@ -86,5 +86,24 @@
     return out;
 }
+
+// redirect qDebug output to a file in the root directory of the current drive
+static void msgHandler(QtMsgType, const char *msg)
+{
+    static FILE *f = 0;
+    if (!f) {
+        f = fopen("\\qprocess.dbg", "w");
+        setbuf(f, NULL);
+    }
+    fprintf(f, "%s\n", msg);
+}
+
+static int setMsgHandler() { qInstallMsgHandler(msgHandler); return 0; }
+Q_CONSTRUCTOR_FUNCTION(setMsgHandler)
+
 QT_END_NAMESPACE
+
+#define DEBUG(a) qDebug a
+#else
+#define DEBUG(a) do {} while(0)
 #endif
 
@@ -246,7 +265,5 @@
 USHORT QProcessManager::addProcess(QProcess *process)
 {
-#if defined (QPROCESS_DEBUG)
-    qDebug("QProcessManager::addProcess(%p)", process);
-#endif
+    DEBUG(("QProcessManager::addProcess(%p)", process));
 
     QMutexLocker locker(&mutex);
@@ -317,7 +334,5 @@
     QProcess *process = instance->processes[procKey];
 
-#if defined (QPROCESS_DEBUG)
-    qDebug("QProcessManager::removeProcess(%p)", process);
-#endif
+    DEBUG(("QProcessManager::removeProcess(%p)", process));
 
     // to guarantee that the given procKey may be reused, we must close all
@@ -339,7 +354,5 @@
     : refcnt(0), finish(false), eventSem(NULLHANDLE), sa_old_sigchld_handler(0)
 {
-#if defined (QPROCESS_DEBUG)
-    qDebug() << "QProcessManager::QProcessManager()";
-#endif
+    DEBUG(() << "QProcessManager::QProcessManager()");
 
     APIRET rc = DosCreateEventSem(NULL, &eventSem,
@@ -354,7 +367,5 @@
 QProcessManager::~QProcessManager()
 {
-#if defined (QPROCESS_DEBUG)
-    qDebug() << "QProcessManager::~QProcessManager()";
-#endif
+    DEBUG(() << "QProcessManager::~QProcessManager()");
 
     Q_ASSERT(!refcnt);
@@ -393,7 +404,5 @@
 void QProcessManager::run()
 {
-#if defined (QPROCESS_DEBUG)
-    qDebug() << "QProcessManager::run() BEGIN";
-#endif
+    DEBUG(() << "QProcessManager::run() BEGIN");
 
     // Note: the rationale behind using a worker thread so far is that
@@ -419,7 +428,5 @@
 
         if (instance->deathFlag.testAndSetRelaxed(1, 0)) {
-#if defined (QPROCESS_DEBUG)
-            qDebug() << "QProcessManager::run(): child death signaled";
-#endif
+            DEBUG(() << "QProcessManager::run(): child death signaled");
             foreach (QProcess *proc, processes) {
                 QProcessPrivate::WaitMode mode = (QProcessPrivate::WaitMode)
@@ -442,7 +449,5 @@
             continue;
 
-#if defined (QPROCESS_DEBUG)
-        qDebug() << "QProcessManager::run(): checking pipes";
-#endif
+        DEBUG(() << "QProcessManager::run(): checking pipes");
 
         // make sure the state array is big enough. The best size for sz pipes
@@ -477,10 +482,8 @@
             if (pass == 1 && status != NPSS_CLOSE)
                 continue;
-#if defined(QPROCESS_DEBUG)
-            qDebug(" %d/%d: fStatus %u fFlag f%02X usKey %04hX usAvail %hu",
+            DEBUG((" %d/%d: fStatus %u fFlag f%02X usKey %04hX usAvail %hu",
                    pass, i, (uint) pipeStates[i].fStatus,
                    (uint) pipeStates[i].fFlag, pipeStates[i].usKey,
-                   pipeStates[i].usAvail);
-#endif
+                   pipeStates[i].usAvail));
             int procKey = toProcKey(pipeStates[i].usKey);
             QProcessPrivate::PipeType type = toPipeType(pipeStates[i].usKey);
@@ -488,7 +491,5 @@
             QProcess *proc = processes[procKey];
             Q_ASSERT(proc);
-#if defined(QPROCESS_DEBUG)
-            qDebug("  process %p", proc);
-#endif
+            DEBUG(("  process %p", proc));
             QProcessPrivate *d = proc->d_func();
 
@@ -538,7 +539,5 @@
     ::free(pipeStates);
 
-#if defined (QPROCESS_DEBUG)
-    qDebug() << "QProcessManager::run() END";
-#endif
+    DEBUG(() << "QProcessManager::run() END");
 }
 
@@ -830,7 +829,5 @@
     }
 
-#if defined(QPROCESS_DEBUG)
-    qDebug("qt_startProcess: workdir is \"%s\"", qPrintable(QDir::currentPath()));
-#endif
+    DEBUG(("qt_startProcess: workdir is \"%s\"", qPrintable(QDir::currentPath())));
 
     // try to find the executable
@@ -883,9 +880,7 @@
     }
 
-#if defined(QPROCESS_DEBUG)
-    qDebug("qt_startProcess: found \"%s\" for \"%s\"",
+    DEBUG(("qt_startProcess: found \"%s\" for \"%s\"",
            qPrintable(QFile::decodeName(fullProgramName)),
-           qPrintable(QFile::decodeName(programName)));
-#endif
+           qPrintable(QFile::decodeName(programName))));
 
     int pid = -1;
@@ -910,13 +905,9 @@
             paths = path.split(QLatin1Char(';'), QString::SkipEmptyParts);
             if (paths.contains(fullPath, Qt::CaseInsensitive)) {
-#if defined(QPROCESS_DEBUG)
-                qDebug("qt_startProcess: \"%s\" is already in BEGINLIBPATH",
-                       qPrintable(fullPath));
-#endif
+                DEBUG(("qt_startProcess: \"%s\" is already in BEGINLIBPATH",
+                       qPrintable(fullPath)));
             } else {
-#if defined(QPROCESS_DEBUG)
-                qDebug("qt_startProcess: prepending \"%s\" to BEGINLIBPATH",
-                       qPrintable(fullPath));
-#endif
+                DEBUG(("qt_startProcess: prepending \"%s\" to BEGINLIBPATH",
+                       qPrintable(fullPath)));
                 prependedLibPath = true;
                 QByteArray newLibPath = libPathBuf;
@@ -961,9 +952,9 @@
 
 #if defined(QPROCESS_DEBUG)
-        qDebug("qt_startProcess: executable \"%s\"",
-               qPrintable(QFile::decodeName(programReal)));
+        DEBUG(("qt_startProcess: executable \"%s\"",
+               qPrintable(QFile::decodeName(programReal))));
         for (int i = 0; argvReal[i]; ++i) {
-            qDebug("qt_startProcess:  arg[%d] \"%s\"",
-                   i, qPrintable(QFile::decodeName(argvReal[i])));
+            DEBUG(("qt_startProcess:  arg[%d] \"%s\"",
+                   i, qPrintable(QFile::decodeName(argvReal[i]))));
         }
 #endif
@@ -1042,7 +1033,5 @@
             ULONG ulSid, ulPidDummy;
             arc = DosStartSession(&data, &ulSid, &ulPidDummy);
-#if defined(QPROCESS_DEBUG)
-            qDebug("qt_startProcess: DosStartSession() returned %ld", arc);
-#endif
+            DEBUG(("qt_startProcess: DosStartSession() returned %ld", arc));
             // Note: for SSF_RELATED_INDEPENDENT, PID of the started process is
             // unknown, return 0 to indicate this
@@ -1062,7 +1051,5 @@
         }
 
-#if defined(QPROCESS_DEBUG)
-        qDebug("qt_startProcess: pid %d", pid);
-#endif
+        DEBUG(("qt_startProcess: pid %d", pid));
 
         if (prependedLibPath) {
@@ -1092,7 +1079,5 @@
     Q_Q(QProcess);
 
-#if defined (QPROCESS_DEBUG)
-    qDebug("QProcessPrivate::startProcess");
-#endif
+    DEBUG(("QProcessPrivate::startProcess"));
 
     // Initialize pipes
@@ -1143,8 +1128,6 @@
         }
         if (arc != NO_ERROR) {
-#if defined (QPROCESS_DEBUG)
-            qDebug("QProcessPrivate::startProcess: DosDupHandle for STDIN "
-                   "returned %lu", arc);
-#endif
+            DEBUG(("QProcessPrivate::startProcess: DosDupHandle for STDIN "
+                   "returned %lu", arc));
             break;
         }
@@ -1168,8 +1151,6 @@
             }
             if (arc != NO_ERROR) {
-#if defined (QPROCESS_DEBUG)
-                qDebug("QProcessPrivate::startProcess: DosDupHandle for STDOUT "
-                       "returned %lu", arc);
-#endif
+                DEBUG(("QProcessPrivate::startProcess: DosDupHandle for STDOUT "
+                       "returned %lu", arc));
                 break;
             }
@@ -1179,8 +1160,5 @@
                     arc = DosDupHandle(realStdout, &realStderr);
                 } else {
-                    // qDebug() uses STDERR so only redirect if !QPROCESS_DEBUG
-#if !defined (QPROCESS_DEBUG)
                     arc = DosDupHandle(stderrChannel.pipe.client, &realStderr);
-#endif
                 }
             }
@@ -1221,7 +1199,5 @@
                                                 .arg(arc)));
         } else {
-#if defined (QPROCESS_DEBUG)
-            qDebug("spawnvpe failed: %s", qPrintable(qt_error_string(errno)));
-#endif
+            DEBUG(("spawnvpe failed: %s", qPrintable(qt_error_string(errno))));
             q->setErrorString(QProcess::tr("Process failed to start: %1")
                                            .arg(qt_error_string(errno)));
@@ -1317,8 +1293,6 @@
         that->pipeData[OutPipe].bytesLeft = newBytes;
 
-#if defined (QPROCESS_DEBUG)
-    qDebug("QProcessPrivate::bytesAvailableFromStdout() == %lld",
-           that->pipeData[OutPipe].bytesLeft);
-#endif
+    DEBUG(("QProcessPrivate::bytesAvailableFromStdout() == %lld",
+           that->pipeData[OutPipe].bytesLeft));
     return that->pipeData[OutPipe].bytesLeft;
 }
@@ -1352,8 +1326,6 @@
         that->pipeData[ErrPipe].bytesLeft = newBytes;
 
-#if defined (QPROCESS_DEBUG)
-    qDebug("QProcessPrivate::bytesAvailableFromStderr() == %lld",
-           that->pipeData[ErrPipe].bytesLeft);
-#endif
+    DEBUG(("QProcessPrivate::bytesAvailableFromStderr() == %lld",
+           that->pipeData[ErrPipe].bytesLeft));
     return that->pipeData[ErrPipe].bytesLeft;
 }
@@ -1372,8 +1344,6 @@
     }
 
-#if defined QPROCESS_DEBUG
-    qDebug("QProcessPrivate::readFromStdout(%p \"%s\", %lld) == %lld",
-           data, qt_prettyDebug(data, bytesRead, 16).constData(), maxlen, bytesRead);
-#endif
+    DEBUG(("QProcessPrivate::readFromStdout(%p \"%s\", %lld) == %lld",
+           data, qt_prettyDebug(data, bytesRead, 16).constData(), maxlen, bytesRead));
     return bytesRead;
 }
@@ -1392,8 +1362,6 @@
     }
 
-#if defined QPROCESS_DEBUG
-    qDebug("QProcessPrivate::readFromStderr(%p \"%s\", %lld) == %lld",
-           data, qt_prettyDebug(data, bytesRead, 16).constData(), maxlen, bytesRead);
-#endif
+    DEBUG(("QProcessPrivate::readFromStderr(%p \"%s\", %lld) == %lld",
+           data, qt_prettyDebug(data, bytesRead, 16).constData(), maxlen, bytesRead));
     return bytesRead;
 }
@@ -1408,8 +1376,6 @@
         that->pipeData[InPipe].bytesLeft = newBytes;
 
-#if defined (QPROCESS_DEBUG)
-    qDebug("QProcessPrivate::writeToStdin(): %lld free bytes in pipe",
-           that->pipeData[InPipe].bytesLeft);
-#endif
+    DEBUG(("QProcessPrivate::writeToStdin(): %lld free bytes in pipe",
+           that->pipeData[InPipe].bytesLeft));
     if (!that->pipeData[InPipe].bytesLeft)
         return 0;
@@ -1426,8 +1392,6 @@
     }
 
-#if defined QPROCESS_DEBUG
-    qDebug("QProcessPrivate::writeToStdin(%p \"%s\", %lld) == %lld",
-           data, qt_prettyDebug(data, maxlen, 16).constData(), maxlen, written);
-#endif
+    DEBUG(("QProcessPrivate::writeToStdin(%p \"%s\", %lld) == %lld",
+           data, qt_prettyDebug(data, maxlen, 16).constData(), maxlen, written));
     return written;
 }
@@ -1435,7 +1399,5 @@
 void QProcessPrivate::terminateProcess()
 {
-#if defined (QPROCESS_DEBUG)
-    qDebug("QProcessPrivate::terminateProcess()");
-#endif
+    DEBUG(("QProcessPrivate::terminateProcess()"));
     if (pid) {
         HSWITCH hswitch = WinQuerySwitchHandle(NULL, pid);
@@ -1466,7 +1428,5 @@
 void QProcessPrivate::killProcess()
 {
-#if defined (QPROCESS_DEBUG)
-    qDebug("QProcessPrivate::killProcess()");
-#endif
+    DEBUG(("QProcessPrivate::killProcess()"));
     if (pid)
         DosKillProcess(DKP_PROCESS, pid);
@@ -1504,7 +1464,5 @@
 {
     Q_Q(QProcess);
-#if defined (QPROCESS_DEBUG)
-    qDebug("QProcessPrivate::waitForReadyRead(%d)", msecs);
-#endif
+    DEBUG(("QProcessPrivate::waitForReadyRead(%d)", msecs));
 
     QTime stopWatch;
@@ -1570,7 +1528,5 @@
 {
     Q_Q(QProcess);
-#if defined (QPROCESS_DEBUG)
-    qDebug("QProcessPrivate::waitForBytesWritten(%d)", msecs);
-#endif
+    DEBUG(("QProcessPrivate::waitForBytesWritten(%d)", msecs));
 
     QTime stopWatch;
@@ -1629,7 +1585,5 @@
 {
     Q_Q(QProcess);
-#if defined (QPROCESS_DEBUG)
-    qDebug("QProcessPrivate::waitForFinished(%d)", msecs);
-#endif
+    DEBUG(("QProcessPrivate::waitForFinished(%d)", msecs));
 
     QTime stopWatch;
@@ -1724,14 +1678,12 @@
         crashed = !WIFEXITED(exitStatus);
         exitCode = WEXITSTATUS(exitStatus);
+        DEBUG(() << "QProcessPrivate::waitForDeadChild() dead with exitCode"
+                 << exitCode << ", crashed?" << crashed);
+        return true;
+    }
 #if defined QPROCESS_DEBUG
-        qDebug() << "QProcessPrivate::waitForDeadChild() dead with exitCode"
-                 << exitCode << ", crashed?" << crashed;
-#endif
-        return true;
-    }
-#if defined QPROCESS_DEBUG
-    qDebug() << "QProcessPrivate::waitForDeadChild() not dead!";
+    DEBUG(() << "QProcessPrivate::waitForDeadChild() not dead!");
     if (waitResult == -1)
-        qDebug() << "QProcessPrivate::waitForDeadChild()" << strerror(errno);
+        DEBUG(() << "QProcessPrivate::waitForDeadChild()" << strerror(errno));
 #endif
     return false;
