2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
2019-04-11 12:58:23 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/util/time.hpp>
|
2019-07-16 22:55:58 +00:00
|
|
|
|
2019-04-11 12:58:23 +00:00
|
|
|
#include <ctime>
|
2019-07-16 22:55:58 +00:00
|
|
|
#include <sstream>
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/util/thread/threading.hpp>
|
2021-03-11 01:02:08 +00:00
|
|
|
#include <type_traits>
|
2019-04-11 12:58:23 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
/** internal */
|
2021-03-11 01:02:08 +00:00
|
|
|
|
|
|
|
// true if T is the same as any of V...
|
|
|
|
template <typename T, typename... V>
|
|
|
|
constexpr bool is_same_any_v = (std::is_same_v<T, V> || ...);
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename TArg, typename... TArgs>
|
2019-04-11 12:58:23 +00:00
|
|
|
void
|
2021-03-11 01:02:08 +00:00
|
|
|
LogAppend(std::ostringstream& ss, TArg&& arg, TArgs&&... args) noexcept
|
2019-04-11 12:58:23 +00:00
|
|
|
{
|
2021-03-11 01:02:08 +00:00
|
|
|
// If you are logging a char/unsigned char/uint8_t then promote it to an integer so that we
|
|
|
|
// print numeric values rather than std::ostream's default of printing it as a raw char.
|
|
|
|
using PlainT = std::remove_reference_t<TArg>;
|
2021-03-23 18:19:54 +00:00
|
|
|
if constexpr (is_same_any_v<PlainT, char, unsigned char, signed char, uint8_t>)
|
2021-03-12 13:50:21 +00:00
|
|
|
ss << +std::forward<TArg>(arg); // Promote to int
|
2021-03-23 18:19:54 +00:00
|
|
|
else if constexpr (std::is_same_v<PlainT, std::byte>)
|
|
|
|
ss << std::to_integer<int>(arg);
|
2021-03-11 01:02:08 +00:00
|
|
|
else
|
2021-03-12 13:50:21 +00:00
|
|
|
ss << std::forward<TArg>(arg);
|
2021-03-26 14:18:39 +00:00
|
|
|
if constexpr (sizeof...(TArgs) > 0)
|
2021-03-12 13:50:21 +00:00
|
|
|
LogAppend(ss, std::forward<TArgs>(args)...);
|
2019-04-11 12:58:23 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 22:55:58 +00:00
|
|
|
inline std::string
|
2019-04-11 12:58:23 +00:00
|
|
|
thread_id_string()
|
|
|
|
{
|
|
|
|
auto tid = std::this_thread::get_id();
|
2020-04-07 18:38:56 +00:00
|
|
|
std::hash<std::thread::id> h;
|
2019-04-11 12:58:23 +00:00
|
|
|
uint16_t id = h(tid) % 1000;
|
2020-05-17 18:00:15 +00:00
|
|
|
#if defined(ANDROID)
|
2019-04-11 12:58:23 +00:00
|
|
|
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;
|
2019-06-10 13:20:48 +00:00
|
|
|
const llarp_time_t now;
|
|
|
|
const llarp_time_t delta;
|
2019-04-11 12:58:23 +00:00
|
|
|
|
2019-06-10 13:20:48 +00:00
|
|
|
log_timestamp();
|
2019-04-11 12:58:23 +00:00
|
|
|
|
2019-06-10 13:20:48 +00:00
|
|
|
explicit log_timestamp(const char* fmt);
|
2019-05-18 15:34:03 +00:00
|
|
|
};
|
|
|
|
|
2020-02-22 23:01:36 +00:00
|
|
|
std::ostream&
|
|
|
|
operator<<(std::ostream& out, const log_timestamp& ts);
|
2019-04-11 12:58:23 +00:00
|
|
|
|
|
|
|
} // namespace llarp
|