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)
project (expandurl-mastodon
VERSION 0.5.6
VERSION 0.6.0
LANGUAGES CXX
)

View File

@ -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 <tastytea@tastytea.de>.

View File

@ -18,11 +18,11 @@
#include <chrono>
#include <csignal>
#include <regex>
#include <syslog.h>
#include <unistd.h> // getuid()
#include <curlpp/cURLpp.hpp>
#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 &notif : 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;

View File

@ -19,10 +19,10 @@
#include <iostream>
#include <mutex>
#include <sstream>
#include <syslog.h>
#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<const string>("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<Easy::Notification> 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<Easy::Notification> 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 &notif
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 &notif
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;

View File

@ -19,13 +19,13 @@
#include <regex>
#include <array>
#include <utility>
#include <syslog.h>
#include <curlpp/cURLpp.hpp>
#include <curlpp/Options.hpp>
#include <curlpp/Infos.hpp>
#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);