lokinet/llarp/util/logging/logger_internal.hpp
Jason Rhinelander 2e9840ea39 Replace abseil date code with Hinnart's date.h
Howard Hinnart's date.h is the library that was accepted as C++20
date/calendar support, so this is essentially a backport of C++20 date
time support.

(It does support timezone support, but requires more of the library and
that seems like overkill for what we need; this just prints UTC
timestamps instead, which need only a header-only include).
2020-02-24 14:27:44 -04:00

58 lines
1.1 KiB
C++

#ifndef LLARP_UTIL_LOGGER_INTERNAL_HPP
#define LLARP_UTIL_LOGGER_INTERNAL_HPP
#include <util/time.hpp>
#include <ctime>
#include <sstream>
#include <util/thread/threading.hpp>
namespace llarp
{
/** internal, recursion terminator */
constexpr void
LogAppend(std::stringstream&) noexcept
{
}
/** internal */
template < typename TArg, typename... TArgs >
void
LogAppend(std::stringstream& ss, TArg&& arg, TArgs&&... args) noexcept
{
ss << std::forward< TArg >(arg);
LogAppend(ss, std::forward< TArgs >(args)...);
}
inline std::string
thread_id_string()
{
auto tid = std::this_thread::get_id();
std::hash< std::thread::id > h;
uint16_t id = h(tid) % 1000;
#if defined(ANDROID) || defined(RPI)
char buff[8] = {0};
snprintf(buff, sizeof(buff), "%u", id);
return buff;
#else
return std::to_string(id);
#endif
}
struct log_timestamp
{
const char* format;
const llarp_time_t now;
const llarp_time_t delta;
log_timestamp();
explicit log_timestamp(const char* fmt);
};
std::ostream&
operator<<(std::ostream& out, const log_timestamp& ts);
} // namespace llarp
#endif