stream now with qCritical()

This commit is contained in:
Syping 2018-05-09 23:04:54 +02:00
parent 15e7a40fa6
commit 4afa8935aa
3 changed files with 24 additions and 10 deletions

View File

@ -29,9 +29,7 @@ int main(int argc, char *argv[])
arguments.removeAt(0); arguments.removeAt(0);
if (arguments.length() >= 2) { if (arguments.length() >= 2) {
mayu a_mayu; mayu a_mayu(arguments.at(0), arguments.at(1));
a_mayu.setHostsFile(arguments.at(0));
a_mayu.setJsonFile(arguments.at(1));
a_mayu.work(); a_mayu.work();
return a_mayu.getResult(); return a_mayu.getResult();
} }

View File

@ -30,10 +30,14 @@ extern "C" {
#include "oping.h" #include "oping.h"
} }
mayu::mayu(QObject *parent) : QObject(parent) mayu::mayu(const QString &hostsFile, const QString &jsonFile, int tries, QObject *parent) : QObject(parent)
{ {
p_return = -1; p_return = -1;
p_tries = 4; p_tries = tries;
if (!hostsFile.isEmpty())
setHostsFile(hostsFile);
if (!jsonFile.isEmpty())
setJsonFile(jsonFile);
} }
void mayu::setHostsFile(const QString &fileName) void mayu::setHostsFile(const QString &fileName)
@ -53,6 +57,11 @@ void mayu::setJsonFile(const QString &fileName)
p_jsonFile = fileName; p_jsonFile = fileName;
} }
void mayu::setMaxTries(int tries)
{
p_tries = tries;
}
const QString mayu::getHostsFile() const QString mayu::getHostsFile()
{ {
return p_hostsFile; return p_hostsFile;
@ -68,6 +77,11 @@ const QString mayu::getJsonFile()
return p_jsonFile; return p_jsonFile;
} }
int mayu::getMaxTries()
{
return p_tries;
}
int mayu::getResult() int mayu::getResult()
{ {
return p_return; return p_return;
@ -196,7 +210,7 @@ void mayu::parse_hosts()
} }
else else
{ {
cerr << "Failed read hosts from " << p_hostsFile.toStdString().c_str(); qCritical() << "Failed read hosts from" << p_hostsFile;
} }
if (!regainPrivileges()) { if (!regainPrivileges()) {
p_return = 3; p_return = 3;
@ -225,7 +239,7 @@ void mayu::work()
if (jsonFile.open(QSaveFile::WriteOnly)) { if (jsonFile.open(QSaveFile::WriteOnly)) {
jsonFile.write(jsonArray); jsonFile.write(jsonArray);
if (!jsonFile.commit()) { if (!jsonFile.commit()) {
cerr << "Failed save result to " << p_jsonFile.toStdString().c_str(); qCritical() << "Failed save result to" << p_jsonFile;
p_return = 1; p_return = 1;
} }
} }
@ -242,7 +256,7 @@ bool mayu::dropPrivileges()
p_uid = geteuid(); p_uid = geteuid();
int status = seteuid(getuid()); int status = seteuid(getuid());
if (status != 0) { if (status != 0) {
cerr << "Dropping of privileges has failed!"; qCritical() << "Dropping of privileges has failed!";
return false; return false;
} }
return true; return true;
@ -256,7 +270,7 @@ bool mayu::regainPrivileges()
#if _POSIX_SAVED_IDS #if _POSIX_SAVED_IDS
int status = seteuid(p_uid); int status = seteuid(p_uid);
if (status != 0) { if (status != 0) {
cerr << "Regaining of privileges has failed!"; qCritical() << "Regaining of privileges has failed!";
return false; return false;
} }
return true; return true;

4
mayu.h
View File

@ -27,13 +27,15 @@ class mayu : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit mayu(QObject *parent = nullptr); explicit mayu(const QString &hostsFile = QString(), const QString &jsonFile = QString(), int tries = 4, QObject *parent = nullptr);
void setHostsFile(const QString &fileName); void setHostsFile(const QString &fileName);
void setHosts(const QStringList &hostsList); void setHosts(const QStringList &hostsList);
void setJsonFile(const QString &fileName); void setJsonFile(const QString &fileName);
void setMaxTries(int tries);
const QString getHostsFile(); const QString getHostsFile();
const QStringList getHosts(); const QStringList getHosts();
const QString getJsonFile(); const QString getJsonFile();
int getMaxTries();
int getResult(); int getResult();
static double ping(const QString &host, int tries, double timeout = 2.5); static double ping(const QString &host, int tries, double timeout = 2.5);