mirror of
https://github.com/oxen-io/lokinet.git
synced 2024-11-11 07:10:36 +00:00
b81f7025c9
Replaces custom logging system with spdlog-based oxen logging. This commit mainly replaces the backend logging with the spdlog-based system, but doesn't (yet) convert all the existing LogWarn, etc. to use the new format-based logging. New logging statements will look like: llarp::log::warning(cat, "blah: {}", val); where `cat` should be set up in each .cpp or cluster of .cpp files, as described in the oxen-logging README. As part of spdlog we get fmt, which gives us nice format strings, where are applied generously in this commit. Making types printable now requires two steps: - add a ToString() method - add this specialization: template <> constexpr inline bool llarp::IsToStringFormattable<llarp::Whatever> = true; This will then allow the type to be printed as a "{}" value in a fmt::format string. This is applied to all our printable types here, and all of the `operator<<` are removed. This commit also: - replaces various uses of `operator<<` to ToString() - replaces various uses of std::stringstream with either fmt::format or plain std::string - Rename some to_string and toString() methods to ToString() for consistency (and to work with fmt) - Replace `stringify(...)` and `make_exception` usage with fmt::format (and remove stringify/make_exception from util/str.hpp).
94 lines
2.5 KiB
C++
94 lines
2.5 KiB
C++
#pragma once
|
|
|
|
#include "types.hpp"
|
|
#include <nlohmann/json.hpp>
|
|
#include <iostream>
|
|
#include <fmt/format.h>
|
|
#include <fmt/chrono.h>
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
namespace llarp
|
|
{
|
|
/// get time right now as milliseconds, this is monotonic
|
|
Duration_t
|
|
time_now_ms();
|
|
|
|
/// get the uptime of the process
|
|
Duration_t
|
|
uptime();
|
|
|
|
/// convert to milliseconds
|
|
uint64_t
|
|
ToMS(Duration_t duration);
|
|
|
|
nlohmann::json
|
|
to_json(const Duration_t& t);
|
|
|
|
template <typename Time_Duration>
|
|
struct time_delta
|
|
{
|
|
const TimePoint_t at;
|
|
};
|
|
} // namespace llarp
|
|
|
|
namespace fmt
|
|
{
|
|
template <typename Time_Duration>
|
|
struct formatter<llarp::time_delta<Time_Duration>> : formatter<std::string>
|
|
{
|
|
template <typename FormatContext>
|
|
auto
|
|
format(const llarp::time_delta<Time_Duration>& td, FormatContext& ctx)
|
|
{
|
|
const auto dlt =
|
|
std::chrono::duration_cast<llarp::Duration_t>(llarp::TimePoint_t::clock::now() - td.at);
|
|
using Parent = formatter<std::string>;
|
|
if (dlt > 0s)
|
|
return Parent::format(fmt::format("{} ago", dlt), ctx);
|
|
if (dlt < 0s)
|
|
return Parent::format(fmt::format("in {}", -dlt), ctx);
|
|
return Parent::format("now", ctx);
|
|
}
|
|
};
|
|
|
|
template <>
|
|
struct formatter<llarp::Duration_t> : formatter<std::string>
|
|
{
|
|
template <typename FormatContext>
|
|
auto
|
|
format(llarp::Duration_t elapsed, FormatContext& ctx)
|
|
{
|
|
bool neg = elapsed < 0s;
|
|
if (neg)
|
|
elapsed = -elapsed;
|
|
const auto hours = std::chrono::duration_cast<std::chrono::hours>(elapsed).count();
|
|
const auto mins = (std::chrono::duration_cast<std::chrono::minutes>(elapsed) % 1h).count();
|
|
const auto secs = (std::chrono::duration_cast<std::chrono::seconds>(elapsed) % 1min).count();
|
|
const auto ms = (std::chrono::duration_cast<std::chrono::milliseconds>(elapsed) % 1s).count();
|
|
return formatter<std::string>::format(
|
|
fmt::format(
|
|
elapsed >= 1h ? "{0}{1:d}h{2:02d}m{3:02d}.{4:03d}s"
|
|
: elapsed >= 1min ? "{0}{2:d}m{3:02d}.{4:03d}s"
|
|
: "{0}{3:d}.{4:03d}s",
|
|
neg ? "-" : "",
|
|
hours,
|
|
mins,
|
|
secs,
|
|
ms),
|
|
ctx);
|
|
}
|
|
};
|
|
|
|
template <>
|
|
struct formatter<llarp::TimePoint_t> : formatter<std::string>
|
|
{
|
|
template <typename FormatContext>
|
|
auto
|
|
format(const llarp::TimePoint_t& tp, FormatContext& ctx)
|
|
{
|
|
return formatter<std::string>::format(fmt::format("{:%c %Z}", tp), ctx);
|
|
}
|
|
};
|
|
} // namespace fmt
|