Commit 69c06d22 authored by Ivan Vilata-i-Balaguer's avatar Ivan Vilata-i-Balaguer
Browse files

Use logger for announcer messages.

This also fixes commit b583c0e1 in case the client has debugging off.
parent 320f0757
#include <list>
#include <sstream>
#include "announcer.h"
#include "../../util/async_queue.h"
#include "../../logger.h"
......@@ -7,6 +8,9 @@
#include "../../util/handler_tracker.h"
#include <boost/utility/string_view.hpp>
#define _LOGPFX "Announcer: "
#define _DEBUG(...) LOG_DEBUG(_LOGPFX, __VA_ARGS__)
using namespace std;
using namespace ouinet;
using namespace ouinet::cache;
......@@ -74,13 +78,11 @@ struct Announcer::Loop {
auto entry_i = find_entry_by_key(key);
bool already_has_key = (entry_i != entries.end());
if (debug()) {
if (already_has_key) {
std::cerr << "Announcer: adding " << key << " (already exists)\n";
entry_i->first.to_remove = false;
} else {
std::cerr << "Announcer: adding " << key << "\n";
}
if (already_has_key) {
_DEBUG("adding ", key, " (already exists)");
entry_i->first.to_remove = false;
} else {
_DEBUG("adding ", key);
}
if (already_has_key) return;
......@@ -106,9 +108,7 @@ struct Announcer::Loop {
if (i->first.key == key) break; // found
if (i == entries.end()) return; // not found
if (debug()) {
std::cerr << "Announcer: marking " << key << " for removal\n";
}
_DEBUG("marking ", key, " for removal");
// The actual removal is not done here but in the main loop.
i->first.to_remove = true;
// No new entries, so no `_timer_cancel` reset.
......@@ -137,9 +137,10 @@ struct Announcer::Loop {
void print_entries() const {
auto now = Clock::now();
stringstream ss;
auto print = [&] (Clock::time_point t) {
if (t == Clock::time_point()) {
cerr << "--:--:--";
ss << "--:--:--";
}
else {
// TODO: For the purpose of analyzing logs, it would be better
......@@ -151,23 +152,26 @@ struct Announcer::Loop {
unsigned mins = secs / 60;
secs -= mins * 60;
cerr << std::setfill('0') << std::setw(2) << hrs;
cerr << ':';
cerr << std::setfill('0') << std::setw(2) << mins;
cerr << ':';
cerr << std::setfill('0') << std::setw(2) << secs;
ss << std::setfill('0') << std::setw(2) << hrs;
ss << ':';
ss << std::setfill('0') << std::setw(2) << mins;
ss << ':';
ss << std::setfill('0') << std::setw(2) << secs;
}
cerr << " ago";
ss << " ago";
};
cerr << "Announcer: entries:" << "\n";
_DEBUG("entries:");
for (auto& ep : entries) {
auto& e = ep.first;
cerr << "Announcer: " << e.infohash << " | successful_update:";
ss << " " << e.infohash << " | successful_update:";
print(e.successful_update);
cerr << " | failed_update:";
ss << " | failed_update:";
print(e.failed_update);
cerr << " | key:" << e.key << "\n";
ss << " | key:" << e.key;
_DEBUG(ss.str());
ss.str({});
}
}
......@@ -180,9 +184,7 @@ struct Announcer::Loop {
// fails to exit.
TRACK_HANDLER();
sys::error_code ec;
if (debug()) {
std::cerr << "Announcer: no entries to update, waiting...\n";
}
_DEBUG("no entries to update, waiting...");
entries.async_wait_for_push(cancel, yield[ec]);
if (cancel) ec = asio::error::operation_aborted;
if (ec) return or_throw(yield, ec, end);
......@@ -194,11 +196,9 @@ struct Announcer::Loop {
auto d = next_update_after(i->first);
if (debug()) {
std::cerr << "Announcer: found entry to update. It'll be updated in "
<< chrono::duration_cast<chrono::seconds>(d).count()
<< " seconds; " << i->first.key << "\n";
}
_DEBUG( "found entry to update. It'll be updated in "
, chrono::duration_cast<chrono::seconds>(d).count()
, " seconds; ", i->first.key);
if (d == 0s) return i;
......@@ -225,20 +225,17 @@ struct Announcer::Loop {
// fails to exit.
TRACK_HANDLER();
sys::error_code ec;
if (debug()) cerr << "Announcer: waiting for DHT\n";
_DEBUG("waiting for DHT");
dht->wait_all_ready(cancel, yield[ec]);
}
auto on_exit = defer([&] {
if (debug()) {
if (debug()) cerr << "Announcer: exiting the loop "
"(cancel:" << (cancel ? "true":"false") << ")\n";
}
_DEBUG("exiting the loop (cancel:", (cancel ? "true":"false"), ")");
});
while (!cancel) {
sys::error_code ec;
if (debug()) cerr << "Announcer: picking entry to update\n";
_DEBUG("picking entry to update");
auto ei = pick_entry(cancel, yield[ec]);
if (cancel) return;
......@@ -284,17 +281,13 @@ struct Announcer::Loop {
void announce(Entry& e, Cancel& cancel, asio::yield_context yield)
{
if (debug()) {
cerr << "Announcer: Announcing " << e.key << "\n";
}
_DEBUG("Announcing ", e.key);
sys::error_code ec;
auto e_key{debug() ? e.key : ""}; // cancellation trashes the key
dht->tracker_announce(e.infohash, boost::none, cancel, yield[ec]);
if (debug()) {
cerr << "Announcer: Announcing ended " << e_key << " ec:" << ec.message() << "\n";
}
_DEBUG("Announcing ended ", e_key, " ec:", ec.message());
return or_throw(yield, ec);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment