lokinet/llarp/util/time.cpp
Jason Rhinelander b81f7025c9
Replace logging with oxen-logger
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).
2022-07-15 22:17:59 -03:00

53 lines
1.0 KiB
C++

#include "time.hpp"
#include <chrono>
#include <iomanip>
namespace llarp
{
namespace
{
using Clock_t = std::chrono::system_clock;
template <typename Res, typename Clock>
static Duration_t
time_since_epoch(std::chrono::time_point<Clock> point)
{
return std::chrono::duration_cast<Res>(point.time_since_epoch());
}
const static auto started_at_system = Clock_t::now();
const static auto started_at_steady = std::chrono::steady_clock::now();
} // namespace
uint64_t
ToMS(Duration_t ms)
{
return ms.count();
}
/// get our uptime in ms
Duration_t
uptime()
{
return std::chrono::duration_cast<Duration_t>(
std::chrono::steady_clock::now() - started_at_steady);
}
Duration_t
time_now_ms()
{
auto t = uptime();
#ifdef TESTNET_SPEED
t /= uint64_t{TESTNET_SPEED};
#endif
return t + time_since_epoch<Duration_t, Clock_t>(started_at_system);
}
nlohmann::json
to_json(const Duration_t& t)
{
return ToMS(t);
}
} // namespace llarp