Logging via syslog added, logging vio stdout/stderr removed

This commit is contained in:
tastytea 2018-05-22 13:44:41 +02:00
parent c215c076e4
commit e9419c07a1
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
5 changed files with 39 additions and 33 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.7) cmake_minimum_required (VERSION 3.7)
project (expandurl-mastodon project (expandurl-mastodon
VERSION 0.5.6 VERSION 0.6.0
LANGUAGES CXX LANGUAGES CXX
) )

View File

@ -62,6 +62,9 @@ and an acess token is generated. The config file can be found in
"access_token": "abc123" "access_token": "abc123"
} }
After the configuration file is generated, you can start expandurl-mastodon as
daemon.
# Copyright # Copyright
Copyright © 2018 tastytea <tastytea@tastytea.de>. Copyright © 2018 tastytea <tastytea@tastytea.de>.

View File

@ -18,11 +18,11 @@
#include <chrono> #include <chrono>
#include <csignal> #include <csignal>
#include <regex> #include <regex>
#include <syslog.h>
#include <unistd.h> // getuid()
#include <curlpp/cURLpp.hpp> #include <curlpp/cURLpp.hpp>
#include "expandurl-mastodon.hpp" #include "expandurl-mastodon.hpp"
using std::cout;
using std::cerr;
using std::string; using std::string;
using Mastodon::Easy; using Mastodon::Easy;
@ -36,11 +36,11 @@ void signal_handler(int signum)
case SIGTERM: case SIGTERM:
if (!running) if (!running)
{ {
cout << "Forced close.\n"; syslog(LOG_NOTICE, "Forced close.");
exit(signum); exit(signum);
} }
running = false; running = false;
cout << "Closing, this could take a few seconds...\n"; syslog(LOG_NOTICE, "Received signal %d, closing...", signum);
break; break;
default: default:
break; break;
@ -52,6 +52,8 @@ int main(int argc, char *argv[])
signal(SIGINT, signal_handler); signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler); signal(SIGTERM, signal_handler);
curlpp::initialize(); 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 listener;
listener.start(); listener.start();
@ -63,8 +65,9 @@ int main(int argc, char *argv[])
if (!listener.stillrunning()) if (!listener.stillrunning())
{ {
listener.stop(); listener.stop();
cout << "DEBUG: Reestablishing connection...\n"; syslog(LOG_DEBUG, "Reestablishing connection...");
listener.start(); listener.start();
syslog(LOG_NOTICE, "Reestablished connection.");
new_messages = listener.catchup(); new_messages = listener.catchup();
} }
@ -76,9 +79,9 @@ int main(int argc, char *argv[])
for (Easy::Notification &notif : new_messages) for (Easy::Notification &notif : new_messages)
{ {
cout << "DEBUG: new message\n"; syslog(LOG_DEBUG, "new message");
const std::uint_fast64_t id = listener.get_parent_id(notif); 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; Easy::Status status;
if (id > 0) if (id > 0)
@ -95,8 +98,7 @@ int main(int argc, char *argv[])
{ {
if (!listener.send_reply(notif.status(), message)) if (!listener.send_reply(notif.status(), message))
{ {
cerr << "ERROR: could not send reply to " << id << syslog(LOG_ERR, "could not send reply to %lu", id);
'\n';
} }
} }
else else
@ -123,6 +125,7 @@ int main(int argc, char *argv[])
} }
listener.stop(); listener.stop();
closelog();
curlpp::terminate(); curlpp::terminate();
return 0; return 0;

View File

@ -19,10 +19,10 @@
#include <iostream> #include <iostream>
#include <mutex> #include <mutex>
#include <sstream> #include <sstream>
#include <syslog.h>
#include "version.hpp" #include "version.hpp"
#include "expandurl-mastodon.hpp" #include "expandurl-mastodon.hpp"
using std::cerr;
using std::cout; using std::cout;
using std::string; using std::string;
@ -44,20 +44,20 @@ Listener::Listener()
} }
else else
{ {
cerr << "WARNING: Could not open " << _configfilepath << ".\n"; syslog(LOG_WARNING, "Could not open %s.", _configfilepath.c_str());
cout << "Attempting to register application and write config file.\n"; syslog(LOG_INFO, "Attempting to register application and write config file.");
if (register_app()) if (register_app())
{ {
cout << "DEBUG: registration successful.\n"; syslog(LOG_INFO, "Registration successful.");
if (!write_config()) if (!write_config())
{ {
cerr << "ERROR: Could not write " << _configfilepath << ".\n"; syslog(LOG_ERR, "Could not write %s.", _configfilepath.c_str());
std::exit(1); std::exit(1);
} }
} }
else else
{ {
cerr << "ERROR: Could not register app.\n"; syslog(LOG_ERR, "Could not register app.");
std::exit(2); std::exit(2);
} }
} }
@ -67,7 +67,7 @@ Listener::~Listener()
{ {
if (!write_config()) 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<const string>("expandurl-mastodon/") + masto.set_useragent(static_cast<const string>("expandurl-mastodon/") +
global::version); global::version);
masto.get_stream(Mastodon::API::v1::streaming_user, _stream, _ptr); masto.get_stream(Mastodon::API::v1::streaming_user, _stream, _ptr);
cout << "DEBUG: Connection lost.\n"; syslog(LOG_DEBUG, "Connection lost.");
_running = false; _running = false;
}); });
while (_ptr == nullptr) while (_ptr == nullptr)
@ -135,7 +135,7 @@ const void Listener::stop()
} }
else else
{ {
cout << "DEBUG: _ptr is false.\n"; syslog(LOG_DEBUG, "_ptr is false.");
} }
} }
@ -170,7 +170,7 @@ const std::vector<Easy::Notification> Listener::get_new_messages()
if (count_empty > 5) if (count_empty > 5)
{ {
count_empty = 0; count_empty = 0;
cout << "DEBUG: Detected broken connection.\n"; syslog(LOG_NOTICE, "Detected broken connection.");
_running = false; _running = false;
} }
} }
@ -184,7 +184,7 @@ const std::vector<Easy::Notification> Listener::catchup()
const string last_id = _config["last_id"].asString(); const string last_id = _config["last_id"].asString();
if (last_id != "") if (last_id != "")
{ {
cout << "DEBUG: catching up...\n"; syslog(LOG_DEBUG, "Catching up...");
API::parametermap parameter = API::parametermap parameter =
{ {
{ "since_id", { last_id } }, { "since_id", { last_id } },
@ -220,7 +220,7 @@ Mastodon::Easy::Status Listener::get_status(const std::uint_fast64_t &id)
} }
else else
{ {
cerr << "ERROR: " << ret << " (in " << __FUNCTION__ << ")\n"; syslog(LOG_ERR, "Error %lu in %s.", ret, __FUNCTION__);
return Easy::Status(); return Easy::Status();
} }
} }
@ -267,12 +267,12 @@ const bool Listener::send_reply(const Easy::Status &to_status,
if (ret == 0) if (ret == 0)
{ {
cout << "DEBUG: Sent reply.\n"; syslog(LOG_DEBUG, "Sent reply");
return true; return true;
} }
else else
{ {
cerr << "ERROR: " << ret << " (in " << __FUNCTION__ << ")\n"; syslog(LOG_ERR, "Error %lu in %s.", ret, __FUNCTION__);
return false; return false;
} }
} }
@ -287,8 +287,8 @@ const std::uint_fast64_t Listener::get_parent_id(const Easy::Notification &notif
answer); answer);
if (ret > 0 || !Easy::Status(answer).valid()) if (ret > 0 || !Easy::Status(answer).valid())
{ {
cerr << "ERROR: " << ret << syslog(LOG_ERR, "Error %lu: Could not fetch status (in %s).",
"Could not fetch status (in " << __FUNCTION__ << ")\n"; ret, __FUNCTION__);
return 0; return 0;
} }
@ -298,8 +298,8 @@ const std::uint_fast64_t Listener::get_parent_id(const Easy::Notification &notif
if (ret > 0) if (ret > 0)
{ {
cerr << "ERROR: " << ret << syslog(LOG_ERR, "Error %lu: Could not get status (in %s).",
"Could not get status (in " << __FUNCTION__ << ")\n"; ret, __FUNCTION__);
return 0; return 0;
} }
else else
@ -354,12 +354,12 @@ const bool Listener::register_app()
} }
else else
{ {
cerr << "ERROR: register_app2(): " << ret << '\n'; syslog(LOG_ERR, "register_app2(): %lu", ret);
} }
} }
else else
{ {
cerr << "ERROR: register_app1(): " << ret << '\n'; syslog(LOG_ERR, "register_app1(): %lu", ret);
} }
return false; return false;

View File

@ -19,13 +19,13 @@
#include <regex> #include <regex>
#include <array> #include <array>
#include <utility> #include <utility>
#include <syslog.h>
#include <curlpp/cURLpp.hpp> #include <curlpp/cURLpp.hpp>
#include <curlpp/Options.hpp> #include <curlpp/Options.hpp>
#include <curlpp/Infos.hpp> #include <curlpp/Infos.hpp>
#include "version.hpp" #include "version.hpp"
#include "expandurl-mastodon.hpp" #include "expandurl-mastodon.hpp"
using std::cerr;
using std::string; using std::string;
namespace curlopts = curlpp::options; namespace curlopts = curlpp::options;
@ -68,8 +68,8 @@ const string expand(const string &url)
} }
catch (const std::exception &e) catch (const std::exception &e)
{ {
cerr << "ERROR: " << e.what() << '\n'; syslog(LOG_ERR, "%s", e.what());
cerr << "The previous error is ignored.\n"; syslog(LOG_NOTICE, "The previous error is ignored.");
} }
return curlpp::infos::EffectiveUrl::get(request); return curlpp::infos::EffectiveUrl::get(request);