00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <QFileInfo>
00029 #include <QCoreApplication>
00030
00031
00032 #if defined (Q_OS_WIN32)
00033 #include <windows.h>
00034 #endif
00035
00036 #include "torprocess.h"
00037
00038
00039 #define FMT_TIMESTAMP "MMM dd hh:mm:ss.zzz yyyy"
00040
00041
00042 TorProcess::TorProcess()
00043 {
00044 openStdout();
00045 connect(this, SIGNAL(readyReadStandardOutput()),
00046 this, SLOT(onReadyRead()));
00047 connect(this, SIGNAL(error(QProcess::ProcessError)),
00048 this, SLOT(onError(QProcess::ProcessError)));
00049 }
00050
00051
00052
00053
00054
00055 void
00056 TorProcess::start(QString app, QString args)
00057 {
00058 #if defined(Q_OS_WIN32)
00059
00060
00061 app = "\"" + app + "\"";
00062 #endif
00063
00064
00065 setEnvironment(QProcess::systemEnvironment());
00066 QProcess::start(app + " " + args, QIODevice::ReadOnly | QIODevice::Text);
00067 }
00068
00069
00070 bool
00071 TorProcess::stop(QString *errmsg)
00072 {
00073
00074
00075 if (state() == QProcess::NotRunning) {
00076 return true;
00077 }
00078
00079
00080 #if defined(Q_OS_WIN32)
00081
00082
00083 kill();
00084 #else
00085 terminate();
00086
00087
00088 if (!waitForFinished(-1)) {
00089 if (errmsg) {
00090 *errmsg =
00091 tr("Process %1 failed to stop. [%2]").arg(pid()).arg(errorString());
00092 }
00093 return false;
00094 }
00095 #endif
00096 return true;
00097 }
00098
00099
00100 quint64
00101 TorProcess::pid()
00102 {
00103 #if defined(Q_OS_WIN32)
00104 return (quint64)((QProcess::pid())->dwProcessId);
00105 #else
00106 return QProcess::pid();
00107 #endif
00108 }
00109
00110
00111
00112 void
00113 TorProcess::openStdout()
00114 {
00115 _logState = Open;
00116 setReadChannelMode(QProcess::MergedChannels);
00117 setReadChannel(QProcess::StandardOutput);
00118 }
00119
00120
00121
00122 void
00123 TorProcess::closeStdout()
00124 {
00125 _logState = Closing;
00126 _logCloseTime = QDateTime::currentDateTime();
00127 }
00128
00129
00130 void
00131 TorProcess::onReadyRead()
00132 {
00133 int i, j;
00134 while (canReadLine()) {
00135
00136
00137
00138 QString line = readLine();
00139 if (_logState == Closed) {
00140 continue;
00141 }
00142
00143
00144 i = line.indexOf("[");
00145 j = line.indexOf("]");
00146 if (i > 0 && j > 0) {
00147 if (_logState == Closing) {
00148
00149 QString msgTime = QString("%1 %2").arg(line.mid(0, i-1))
00150 .arg(_logCloseTime.date().year());
00151
00152 if (_logCloseTime < QDateTime::fromString(msgTime, FMT_TIMESTAMP)) {
00153
00154
00155 _logState = Closed;
00156 closeReadChannel(QProcess::StandardOutput);
00157 }
00158 }
00159 if (_logState != Closed) {
00160 emit log(line.mid(i+1, j-i-1), line.mid(j+2));
00161 }
00162 }
00163 }
00164 }
00165
00166
00167
00168
00169 void
00170 TorProcess::onError(QProcess::ProcessError error)
00171 {
00172 if (error == QProcess::FailedToStart) {
00173
00174 emit startFailed(errorString());
00175 }
00176 }
00177