From 60ada470dbc52fd701ee4d70b8d2667e5c196b1c Mon Sep 17 00:00:00 2001 From: Jeff Date: Fri, 20 May 2022 11:11:34 -0400 Subject: [PATCH] format systemd status as time deltas from now --- llarp/router/router.cpp | 5 +++-- llarp/util/time.hpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index 43de835e5..5c04b6799 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -870,10 +870,11 @@ namespace llarp << NumberOfConnectedRouters() << "/" << NumberOfConnectedClients() << " | " << pathContext().CurrentTransitPaths() << " active paths | " << "block " << (m_lokidRpcClient ? m_lokidRpcClient->BlockHeight() : 0) << " | gossip: " - << "(next/last) " << _rcGossiper.NextGossipAt() << "/"; + << "(next/last) " << time_delta{_rcGossiper.NextGossipAt()} + << " / "; if (auto maybe = _rcGossiper.LastGossipAt()) { - ss << *maybe; + ss << time_delta{*maybe}; } else { diff --git a/llarp/util/time.hpp b/llarp/util/time.hpp index 0b3c20d3d..abd725f05 100644 --- a/llarp/util/time.hpp +++ b/llarp/util/time.hpp @@ -29,4 +29,33 @@ namespace llarp std::ostream& operator<<(std::ostream& out, const TimePoint_t& t); + template + struct time_delta + { + const TimePoint_t at; + + std::ostream& + operator()(std::ostream& out) const + { + const auto dlt = std::chrono::duration_cast(TimePoint_t::clock::now() - at); + if (dlt > 0s) + return out << std::chrono::duration_cast(dlt) << " ago "; + else if (dlt < 0s) + return out << "in " << std::chrono::duration_cast(-dlt); + else + return out << "now"; + } + }; + + inline std::ostream& + operator<<(std::ostream& out, const time_delta& td) + { + return td(out); + } + + inline std::ostream& + operator<<(std::ostream& out, const time_delta& td) + { + return td(out); + } } // namespace llarp