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
This commit is contained in:
Enrico Forestieri 2009-05-13 12:55:01 +00:00
parent 3cdf2acb95
commit 0ed38ed1f4
5 changed files with 31 additions and 19 deletions

View File

@ -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

View File

@ -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();

View File

@ -254,9 +254,9 @@ string const & stderrdev()
}
bool terminal_output()
bool is_terminal(io_channel channel)
{
return isatty(1) && isatty(2);
return isatty(channel);
}

View File

@ -157,9 +157,9 @@ string const & stderrdev()
}
bool terminal_output()
bool is_terminal(io_channel channel)
{
return isatty(1) && isatty(2);
return isatty(channel);
}

View File

@ -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: