no-op LogTrace(); print chars as ints

- LogTrace() (and LogTraceTag, etc.) are now no-ops for release builds.
(hoping there are no side effects in trace logging!)
- renamed llarp::_Log to llarp::_log because _Log is a reserved keyword
- change logging code to implicitly convert 1-byte types (char, unsigned
char, uint8_t) to ints so that we print them as numeric values rather
than raw chars because, more often than not, printing a single char is
trying to log an 8-bit value.
pull/1576/head
Jason Rhinelander 3 years ago committed by Jeff Becker
parent b61bd82b4b
commit 39d31df059
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -82,34 +82,47 @@ namespace llarp
/** internal */
template <typename... TArgs>
inline static void
_Log(LogLevel lvl, const char* fname, int lineno, TArgs&&... args) noexcept
_log(LogLevel lvl, const char* fname, int lineno, TArgs&&... args) noexcept
{
auto& log = LogContext::Instance();
if (log.curLevel > lvl || log.logStream == nullptr)
return;
std::stringstream ss;
LogAppend(ss, std::forward<TArgs>(args)...);
std::ostringstream ss;
if constexpr (sizeof...(args))
LogAppend(ss, std::forward<TArgs>(args)...);
log.logStream->AppendLog(lvl, fname, lineno, log.nodeName, ss.str());
}
inline void _log_noop() noexcept
{}
} // namespace llarp
#define LogTrace(...) _Log(llarp::eLogTrace, LOG_TAG, __LINE__, __VA_ARGS__)
#define LogDebug(...) _Log(llarp::eLogDebug, LOG_TAG, __LINE__, __VA_ARGS__)
#define LogInfo(...) _Log(llarp::eLogInfo, LOG_TAG, __LINE__, __VA_ARGS__)
#define LogWarn(...) _Log(llarp::eLogWarn, LOG_TAG, __LINE__, __VA_ARGS__)
#define LogError(...) _Log(llarp::eLogError, LOG_TAG, __LINE__, __VA_ARGS__)
#define LogTraceTag(tag, ...) _Log(llarp::eLogTrace, tag, __LINE__, __VA_ARGS__)
#define LogDebugTag(tag, ...) _Log(llarp::eLogDebug, tag, __LINE__, __VA_ARGS__)
#define LogInfoTag(tag, ...) _Log(llarp::eLogInfo, tag, __LINE__, __VA_ARGS__)
#define LogWarnTag(tag, ...) _Log(llarp::eLogWarn, tag, __LINE__, __VA_ARGS__)
#define LogErrorTag(tag, ...) _Log(llarp::eLogError, tag, __LINE__, __VA_ARGS__)
#define LogTraceExplicit(tag, line, ...) _Log(llarp::eLogTrace, tag, line, __VA_ARGS__)
#define LogDebugExplicit(tag, line, ...) _Log(llarp::eLogDebug, tag, line, __VA_ARGS__)
#define LogInfoExplicit(tag, line, ...) _Log(llarp::eLogInfo, tag, line __VA_ARGS__)
#define LogWarnExplicit(tag, line, ...) _Log(llarp::eLogWarn, tag, line, __VA_ARGS__)
#define LogErrorExplicit(tag, line, ...) _Log(llarp::eLogError, tag, line, __VA_ARGS__)
#define LogDebug(...) _log(llarp::eLogDebug, LOG_TAG, __LINE__, __VA_ARGS__)
#define LogInfo(...) _log(llarp::eLogInfo, LOG_TAG, __LINE__, __VA_ARGS__)
#define LogWarn(...) _log(llarp::eLogWarn, LOG_TAG, __LINE__, __VA_ARGS__)
#define LogError(...) _log(llarp::eLogError, LOG_TAG, __LINE__, __VA_ARGS__)
#define LogDebugTag(tag, ...) _log(llarp::eLogDebug, tag, __LINE__, __VA_ARGS__)
#define LogInfoTag(tag, ...) _log(llarp::eLogInfo, tag, __LINE__, __VA_ARGS__)
#define LogWarnTag(tag, ...) _log(llarp::eLogWarn, tag, __LINE__, __VA_ARGS__)
#define LogErrorTag(tag, ...) _log(llarp::eLogError, tag, __LINE__, __VA_ARGS__)
#define LogDebugExplicit(tag, line, ...) _log(llarp::eLogDebug, tag, line, __VA_ARGS__)
#define LogInfoExplicit(tag, line, ...) _log(llarp::eLogInfo, tag, line __VA_ARGS__)
#define LogWarnExplicit(tag, line, ...) _log(llarp::eLogWarn, tag, line, __VA_ARGS__)
#define LogErrorExplicit(tag, line, ...) _log(llarp::eLogError, tag, line, __VA_ARGS__)
// null-op Trace logging if this is a release build
#ifdef NDEBUG
#define LogTrace(...) _log_noop()
#define LogTraceTag(tag, ...) _log_noop()
#define LogTraceExplicit(tag, line, ...) _log_noop()
#else
#define LogTrace(...) _log(llarp::eLogTrace, LOG_TAG, __LINE__, __VA_ARGS__)
#define LogTraceTag(tag, ...) _log(llarp::eLogTrace, tag, __LINE__, __VA_ARGS__)
#define LogTraceExplicit(tag, line, ...) _log(llarp::eLogTrace, tag, line, __VA_ARGS__)
#endif
#ifndef LOG_TAG
#define LOG_TAG "default"

@ -5,20 +5,29 @@
#include <ctime>
#include <sstream>
#include <llarp/util/thread/threading.hpp>
#include <type_traits>
namespace llarp
{
/** internal, recursion terminator */
constexpr void
LogAppend(std::stringstream&) noexcept
{}
/** internal */
// 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> || ...);
template <typename TArg, typename... TArgs>
void
LogAppend(std::stringstream& ss, TArg&& arg, TArgs&&... args) noexcept
LogAppend(std::ostringstream& ss, TArg&& arg, TArgs&&... args) noexcept
{
ss << std::forward<TArg>(arg);
LogAppend(ss, std::forward<TArgs>(args)...);
// 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>;
if constexpr (is_same_any_v<PlainT, char, unsigned char, signed char, uint8_t, std::byte>)
ss << +std::forward<TArg>(arg); // Promote to int
else
ss << std::forward<TArg>(arg);
if constexpr (sizeof...(TArgs))
LogAppend(ss, std::forward<TArgs>(args)...);
}
inline std::string

Loading…
Cancel
Save