From 0ed38ed1f4d30d8eaaba6aa8b6236c55f33d8719 Mon Sep 17 00:00:00 2001 From: Enrico Forestieri Date: Wed, 13 May 2009 12:55:01 +0000 Subject: [PATCH] Allow to separately redirect stdout and stderr. Now "lyx > stdout.log" should produce the same output as before the switch to QProcess. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@29655 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/support/Systemcall.cpp | 29 ++++++++++++++++++----------- src/support/os.h | 11 ++++++++--- src/support/os_cygwin.cpp | 4 ++-- src/support/os_unix.cpp | 4 ++-- src/support/os_win32.cpp | 2 +- 5 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/support/Systemcall.cpp b/src/support/Systemcall.cpp index f1db006a8e..b7f0355fc6 100644 --- a/src/support/Systemcall.cpp +++ b/src/support/Systemcall.cpp @@ -60,12 +60,15 @@ int Systemcall::startscript(Starttype how, string const & what) #else QString cmd = QString::fromLocal8Bit(what.c_str()); QProcess * process = new QProcess; - if (os::terminal_output()) { - // Qt won't start the process if we redirect stdout and - // stderr this way, without running in a terminal. + + // Qt won't start the process if we redirect stdout/stderr in + // this way and they are not connected to a terminal (maybe + // because we were launched from some desktop GUI). + if (os::is_terminal(os::STDOUT)) process->setStandardOutputFile(toqstr(os::stdoutdev())); + if (os::is_terminal(os::STDERR)) process->setStandardErrorFile(toqstr(os::stderrdev())); - } + process->start(cmd); if (!process->waitForStarted(3000)) { LYXERR0("Qprocess " << cmd << " did not start!"); @@ -92,13 +95,17 @@ int Systemcall::startscript(Starttype how, string const & what) LYXERR0("state " << process->state()); LYXERR0("status " << process->exitStatus()); } - if (!os::terminal_output()) { - // The output may have been redirected. But even if we are not - // running in a terminal, the output could go to some log file, - // for example ~/.xsession-errors on *nix. - cout << fromqstr(QString::fromLocal8Bit(process->readAllStandardOutput().data())) << endl; - cerr << fromqstr(QString::fromLocal8Bit(process->readAllStandardError().data())) << endl; - } + + // If the output has been redirected, we write it all at once. + // Even if we are not running in a terminal, the output could go + // to some log file, for example ~/.xsession-errors on *nix. + if (!os::is_terminal(os::STDOUT)) + cout << fromqstr(QString::fromLocal8Bit( + process->readAllStandardOutput().data())); + if (!os::is_terminal(os::STDERR)) + cerr << fromqstr(QString::fromLocal8Bit( + process->readAllStandardError().data())); + killProcess(process); return exit_code; #endif diff --git a/src/support/os.h b/src/support/os.h index b5d1fecd80..1644ef4bf0 100644 --- a/src/support/os.h +++ b/src/support/os.h @@ -27,6 +27,12 @@ enum shell_type { CMD_EXE }; +enum io_channel { + STDIN = 0, + STDOUT, + STDERR +}; + /// Do some work just once. void init(int argc, char * argv[]); @@ -39,9 +45,8 @@ std::string const & stdoutdev(); /// Returns the name of the stderr device (/dev/stderr, /dev/tty, conout$). std::string const & stderrdev(); -/// Tells whether LyX is being run from a terminal and stdout/stderr are -/// not redirected. -bool terminal_output(); +/// Tells whether \p channel is connected to a terminal or not. +bool is_terminal(io_channel channel); /// Returns "/" on *nix, "C:/", etc on Windows. std::string current_root(); diff --git a/src/support/os_cygwin.cpp b/src/support/os_cygwin.cpp index 840a11ccc1..068d7035d6 100644 --- a/src/support/os_cygwin.cpp +++ b/src/support/os_cygwin.cpp @@ -254,9 +254,9 @@ string const & stderrdev() } -bool terminal_output() +bool is_terminal(io_channel channel) { - return isatty(1) && isatty(2); + return isatty(channel); } diff --git a/src/support/os_unix.cpp b/src/support/os_unix.cpp index ccc04b69e2..a9ae59850c 100644 --- a/src/support/os_unix.cpp +++ b/src/support/os_unix.cpp @@ -157,9 +157,9 @@ string const & stderrdev() } -bool terminal_output() +bool is_terminal(io_channel channel) { - return isatty(1) && isatty(2); + return isatty(channel); } diff --git a/src/support/os_win32.cpp b/src/support/os_win32.cpp index f17f06de5f..464d8df111 100644 --- a/src/support/os_win32.cpp +++ b/src/support/os_win32.cpp @@ -310,7 +310,7 @@ string const & stderrdev() } -bool terminal_output() +bool is_terminal(io_channel channel) { // FIXME: Passing conout$ to Qt fails, most probably for the // reason explained here: -- 2.39.2