From e9419c07a182b0752c8918c06ce53c8f165eb735 Mon Sep 17 00:00:00 2001 From: tastytea Date: Tue, 22 May 2018 13:44:41 +0200 Subject: [PATCH] Logging via syslog added, logging vio stdout/stderr removed --- CMakeLists.txt | 2 +- README.md | 3 +++ src/main.cpp | 21 ++++++++++++--------- src/masto.cpp | 40 ++++++++++++++++++++-------------------- src/url.cpp | 6 +++--- 5 files changed, 39 insertions(+), 33 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0bcaf8c..e0a5775 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (expandurl-mastodon - VERSION 0.5.6 + VERSION 0.6.0 LANGUAGES CXX ) diff --git a/README.md b/README.md index 82a18f2..6efe394 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,9 @@ and an acess token is generated. The config file can be found in "access_token": "abc123" } +After the configuration file is generated, you can start expandurl-mastodon as +daemon. + # Copyright Copyright © 2018 tastytea . diff --git a/src/main.cpp b/src/main.cpp index 4c0596c..3655216 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,11 +18,11 @@ #include #include #include +#include +#include // getuid() #include #include "expandurl-mastodon.hpp" -using std::cout; -using std::cerr; using std::string; using Mastodon::Easy; @@ -36,11 +36,11 @@ void signal_handler(int signum) case SIGTERM: if (!running) { - cout << "Forced close.\n"; + syslog(LOG_NOTICE, "Forced close."); exit(signum); } running = false; - cout << "Closing, this could take a few seconds...\n"; + syslog(LOG_NOTICE, "Received signal %d, closing...", signum); break; default: break; @@ -52,6 +52,8 @@ int main(int argc, char *argv[]) signal(SIGINT, signal_handler); signal(SIGTERM, signal_handler); curlpp::initialize(); + openlog("expandurl-mastodon", LOG_CONS | LOG_NDELAY | LOG_PID, LOG_LOCAL0); + syslog(LOG_NOTICE, "Program started by User %d", getuid()); Listener listener; listener.start(); @@ -63,8 +65,9 @@ int main(int argc, char *argv[]) if (!listener.stillrunning()) { listener.stop(); - cout << "DEBUG: Reestablishing connection...\n"; + syslog(LOG_DEBUG, "Reestablishing connection..."); listener.start(); + syslog(LOG_NOTICE, "Reestablished connection."); new_messages = listener.catchup(); } @@ -76,9 +79,9 @@ int main(int argc, char *argv[]) for (Easy::Notification ¬if : new_messages) { - cout << "DEBUG: new message\n"; + syslog(LOG_DEBUG, "new message"); const std::uint_fast64_t id = listener.get_parent_id(notif); - cout << "DEBUG: in_reply_to_id: " << id << '\n'; + syslog(LOG_DEBUG, "in_reply_to_id: %lu", id); Easy::Status status; if (id > 0) @@ -95,8 +98,7 @@ int main(int argc, char *argv[]) { if (!listener.send_reply(notif.status(), message)) { - cerr << "ERROR: could not send reply to " << id << - '\n'; + syslog(LOG_ERR, "could not send reply to %lu", id); } } else @@ -123,6 +125,7 @@ int main(int argc, char *argv[]) } listener.stop(); + closelog(); curlpp::terminate(); return 0; diff --git a/src/masto.cpp b/src/masto.cpp index 94826ed..8e549fa 100644 --- a/src/masto.cpp +++ b/src/masto.cpp @@ -19,10 +19,10 @@ #include #include #include +#include #include "version.hpp" #include "expandurl-mastodon.hpp" -using std::cerr; using std::cout; using std::string; @@ -44,20 +44,20 @@ Listener::Listener() } else { - cerr << "WARNING: Could not open " << _configfilepath << ".\n"; - cout << "Attempting to register application and write config file.\n"; + syslog(LOG_WARNING, "Could not open %s.", _configfilepath.c_str()); + syslog(LOG_INFO, "Attempting to register application and write config file."); if (register_app()) { - cout << "DEBUG: registration successful.\n"; + syslog(LOG_INFO, "Registration successful."); if (!write_config()) { - cerr << "ERROR: Could not write " << _configfilepath << ".\n"; + syslog(LOG_ERR, "Could not write %s.", _configfilepath.c_str()); std::exit(1); } } else { - cerr << "ERROR: Could not register app.\n"; + syslog(LOG_ERR, "Could not register app."); std::exit(2); } } @@ -67,7 +67,7 @@ Listener::~Listener() { if (!write_config()) { - cerr << "ERROR: Could not write " << _configfilepath << ".\n"; + syslog(LOG_ERR, "Could not write %s.", _configfilepath.c_str()); } } @@ -115,7 +115,7 @@ const void Listener::start() masto.set_useragent(static_cast("expandurl-mastodon/") + global::version); masto.get_stream(Mastodon::API::v1::streaming_user, _stream, _ptr); - cout << "DEBUG: Connection lost.\n"; + syslog(LOG_DEBUG, "Connection lost."); _running = false; }); while (_ptr == nullptr) @@ -135,7 +135,7 @@ const void Listener::stop() } else { - cout << "DEBUG: _ptr is false.\n"; + syslog(LOG_DEBUG, "_ptr is false."); } } @@ -170,7 +170,7 @@ const std::vector Listener::get_new_messages() if (count_empty > 5) { count_empty = 0; - cout << "DEBUG: Detected broken connection.\n"; + syslog(LOG_NOTICE, "Detected broken connection."); _running = false; } } @@ -184,7 +184,7 @@ const std::vector Listener::catchup() const string last_id = _config["last_id"].asString(); if (last_id != "") { - cout << "DEBUG: catching up...\n"; + syslog(LOG_DEBUG, "Catching up..."); API::parametermap parameter = { { "since_id", { last_id } }, @@ -220,7 +220,7 @@ Mastodon::Easy::Status Listener::get_status(const std::uint_fast64_t &id) } else { - cerr << "ERROR: " << ret << " (in " << __FUNCTION__ << ")\n"; + syslog(LOG_ERR, "Error %lu in %s.", ret, __FUNCTION__); return Easy::Status(); } } @@ -267,12 +267,12 @@ const bool Listener::send_reply(const Easy::Status &to_status, if (ret == 0) { - cout << "DEBUG: Sent reply.\n"; + syslog(LOG_DEBUG, "Sent reply"); return true; } else { - cerr << "ERROR: " << ret << " (in " << __FUNCTION__ << ")\n"; + syslog(LOG_ERR, "Error %lu in %s.", ret, __FUNCTION__); return false; } } @@ -287,8 +287,8 @@ const std::uint_fast64_t Listener::get_parent_id(const Easy::Notification ¬if answer); if (ret > 0 || !Easy::Status(answer).valid()) { - cerr << "ERROR: " << ret << - "Could not fetch status (in " << __FUNCTION__ << ")\n"; + syslog(LOG_ERR, "Error %lu: Could not fetch status (in %s).", + ret, __FUNCTION__); return 0; } @@ -298,8 +298,8 @@ const std::uint_fast64_t Listener::get_parent_id(const Easy::Notification ¬if if (ret > 0) { - cerr << "ERROR: " << ret << - "Could not get status (in " << __FUNCTION__ << ")\n"; + syslog(LOG_ERR, "Error %lu: Could not get status (in %s).", + ret, __FUNCTION__); return 0; } else @@ -354,12 +354,12 @@ const bool Listener::register_app() } else { - cerr << "ERROR: register_app2(): " << ret << '\n'; + syslog(LOG_ERR, "register_app2(): %lu", ret); } } else { - cerr << "ERROR: register_app1(): " << ret << '\n'; + syslog(LOG_ERR, "register_app1(): %lu", ret); } return false; diff --git a/src/url.cpp b/src/url.cpp index 37b2375..c353ad2 100644 --- a/src/url.cpp +++ b/src/url.cpp @@ -19,13 +19,13 @@ #include #include #include +#include #include #include #include #include "version.hpp" #include "expandurl-mastodon.hpp" -using std::cerr; using std::string; namespace curlopts = curlpp::options; @@ -68,8 +68,8 @@ const string expand(const string &url) } catch (const std::exception &e) { - cerr << "ERROR: " << e.what() << '\n'; - cerr << "The previous error is ignored.\n"; + syslog(LOG_ERR, "%s", e.what()); + syslog(LOG_NOTICE, "The previous error is ignored."); } return curlpp::infos::EffectiveUrl::get(request);