From 4afa8935aa937441fd595e8e145642d68bf2c9cf Mon Sep 17 00:00:00 2001 From: Syping Date: Wed, 9 May 2018 23:04:54 +0200 Subject: [PATCH] stream now with qCritical() --- main.cpp | 4 +--- mayu.cpp | 26 ++++++++++++++++++++------ mayu.h | 4 +++- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/main.cpp b/main.cpp index ed023a9..bda8c0e 100644 --- a/main.cpp +++ b/main.cpp @@ -29,9 +29,7 @@ int main(int argc, char *argv[]) arguments.removeAt(0); if (arguments.length() >= 2) { - mayu a_mayu; - a_mayu.setHostsFile(arguments.at(0)); - a_mayu.setJsonFile(arguments.at(1)); + mayu a_mayu(arguments.at(0), arguments.at(1)); a_mayu.work(); return a_mayu.getResult(); } diff --git a/mayu.cpp b/mayu.cpp index d4e2745..4fd09ab 100644 --- a/mayu.cpp +++ b/mayu.cpp @@ -30,10 +30,14 @@ extern "C" { #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_tries = 4; + p_tries = tries; + if (!hostsFile.isEmpty()) + setHostsFile(hostsFile); + if (!jsonFile.isEmpty()) + setJsonFile(jsonFile); } void mayu::setHostsFile(const QString &fileName) @@ -53,6 +57,11 @@ void mayu::setJsonFile(const QString &fileName) p_jsonFile = fileName; } +void mayu::setMaxTries(int tries) +{ + p_tries = tries; +} + const QString mayu::getHostsFile() { return p_hostsFile; @@ -68,6 +77,11 @@ const QString mayu::getJsonFile() return p_jsonFile; } +int mayu::getMaxTries() +{ + return p_tries; +} + int mayu::getResult() { return p_return; @@ -196,7 +210,7 @@ void mayu::parse_hosts() } else { - cerr << "Failed read hosts from " << p_hostsFile.toStdString().c_str(); + qCritical() << "Failed read hosts from" << p_hostsFile; } if (!regainPrivileges()) { p_return = 3; @@ -225,7 +239,7 @@ void mayu::work() if (jsonFile.open(QSaveFile::WriteOnly)) { jsonFile.write(jsonArray); if (!jsonFile.commit()) { - cerr << "Failed save result to " << p_jsonFile.toStdString().c_str(); + qCritical() << "Failed save result to" << p_jsonFile; p_return = 1; } } @@ -242,7 +256,7 @@ bool mayu::dropPrivileges() p_uid = geteuid(); int status = seteuid(getuid()); if (status != 0) { - cerr << "Dropping of privileges has failed!"; + qCritical() << "Dropping of privileges has failed!"; return false; } return true; @@ -256,7 +270,7 @@ bool mayu::regainPrivileges() #if _POSIX_SAVED_IDS int status = seteuid(p_uid); if (status != 0) { - cerr << "Regaining of privileges has failed!"; + qCritical() << "Regaining of privileges has failed!"; return false; } return true; diff --git a/mayu.h b/mayu.h index 3df7b9d..79a23fe 100644 --- a/mayu.h +++ b/mayu.h @@ -27,13 +27,15 @@ class mayu : public QObject { Q_OBJECT 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 setHosts(const QStringList &hostsList); void setJsonFile(const QString &fileName); + void setMaxTries(int tries); const QString getHostsFile(); const QStringList getHosts(); const QString getJsonFile(); + int getMaxTries(); int getResult(); static double ping(const QString &host, int tries, double timeout = 2.5);