lokinet/llarp/util/logger_internal.hpp

73 lines
1.5 KiB
C++
Raw Normal View History

2019-04-11 12:58:23 +00:00
#ifndef LLARP_UTIL_LOGGER_INTERNAL_HPP
#define LLARP_UTIL_LOGGER_INTERNAL_HPP
2019-07-16 22:55:58 +00:00
#include <util/time.hpp>
#include <absl/time/clock.h>
#include <absl/time/time.h>
2019-04-11 12:58:23 +00:00
#include <ctime>
#include <iomanip>
2019-07-16 22:55:58 +00:00
#include <sstream>
2019-04-11 12:58:23 +00:00
#include <thread>
namespace llarp
{
/** internal */
template < typename TArg >
void
LogAppend(std::stringstream& ss, TArg&& arg) noexcept
{
ss << std::forward< TArg >(arg);
}
/** internal */
template < typename TArg, typename... TArgs >
void
LogAppend(std::stringstream& ss, TArg&& arg, TArgs&&... args) noexcept
{
LogAppend(ss, std::forward< TArg >(arg));
LogAppend(ss, std::forward< TArgs >(args)...);
}
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();
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;
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
};
inline std::ostream&
operator<<(std::ostream& out, const log_timestamp& ts)
{
2019-04-11 12:58:23 +00:00
#if defined(ANDROID) || defined(RPI)
2019-05-18 15:34:03 +00:00
(void)ts;
2019-06-10 13:20:48 +00:00
return out << ts.now << " [+" << ts.delta << " ms]";
2019-04-11 12:58:23 +00:00
#else
2019-05-18 15:34:03 +00:00
absl::TimeZone tz = absl::LocalTimeZone();
2019-06-10 13:20:48 +00:00
return out << absl::FormatTime(ts.format, absl::FromUnixMillis(ts.now), tz)
<< " [+" << ts.delta << " ms]";
2019-04-11 12:58:23 +00:00
#endif
2019-05-18 15:34:03 +00:00
}
2019-04-11 12:58:23 +00:00
} // namespace llarp
#endif