more cleanup without behavior changes

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@32324 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Peter Kümmel 2009-12-04 10:11:06 +00:00
parent a0bdf5e666
commit 314b79576e
2 changed files with 65 additions and 43 deletions

View File

@ -48,17 +48,6 @@ using namespace std;
namespace lyx { namespace lyx {
namespace support { namespace support {
static void killProcess(QProcess * p)
{
p->disconnect();
p->closeReadChannel(QProcess::StandardOutput);
p->closeReadChannel(QProcess::StandardError);
p->close();
delete p;
}
// Reuse of instance // Reuse of instance
@ -114,19 +103,10 @@ int Systemcall::startscript(Starttype how, string const & what)
{ {
string outfile; string outfile;
QString cmd = toqstr(parsecmd(what, outfile)); QString cmd = toqstr(parsecmd(what, outfile));
QProcess * process = new QProcess; SystemcallPrivate d(outfile);
SystemcallPrivate d(process);
if (!outfile.empty()) {
// Check whether we have to simply throw away the output.
if (outfile != os::nulldev())
process->setStandardOutputFile(toqstr(outfile));
} else if (os::is_terminal(os::STDOUT))
d.showout();
if (os::is_terminal(os::STDERR))
d.showerr();
bool processEvents = false; bool processEvents = false;
d.startProcess(cmd); d.startProcess(cmd);
if (!d.waitWhile(SystemcallPrivate::Starting, processEvents, 3000)) { if (!d.waitWhile(SystemcallPrivate::Starting, processEvents, 3000)) {
LYXERR0("QProcess " << cmd << " did not start!"); LYXERR0("QProcess " << cmd << " did not start!");
@ -146,36 +126,37 @@ int Systemcall::startscript(Starttype how, string const & what)
return 20; return 20;
} }
int const exit_code = process->exitCode(); int const exit_code = d.exitCode();
if (exit_code) { if (exit_code) {
LYXERR0("QProcess " << cmd << " finished!"); LYXERR0("QProcess " << cmd << " finished!");
LYXERR0("error " << exit_code << ": " << d.errorMessage()); LYXERR0("error " << exit_code << ": " << d.errorMessage());
} }
// If the output has been redirected, we write it all at once. d.flush();
// Even if we are not running in a terminal, the output could go d.killProcess();
// to some log file, for example ~/.xsession-errors on *nix.
if (!os::is_terminal(os::STDOUT) && outfile.empty())
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; return exit_code;
} }
SystemcallPrivate::SystemcallPrivate(QProcess * proc) : proc_(proc), outindex_(0), SystemcallPrivate::SystemcallPrivate(const std::string& of) :
errindex_(0), showout_(false), showerr_(false) proc_(new QProcess), outindex_(0),
errindex_(0), showout_(false), showerr_(false), outfile(of)
{ {
connect(proc, SIGNAL(readyReadStandardOutput()), SLOT(stdOut())); if (!outfile.empty()) {
connect(proc, SIGNAL(readyReadStandardError()), SLOT(stdErr())); // Check whether we have to simply throw away the output.
connect(proc, SIGNAL(error(QProcess::ProcessError)), SLOT(processError(QProcess::ProcessError))); if (outfile != os::nulldev())
connect(proc, SIGNAL(started()), this, SLOT(processStarted())); proc_->setStandardOutputFile(toqstr(outfile));
connect(proc, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(processFinished(int, QProcess::ExitStatus))); } else if (os::is_terminal(os::STDOUT))
showout();
if (os::is_terminal(os::STDERR))
showerr();
connect(proc_, SIGNAL(readyReadStandardOutput()), SLOT(stdOut()));
connect(proc_, SIGNAL(readyReadStandardError()), SLOT(stdErr()));
connect(proc_, SIGNAL(error(QProcess::ProcessError)), SLOT(processError(QProcess::ProcessError)));
connect(proc_, SIGNAL(started()), this, SLOT(processStarted()));
connect(proc_, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(processFinished(int, QProcess::ExitStatus)));
} }
@ -224,6 +205,7 @@ bool SystemcallPrivate::waitWhile(State waitwhile, bool processEvents, int timeo
} }
SystemcallPrivate::~SystemcallPrivate() SystemcallPrivate::~SystemcallPrivate()
{ {
if (outindex_) { if (outindex_) {
@ -241,6 +223,19 @@ SystemcallPrivate::~SystemcallPrivate()
} }
void SystemcallPrivate::flush()
{
// 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) && outfile.empty())
cout << fromqstr(QString::fromLocal8Bit(
proc_->readAllStandardOutput().data()));
if (!os::is_terminal(os::STDERR))
cerr << fromqstr(QString::fromLocal8Bit(
proc_->readAllStandardError().data()));
}
void SystemcallPrivate::stdOut() void SystemcallPrivate::stdOut()
{ {
if (showout_) { if (showout_) {
@ -343,6 +338,27 @@ QString SystemcallPrivate::exitStatusMessage() const
return message; return message;
} }
int SystemcallPrivate::exitCode()
{
return proc_->exitCode();
}
void SystemcallPrivate::killProcess()
{
killProcess(proc_);
}
void SystemcallPrivate::killProcess(QProcess * p)
{
p->disconnect();
p->closeReadChannel(QProcess::StandardOutput);
p->closeReadChannel(QProcess::StandardError);
p->close();
delete p;
}
#include "moc_SystemcallPrivate.cpp" #include "moc_SystemcallPrivate.cpp"
#endif #endif

View File

@ -30,7 +30,7 @@ class SystemcallPrivate : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
SystemcallPrivate(QProcess * proc); SystemcallPrivate(const std::string& outfile);
~SystemcallPrivate(); ~SystemcallPrivate();
/// Should the standard output be displayed? /// Should the standard output be displayed?
@ -50,10 +50,14 @@ public:
bool waitWhile(State, bool processEvents, int timeout = -1); bool waitWhile(State, bool processEvents, int timeout = -1);
void startProcess(const QString& cmd); void startProcess(const QString& cmd);
int exitCode();
QString errorMessage() const; QString errorMessage() const;
QString exitStatusMessage() const; QString exitStatusMessage() const;
void flush();
void killProcess();
static void killProcess(QProcess * p);
private: private:
/// Pointer to the process to monitor. /// Pointer to the process to monitor.
@ -62,6 +66,8 @@ private:
size_t outindex_; size_t outindex_;
/// Index to the standard error buffer. /// Index to the standard error buffer.
size_t errindex_; size_t errindex_;
///
std::string outfile;
/// Size of buffers. /// Size of buffers.
static size_t const bufsize_ = 200; static size_t const bufsize_ = 200;
/// Standard output buffer. /// Standard output buffer.