mirror of https://github.com/oxen-io/lokinet
Use more fmt
This commit is contained in:
parent
eec8244a6c
commit
784f2938f1
|
@ -149,45 +149,23 @@ namespace llarp
|
|||
std::string
|
||||
friendly_duration(std::chrono::nanoseconds dur)
|
||||
{
|
||||
std::ostringstream os;
|
||||
bool some = false;
|
||||
if (dur >= 24h)
|
||||
{
|
||||
os << dur / 24h << 'd';
|
||||
dur %= 24h;
|
||||
some = true;
|
||||
}
|
||||
if (dur >= 1h || some)
|
||||
{
|
||||
os << dur / 1h << 'h';
|
||||
dur %= 1h;
|
||||
some = true;
|
||||
}
|
||||
if (dur >= 1min || some)
|
||||
{
|
||||
os << dur / 1min << 'm';
|
||||
dur %= 1min;
|
||||
some = true;
|
||||
}
|
||||
if (some)
|
||||
{
|
||||
// If we have >= minutes then don't bother with fractional seconds
|
||||
os << dur / 1s << 's';
|
||||
}
|
||||
else
|
||||
{
|
||||
double seconds = std::chrono::duration<double>(dur).count();
|
||||
os.precision(3);
|
||||
if (dur >= 1s)
|
||||
os << seconds << "s";
|
||||
else if (dur >= 1ms)
|
||||
os << seconds * 1000 << "ms";
|
||||
else if (dur >= 1us)
|
||||
os << seconds * 1'000'000 << u8"µs";
|
||||
else
|
||||
os << seconds * 1'000'000'000 << "ns";
|
||||
}
|
||||
return os.str();
|
||||
const double dsecs = std::chrono::duration<double>(dur).count();
|
||||
return fmt::format(
|
||||
dur >= 24h ? "{0}d{1}h{2}m{3}s"
|
||||
: dur >= 1h ? "{1}h{2}m{3}s"
|
||||
: dur >= 1min ? "{2}m{3}s"
|
||||
: dur >= 1s ? "{4:.3f}s"
|
||||
: dur >= 1ms ? "{5:.3f}s"
|
||||
: dur >= 1us ? u8"{6:.3f}µs"
|
||||
: "{7}ns",
|
||||
dur / 24h,
|
||||
dur / 1h,
|
||||
dur / 1min,
|
||||
dur / 1s,
|
||||
dsecs,
|
||||
dsecs * 1'000,
|
||||
dsecs * 1'000'000,
|
||||
dur.count());
|
||||
}
|
||||
|
||||
} // namespace llarp
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
#pragma once
|
||||
|
||||
#include <string_view>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <chrono>
|
||||
#include <iterator>
|
||||
#include <charconv>
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
bool
|
||||
|
@ -96,17 +97,12 @@ namespace llarp
|
|||
split_any(std::string_view str, std::string_view delims, bool trim = false);
|
||||
|
||||
/// Joins [begin, end) with a delimiter and returns the resulting string. Elements can be
|
||||
/// anything that can be sent to an ostream via `<<`.
|
||||
/// anything that is fmt formattable.
|
||||
template <typename It>
|
||||
std::string
|
||||
join(std::string_view delimiter, It begin, It end)
|
||||
{
|
||||
std::ostringstream o;
|
||||
if (begin != end)
|
||||
o << *begin++;
|
||||
while (begin != end)
|
||||
o << delimiter << *begin++;
|
||||
return o.str();
|
||||
return fmt::format("{}", fmt::join(delimiter, begin, end));
|
||||
}
|
||||
|
||||
/// Wrapper around the above that takes a container and passes c.begin(), c.end() to the above.
|
||||
|
|
|
@ -8,6 +8,8 @@
|
|||
#include <llarp/ev/vpn.hpp>
|
||||
#include <llarp/router/abstractrouter.hpp>
|
||||
|
||||
#include <fmt/std.h>
|
||||
|
||||
// DDK macros
|
||||
#define CTL_CODE(DeviceType, Function, Method, Access) \
|
||||
(((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method))
|
||||
|
@ -237,9 +239,7 @@ namespace llarp::vpn
|
|||
LogError("cannot query registry");
|
||||
throw std::invalid_argument{"cannot query registery"};
|
||||
}
|
||||
std::stringstream ss;
|
||||
ss << "\\\\.\\Global\\" << device_id << ".tap";
|
||||
const auto fname = ss.str();
|
||||
const auto fname = fmt::format(R"(\\.\Global\{}.tap)", device_id);
|
||||
m_Device = CreateFile(
|
||||
fname.c_str(),
|
||||
GENERIC_WRITE | GENERIC_READ,
|
||||
|
@ -537,16 +537,8 @@ namespace llarp::vpn
|
|||
void
|
||||
Route(IPVariant_t ip, IPVariant_t gateway, std::string cmd)
|
||||
{
|
||||
std::stringstream ss;
|
||||
std::string ip_str;
|
||||
std::string gateway_str;
|
||||
|
||||
std::visit([&ip_str](auto&& ip) { ip_str = ip.ToString(); }, ip);
|
||||
std::visit([&gateway_str](auto&& gateway) { gateway_str = gateway.ToString(); }, gateway);
|
||||
|
||||
ss << RouteCommand() << " " << cmd << " " << ip_str << " MASK 255.255.255.255 " << gateway_str
|
||||
<< " METRIC 2";
|
||||
Execute(ss.str());
|
||||
Execute(fmt::format(
|
||||
"{} {} {} MASK 255.255.255.255 {} METRIC 2", RouteCommand(), cmd, ip, gateway));
|
||||
}
|
||||
|
||||
void
|
||||
|
|
Loading…
Reference in New Issue