From 5c457ff48629d3be4c2c9f384430f7ae9f5f3fa5 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Tue, 11 May 2021 07:22:36 -0400 Subject: [PATCH 01/10] refactor logging to use std::source_location * use std::source_location instead of godawful macros in logging * remove unused/absolutely haram af json logstream * fix bug in android logger where it doesn't respect eLogNone --- cmake/add_log_tag.cmake | 3 +- daemon/lokinet.cpp | 6 +- llarp/CMakeLists.txt | 1 - llarp/config/config.cpp | 1 - llarp/dns/message.cpp | 6 +- llarp/router/router.cpp | 1 - llarp/util/bencode.hpp | 7 +- llarp/util/logging/android_logger.cpp | 7 +- llarp/util/logging/android_logger.hpp | 4 +- llarp/util/logging/file_logger.cpp | 10 +- llarp/util/logging/file_logger.hpp | 6 +- llarp/util/logging/json_logger.cpp | 24 ----- llarp/util/logging/json_logger.hpp | 28 ------ llarp/util/logging/logger.cpp | 21 +---- llarp/util/logging/logger.hpp | 108 +++++++++++++++------- llarp/util/logging/logger_syslog.hpp | 4 +- llarp/util/logging/logstream.hpp | 11 ++- llarp/util/logging/ostream_logger.cpp | 6 +- llarp/util/logging/ostream_logger.hpp | 4 +- llarp/util/logging/source_location.hpp | 9 ++ llarp/util/logging/syslog_logger.cpp | 6 +- llarp/util/logging/win32_logger.cpp | 8 +- llarp/util/logging/win32_logger.hpp | 4 +- llarp/util/str.hpp | 9 ++ llarp/vpn/win32.hpp | 2 +- pybind/CMakeLists.txt | 2 +- test/CMakeLists.txt | 2 +- test/router/test_llarp_router_version.cpp | 2 +- 28 files changed, 144 insertions(+), 158 deletions(-) delete mode 100644 llarp/util/logging/json_logger.cpp delete mode 100644 llarp/util/logging/json_logger.hpp create mode 100644 llarp/util/logging/source_location.hpp diff --git a/cmake/add_log_tag.cmake b/cmake/add_log_tag.cmake index 1bde3029d..b86ab5717 100644 --- a/cmake/add_log_tag.cmake +++ b/cmake/add_log_tag.cmake @@ -2,7 +2,6 @@ function(add_log_tag target) get_target_property(TARGET_SRCS ${target} SOURCES) foreach(F ${TARGET_SRCS}) get_filename_component(fpath "${F}" ABSOLUTE) - string(REPLACE "${PROJECT_SOURCE_DIR}/" "" logtag "${fpath}") - set_property(SOURCE ${F} APPEND PROPERTY COMPILE_DEFINITIONS LOG_TAG=\"${logtag}\") + set_property(SOURCE ${F} APPEND PROPERTY COMPILE_DEFINITIONS SOURCE_ROOT=\"${PROJECT_SOURCE_DIR}\") endforeach() endfunction() diff --git a/daemon/lokinet.cpp b/daemon/lokinet.cpp index 4acff0272..a4e552dc0 100644 --- a/daemon/lokinet.cpp +++ b/daemon/lokinet.cpp @@ -496,7 +496,7 @@ lokinet_main(int argc, char* argv[]) } catch (std::exception& ex) { - LogError("cannot generate config at ", *configFile, ": ", ex.what()); + llarp::LogError("cannot generate config at ", *configFile, ": ", ex.what()); return 1; } } @@ -512,7 +512,7 @@ lokinet_main(int argc, char* argv[]) } catch (std::exception& ex) { - LogError("cannot check if ", *configFile, " exists: ", ex.what()); + llarp::LogError("cannot check if ", *configFile, " exists: ", ex.what()); return 1; } } @@ -574,7 +574,7 @@ lokinet_main(int argc, char* argv[]) "file a bug report now or be cursed with this " "annoying image in your syslog for all time."}) { - LogError(wtf); + llarp::LogError{wtf}; llarp::LogContext::Instance().ImmediateFlush(); } #ifdef _WIN32 diff --git a/llarp/CMakeLists.txt b/llarp/CMakeLists.txt index b35b8497e..05a60bbc3 100644 --- a/llarp/CMakeLists.txt +++ b/llarp/CMakeLists.txt @@ -10,7 +10,6 @@ add_library(lokinet-util util/logging/android_logger.cpp util/logging/buffer.cpp util/logging/file_logger.cpp - util/logging/json_logger.cpp util/logging/logger.cpp util/logging/logger_internal.cpp util/logging/loglevel.cpp diff --git a/llarp/config/config.cpp b/llarp/config/config.cpp index 4e1b4c80e..66dabf4f8 100644 --- a/llarp/config/config.cpp +++ b/llarp/config/config.cpp @@ -1048,7 +1048,6 @@ namespace llarp Comment{ "Log type (format). Valid options are:", " file - plaintext formatting", - " json - json-formatted log statements", " syslog - logs directed to syslog", }); diff --git a/llarp/dns/message.cpp b/llarp/dns/message.cpp index f10e9b9c6..0d78d1fdb 100644 --- a/llarp/dns/message.cpp +++ b/llarp/dns/message.cpp @@ -117,16 +117,16 @@ namespace llarp { if (!qd.Decode(buf)) { - llarp::LogError("failed to decode question"); + LogError("failed to decode question"); return false; } - llarp::LogDebug(qd); + LogDebug("dns question: ", qd); } for (auto& an : answers) { if (not an.Decode(buf)) { - llarp::LogDebug("failed to decode answer"); + LogDebug("failed to decode answer"); return false; } } diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index 4fa608afc..5655a3092 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -15,7 +15,6 @@ #include #include #include -#include #include #include #include diff --git a/llarp/util/bencode.hpp b/llarp/util/bencode.hpp index cfb49c849..7ccabdcab 100644 --- a/llarp/util/bencode.hpp +++ b/llarp/util/bencode.hpp @@ -75,7 +75,7 @@ namespace llarp { if (!item.BDecode(buf)) { - llarp::LogWarnTag("llarp/bencode.hpp", "failed to decode key ", k, " for entry in dict"); + llarp::LogWarn("failed to decode key ", k, " for entry in dict"); return false; } @@ -94,7 +94,7 @@ namespace llarp uint64_t read_i; if (!bencode_read_integer(buf, &read_i)) { - llarp::LogWarnTag("llarp/BEncode.hpp", "failed to decode key ", k, " for integer in dict"); + llarp::LogWarn("failed to decode key ", k, " for integer in dict"); return false; } @@ -210,8 +210,7 @@ namespace llarp return true; if (sink.DecodeKey(*key, buffer)) return true; - llarp::LogWarnTag( - "llarp/bencode.hpp", "undefined key '", *key->cur, "' for entry in dict"); + llarp::LogWarn("undefined key '", *key->cur, "' for entry in dict"); return false; }, diff --git a/llarp/util/logging/android_logger.cpp b/llarp/util/logging/android_logger.cpp index c9d9de01f..3cf9343cb 100644 --- a/llarp/util/logging/android_logger.cpp +++ b/llarp/util/logging/android_logger.cpp @@ -8,7 +8,8 @@ namespace llarp { void AndroidLogStream::PreLog( - std::stringstream& ss, LogLevel lvl, const char* fname, int lineno, const std::string&) const + std::stringstream& ss, LogLevel lvl, std::string_view fname, int lineno, const std::string&) + const { switch (lvl) { @@ -43,7 +44,7 @@ namespace llarp {} void - AndroidLogStream::Print(LogLevel lvl, const char* tag, const std::string& msg) + AndroidLogStream::Print(LogLevel lvl, std::string_view tag, const std::string& msg) { std::string str("lokinet|"); str += tag; @@ -62,6 +63,8 @@ namespace llarp case eLogError: __android_log_write(ANDROID_LOG_ERROR, str.c_str(), msg.c_str()); return; + default: + return; } } // namespace llarp diff --git a/llarp/util/logging/android_logger.hpp b/llarp/util/logging/android_logger.hpp index 15345776e..e42989910 100644 --- a/llarp/util/logging/android_logger.hpp +++ b/llarp/util/logging/android_logger.hpp @@ -12,12 +12,12 @@ namespace llarp PreLog( std::stringstream& s, LogLevel lvl, - const char* fname, + std::string_view filename, int lineno, const std::string& nodename) const override; void - Print(LogLevel lvl, const char* filename, const std::string& msg) override; + Print(LogLevel lvl, std::string_view filename, const std::string& msg) override; void PostLog(std::stringstream&) const override; diff --git a/llarp/util/logging/file_logger.cpp b/llarp/util/logging/file_logger.cpp index 229b9c148..c41d092e8 100644 --- a/llarp/util/logging/file_logger.cpp +++ b/llarp/util/logging/file_logger.cpp @@ -65,18 +65,18 @@ namespace llarp FileLogStream::PreLog( std::stringstream& ss, LogLevel lvl, - const char* fname, + std::string_view filename, int lineno, const std::string& nodename) const { ss << "[" << LogLevelToString(lvl) << "] "; ss << "[" << nodename << "]" - << "(" << thread_id_string() << ") " << log_timestamp() << " " << fname << ":" << lineno + << "(" << thread_id_string() << ") " << log_timestamp() << " " << filename << ":" << lineno << "\t"; } void - FileLogStream::Print(LogLevel, const char*, const std::string& msg) + FileLogStream::Print(LogLevel, std::string_view, const std::string& msg) { m_Lines.pushBack(msg); } @@ -84,12 +84,12 @@ namespace llarp void FileLogStream::AppendLog( LogLevel lvl, - const char* fname, + std::string_view filename, int lineno, const std::string& nodename, const std::string msg) { - ILogStream::AppendLog(lvl, fname, lineno, nodename, msg); + ILogStream::AppendLog(lvl, filename, lineno, nodename, msg); Tick(llarp::time_now_ms()); } diff --git a/llarp/util/logging/file_logger.hpp b/llarp/util/logging/file_logger.hpp index 7b6bfbd49..07af96102 100644 --- a/llarp/util/logging/file_logger.hpp +++ b/llarp/util/logging/file_logger.hpp @@ -23,12 +23,12 @@ namespace llarp PreLog( std::stringstream& out, LogLevel lvl, - const char* fname, + std::string_view filename, int lineno, const std::string& nodename) const override; void - Print(LogLevel, const char*, const std::string& msg) override; + Print(LogLevel, std::string_view filename, const std::string& msg) override; void Tick(llarp_time_t now) override; @@ -39,7 +39,7 @@ namespace llarp void AppendLog( LogLevel lvl, - const char* fname, + std::string_view filename, int lineno, const std::string& nodename, const std::string msg) override; diff --git a/llarp/util/logging/json_logger.cpp b/llarp/util/logging/json_logger.cpp deleted file mode 100644 index 119e910f3..000000000 --- a/llarp/util/logging/json_logger.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "json_logger.hpp" -#include - -namespace llarp -{ - void - JSONLogStream::AppendLog( - LogLevel lvl, - const char* fname, - int lineno, - const std::string& nodename, - const std::string msg) - { - json::Object obj; - obj["time"] = to_json(llarp::time_now_ms()); - obj["nickname"] = nodename; - obj["file"] = std::string(fname); - obj["line"] = lineno; - obj["level"] = LogLevelToString(lvl); - obj["message"] = msg; - m_Lines.pushBack(obj.dump()); - } - -} // namespace llarp diff --git a/llarp/util/logging/json_logger.hpp b/llarp/util/logging/json_logger.hpp deleted file mode 100644 index af0476b6f..000000000 --- a/llarp/util/logging/json_logger.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef LLARP_UTIL_JSON_LOGGER -#define LLARP_UTIL_JSON_LOGGER - -#include "file_logger.hpp" - -namespace llarp -{ - struct JSONLogStream : public FileLogStream - { - JSONLogStream( - std::function disk, - FILE* f, - llarp_time_t flushInterval, - bool closeFile) - : FileLogStream(std::move(disk), f, flushInterval, closeFile) - {} - - void - AppendLog( - LogLevel lvl, - const char* fname, - int lineno, - const std::string& nodename, - const std::string msg) override; - }; -} // namespace llarp - -#endif diff --git a/llarp/util/logging/logger.cpp b/llarp/util/logging/logger.cpp index 4730e32b3..3357ab291 100644 --- a/llarp/util/logging/logger.cpp +++ b/llarp/util/logging/logger.cpp @@ -2,7 +2,6 @@ #include "ostream_logger.hpp" #include "logger_syslog.hpp" #include "file_logger.hpp" -#include "json_logger.hpp" #if defined(_WIN32) #include "win32_logger.hpp" #endif @@ -36,8 +35,6 @@ namespace llarp return LogType::Unknown; else if (str == "file") return LogType::File; - else if (str == "json") - return LogType::Json; else if (str == "syslog") return LogType::Syslog; @@ -111,7 +108,7 @@ namespace llarp { logfile = stdout; } - else + else if (type != LogType::Syslog) { logfile = ::fopen(file.c_str(), "a"); if (not logfile) @@ -141,26 +138,10 @@ namespace llarp } break; - case LogType::Json: - LogInfo("Switching logger to JSON with file: ", file); - std::cout << std::flush; - - LogContext::Instance().logStream = - std::make_unique(io, logfile, 100ms, logfile != stdout); - break; case LogType::Syslog: - if (logfile) - { - // TODO: this logic should be handled in Config - // TODO: this won't even work because of default value for 'file' (== "stdout") - ::fclose(logfile); - throw std::invalid_argument("Cannot mix log type=syslog and file=*"); - } #if defined(_WIN32) throw std::runtime_error("syslog not supported on win32"); #else - LogInfo("Switching logger to syslog"); - std::cout << std::flush; LogContext::Instance().logStream = std::make_unique(); #endif break; diff --git a/llarp/util/logging/logger.hpp b/llarp/util/logging/logger.hpp index 599242147..0c20ac38a 100644 --- a/llarp/util/logging/logger.hpp +++ b/llarp/util/logging/logger.hpp @@ -1,9 +1,11 @@ #pragma once #include +#include #include #include "logstream.hpp" #include "logger_internal.hpp" +#include "source_location.hpp" namespace llarp { @@ -11,7 +13,6 @@ namespace llarp { Unknown = 0, File, - Json, Syslog, }; LogType @@ -80,7 +81,7 @@ namespace llarp /** internal */ template inline static void - _log(LogLevel lvl, const char* fname, int lineno, TArgs&&... args) noexcept + _log(LogLevel lvl, const slns::source_location& location, TArgs&&... args) noexcept { auto& log = LogContext::Instance(); if (log.curLevel > lvl || log.logStream == nullptr) @@ -88,41 +89,80 @@ namespace llarp std::ostringstream ss; if constexpr (sizeof...(args) > 0) LogAppend(ss, std::forward(args)...); - log.logStream->AppendLog(lvl, fname, lineno, log.nodeName, ss.str()); + log.logStream->AppendLog( + lvl, + strip_prefix(location.file_name(), SOURCE_ROOT), + location.line(), + log.nodeName, + ss.str()); } - inline void - _log_noop() noexcept - {} + template + struct LogTrace + { + LogTrace( + [[maybe_unused]] T... args, + [[maybe_unused]] const slns::source_location& location = slns::source_location::current()) + { +#ifndef NDEBUG + _log(eLogTrace, location, std::forward(args)...); +#endif + } + }; -} // namespace llarp + template + LogTrace(T&&...) -> LogTrace; -#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__) + template + struct LogDebug + { + LogDebug( + [[maybe_unused]] T... args, + [[maybe_unused]] const slns::source_location& location = slns::source_location::current()) + { +#ifndef NDEBUG + _log(eLogDebug, location, std::forward(args)...); #endif + } + }; -#ifndef LOG_TAG -#define LOG_TAG "default" -#endif + template + LogDebug(T&&...) -> LogDebug; + + template + struct LogInfo + { + LogInfo(T... args, const slns::source_location& location = slns::source_location::current()) + { + _log(eLogInfo, location, std::forward(args)...); + } + }; + + template + LogInfo(T&&...) -> LogInfo; + + template + struct LogWarn + { + LogWarn(T... args, const slns::source_location& location = slns::source_location::current()) + { + _log(eLogWarn, location, std::forward(args)...); + } + }; + + template + LogWarn(T&&...) -> LogWarn; + + template + struct LogError + { + LogError(T... args, const slns::source_location& location = slns::source_location::current()) + { + _log(eLogError, location, std::forward(args)...); + } + }; + + template + LogError(T&&...) -> LogError; + +} // namespace llarp diff --git a/llarp/util/logging/logger_syslog.hpp b/llarp/util/logging/logger_syslog.hpp index 71d436451..dee7523de 100644 --- a/llarp/util/logging/logger_syslog.hpp +++ b/llarp/util/logging/logger_syslog.hpp @@ -11,12 +11,12 @@ namespace llarp PreLog( std::stringstream& s, LogLevel lvl, - const char* fname, + std::string_view filename, int lineno, const std::string& nodename) const override; void - Print(LogLevel lvl, const char* tag, const std::string& msg) override; + Print(LogLevel lvl, std::string_view tag, const std::string& msg) override; void PostLog(std::stringstream& ss) const override; diff --git a/llarp/util/logging/logstream.hpp b/llarp/util/logging/logstream.hpp index e7b0767a4..d5e00d90d 100644 --- a/llarp/util/logging/logstream.hpp +++ b/llarp/util/logging/logstream.hpp @@ -6,6 +6,7 @@ #include #include #include +#include namespace llarp { @@ -18,12 +19,12 @@ namespace llarp PreLog( std::stringstream& out, LogLevel lvl, - const char* fname, + std::string_view filename, int lineno, const std::string& nodename) const = 0; virtual void - Print(LogLevel lvl, const char* filename, const std::string& msg) = 0; + Print(LogLevel lvl, std::string_view filename, const std::string& msg) = 0; virtual void PostLog(std::stringstream& out) const = 0; @@ -31,16 +32,16 @@ namespace llarp virtual void AppendLog( LogLevel lvl, - const char* fname, + std::string_view filename, int lineno, const std::string& nodename, const std::string msg) { std::stringstream ss; - PreLog(ss, lvl, fname, lineno, nodename); + PreLog(ss, lvl, filename, lineno, nodename); ss << msg; PostLog(ss); - Print(lvl, fname, ss.str()); + Print(lvl, filename, ss.str()); } /// A blocking call to flush to disk. Should only be called in rare circumstances. diff --git a/llarp/util/logging/ostream_logger.cpp b/llarp/util/logging/ostream_logger.cpp index 2b81fbc7f..15078e870 100644 --- a/llarp/util/logging/ostream_logger.cpp +++ b/llarp/util/logging/ostream_logger.cpp @@ -11,7 +11,7 @@ namespace llarp OStreamLogStream::PreLog( std::stringstream& ss, LogLevel lvl, - const char* fname, + std::string_view filename, int lineno, const std::string& nodename) const { @@ -38,7 +38,7 @@ namespace llarp } ss << "[" << LogLevelToString(lvl) << "] "; ss << "[" << nodename << "]" - << "(" << thread_id_string() << ") " << log_timestamp() << " " << fname << ":" << lineno + << "(" << thread_id_string() << ") " << log_timestamp() << " " << filename << ":" << lineno << "\t"; } @@ -51,7 +51,7 @@ namespace llarp } void - OStreamLogStream::Print(LogLevel, const char*, const std::string& msg) + OStreamLogStream::Print(LogLevel, std::string_view, const std::string& msg) { m_Out << msg << std::flush; } diff --git a/llarp/util/logging/ostream_logger.hpp b/llarp/util/logging/ostream_logger.hpp index fa92ab988..5961447e4 100644 --- a/llarp/util/logging/ostream_logger.hpp +++ b/llarp/util/logging/ostream_logger.hpp @@ -15,12 +15,12 @@ namespace llarp PreLog( std::stringstream& s, LogLevel lvl, - const char* fname, + std::string_view filename, int lineno, const std::string& nodename) const override; virtual void - Print(LogLevel lvl, const char* tag, const std::string& msg) override; + Print(LogLevel lvl, std::string_view tag, const std::string& msg) override; void PostLog(std::stringstream& ss) const override; diff --git a/llarp/util/logging/source_location.hpp b/llarp/util/logging/source_location.hpp new file mode 100644 index 000000000..f4699b61b --- /dev/null +++ b/llarp/util/logging/source_location.hpp @@ -0,0 +1,9 @@ +#pragma once + +#ifdef __cpp_lib_source_location +#include +namespace slns = std; +#else +#include +namespace slns = std::experimental; +#endif diff --git a/llarp/util/logging/syslog_logger.cpp b/llarp/util/logging/syslog_logger.cpp index 4c98fe0d9..feb124806 100644 --- a/llarp/util/logging/syslog_logger.cpp +++ b/llarp/util/logging/syslog_logger.cpp @@ -10,18 +10,18 @@ namespace llarp SysLogStream::PreLog( std::stringstream& ss, LogLevel lvl, - const char* fname, + std::string_view filename, int lineno, const std::string& nodename) const { ss << "[" << LogLevelToString(lvl) << "] "; ss << "[" << nodename << "]" - << "(" << thread_id_string() << ") " << log_timestamp() << " " << fname << ":" << lineno + << "(" << thread_id_string() << ") " << log_timestamp() << " " << filename << ":" << lineno << "\t"; } void - SysLogStream::Print(LogLevel lvl, const char*, const std::string& msg) + SysLogStream::Print(LogLevel lvl, std::string_view, const std::string& msg) { switch (lvl) { diff --git a/llarp/util/logging/win32_logger.cpp b/llarp/util/logging/win32_logger.cpp index ee155bb30..2e0448c63 100644 --- a/llarp/util/logging/win32_logger.cpp +++ b/llarp/util/logging/win32_logger.cpp @@ -27,7 +27,7 @@ namespace llarp Win32LogStream::PreLog( std::stringstream& ss, LogLevel lvl, - const char* fname, + std::string_view filename, int lineno, const std::string& nodename) const { @@ -54,11 +54,11 @@ namespace llarp break; } ss << "[" << nodename << "]" - << "(" << thread_id_string() << ") " << log_timestamp() << " " << fname << ":" << lineno + << "(" << thread_id_string() << ") " << log_timestamp() << " " << filename << ":" << lineno << "\t"; } else - OStreamLogStream::PreLog(ss, lvl, fname, lineno, nodename); + OStreamLogStream::PreLog(ss, lvl, filename, lineno, nodename); } void @@ -71,7 +71,7 @@ namespace llarp } void - Win32LogStream::Print(LogLevel lvl, const char*, const std::string& msg) + Win32LogStream::Print(LogLevel lvl, std::string_view, const std::string& msg) { if (!isConsoleModern) { diff --git a/llarp/util/logging/win32_logger.hpp b/llarp/util/logging/win32_logger.hpp index df9e99427..2be5e5609 100644 --- a/llarp/util/logging/win32_logger.hpp +++ b/llarp/util/logging/win32_logger.hpp @@ -14,7 +14,7 @@ namespace llarp PreLog( std::stringstream& s, LogLevel lvl, - const char* fname, + std::string_view filename, int lineno, const std::string& nodename) const override; @@ -24,7 +24,7 @@ namespace llarp void Tick(llarp_time_t) override{}; void - Print(LogLevel lvl, const char*, const std::string& msg) override; + Print(LogLevel lvl, std::string_view, const std::string& msg) override; private: std::ostream& m_Out; diff --git a/llarp/util/str.hpp b/llarp/util/str.hpp index 2fdebe838..c90781c9f 100644 --- a/llarp/util/str.hpp +++ b/llarp/util/str.hpp @@ -70,6 +70,15 @@ namespace llarp return str.size() >= suffix.size() && str.substr(str.size() - suffix.size()) == suffix; } + /// removes a prefix from a string if it exists + inline std::string_view + strip_prefix(std::string_view str, std::string_view prefix) + { + if (starts_with(str, prefix)) + return str.substr(prefix.size()); + return str; + } + /// Splits a string on some delimiter string and returns a vector of string_view's pointing into /// the pieces of the original string. The pieces are valid only as long as the original string /// remains valid. Leading and trailing empty substrings are not removed. If delim is empty you diff --git a/llarp/vpn/win32.hpp b/llarp/vpn/win32.hpp index 06e2c8297..e52c73e53 100644 --- a/llarp/vpn/win32.hpp +++ b/llarp/vpn/win32.hpp @@ -205,7 +205,7 @@ namespace llarp::vpn NetSH(std::string commands) { commands = NetSHCommand() + " interface IPv6 " + commands; - LogInfo(commands); + LogInfo("exec: ", commands); ::system(commands.c_str()); } diff --git a/pybind/CMakeLists.txt b/pybind/CMakeLists.txt index a8706ff50..c2b46ce6e 100644 --- a/pybind/CMakeLists.txt +++ b/pybind/CMakeLists.txt @@ -18,4 +18,4 @@ pybind11_add_module(pyllarp MODULE ) target_link_libraries(pyllarp PUBLIC liblokinet) target_include_directories(pyllarp PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) - +add_log_tag(pyllarp) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 11c05aa5d..0c6c43870 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -53,7 +53,7 @@ add_executable(testAll target_link_libraries(testAll PUBLIC liblokinet Catch2::Catch2) target_include_directories(testAll PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) - +add_log_tag(testAll) if(WIN32) target_sources(testAll PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/win32/test.rc") target_link_libraries(testAll PUBLIC ws2_32 iphlpapi shlwapi) diff --git a/test/router/test_llarp_router_version.cpp b/test/router/test_llarp_router_version.cpp index 01adc3cdf..6230b2161 100644 --- a/test/router/test_llarp_router_version.cpp +++ b/test/router/test_llarp_router_version.cpp @@ -57,7 +57,7 @@ TEST_CASE("BEncode", "[RouterVersion]") CHECK(v1235.BEncode(&buf)); std::string s((const char*)buf.begin(), (buf.end() - buf.begin())); - LogInfo("bencoded: ", buf.begin()); + llarp::LogInfo("bencoded: ", buf.begin()); CHECK_THAT((const char*)buf.begin(), Equals("li5ei1ei2ei3ee")); } From 0546dab2e3cb2c8ade75d2eafdb29dece6853608 Mon Sep 17 00:00:00 2001 From: jeff Date: Tue, 11 May 2021 07:53:32 -0400 Subject: [PATCH 02/10] make source location happy on macos * because of course apple doesn't provide any implementation (lmao) we provide one ourself --- llarp/util/logging/source_location.hpp | 63 ++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/llarp/util/logging/source_location.hpp b/llarp/util/logging/source_location.hpp index f4699b61b..b011a7ede 100644 --- a/llarp/util/logging/source_location.hpp +++ b/llarp/util/logging/source_location.hpp @@ -4,6 +4,69 @@ #include namespace slns = std; #else +#ifdef __APPLE__ +namespace slns +{ + struct source_location + { + public: + static constexpr source_location + current( + const char* fileName = __builtin_FILE(), + const char* functionName = __builtin_FUNCTION(), + const uint_least32_t lineNumber = __builtin_LINE(), + const uint_least32_t columnOffset = __builtin_COLUMN()) noexcept + { + return source_location{fileName, functionName, lineNumber, columnOffset}; + } + + source_location(const source_location&) = default; + source_location(source_location&&) = default; + + constexpr const char* + file_name() const noexcept + { + return fileName; + } + + constexpr const char* + function_name() const noexcept + { + return functionName; + } + + constexpr uint_least32_t + line() const noexcept + { + return lineNumber; + } + + constexpr std::uint_least32_t + column() const noexcept + { + return columnOffset; + } + + private: + constexpr explicit source_location( + const char* fileName, + const char* functionName, + const uint_least32_t lineNumber, + const uint_least32_t columnOffset) noexcept + : fileName(fileName) + , functionName(functionName) + , lineNumber(lineNumber) + , columnOffset(columnOffset) + {} + + const char* fileName; + const char* functionName; + const std::uint_least32_t lineNumber; + const std::uint_least32_t columnOffset; + }; +} // namespace slns +#else #include namespace slns = std::experimental; #endif +#endif From 18c5b43e6358d151116950da6976cb88b05ff519 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Tue, 11 May 2021 08:17:13 -0400 Subject: [PATCH 03/10] use inline source_location implementation for android too --- llarp/quic/connection.cpp | 2 +- llarp/util/logging/source_location.hpp | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/llarp/quic/connection.cpp b/llarp/quic/connection.cpp index 8d7cc8728..778908385 100644 --- a/llarp/quic/connection.cpp +++ b/llarp/quic/connection.cpp @@ -337,7 +337,7 @@ namespace llarp::quic va_start(ap, fmt); if (char* msg; vasprintf(&msg, fmt, ap) >= 0) { - LogTraceExplicit("external/ngtcp2/*.c", 0, msg); + LogTrace{msg}; std::free(msg); } va_end(ap); diff --git a/llarp/util/logging/source_location.hpp b/llarp/util/logging/source_location.hpp index b011a7ede..69a03fa8d 100644 --- a/llarp/util/logging/source_location.hpp +++ b/llarp/util/logging/source_location.hpp @@ -4,7 +4,6 @@ #include namespace slns = std; #else -#ifdef __APPLE__ namespace slns { struct source_location @@ -15,7 +14,12 @@ namespace slns const char* fileName = __builtin_FILE(), const char* functionName = __builtin_FUNCTION(), const uint_least32_t lineNumber = __builtin_LINE(), - const uint_least32_t columnOffset = __builtin_COLUMN()) noexcept +#if defined(__GNUC__) and (__GNUC__ > 4 or (__GNUC__ == 4 and __GNUC_MINOR__ >= 8)) + const uint_least32_t columnOffset = 0) +#else + const uint_least32_t columnOffset = __builtin_COLUMN()) +#endif + noexcept { return source_location{fileName, functionName, lineNumber, columnOffset}; } @@ -65,8 +69,4 @@ namespace slns const std::uint_least32_t columnOffset; }; } // namespace slns -#else -#include -namespace slns = std::experimental; -#endif #endif From acdb8a19a67c1ea09aaf12cf6a1271d9f4ce771a Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Tue, 11 May 2021 08:36:01 -0400 Subject: [PATCH 04/10] for some reason [[maybe_unused]] is hated by buster 32 bit x86 so we do something else to appease that compiler. --- llarp/util/logging/logger.hpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/llarp/util/logging/logger.hpp b/llarp/util/logging/logger.hpp index 0c20ac38a..6746fd1e9 100644 --- a/llarp/util/logging/logger.hpp +++ b/llarp/util/logging/logger.hpp @@ -100,11 +100,12 @@ namespace llarp template struct LogTrace { - LogTrace( - [[maybe_unused]] T... args, - [[maybe_unused]] const slns::source_location& location = slns::source_location::current()) + LogTrace(T... args, const slns::source_location& location = slns::source_location::current()) { -#ifndef NDEBUG +#ifdef NDEBUG + const auto discard = [](auto&&...) {}; + discard(location, std::forward(args)...); +#else _log(eLogTrace, location, std::forward(args)...); #endif } @@ -116,11 +117,12 @@ namespace llarp template struct LogDebug { - LogDebug( - [[maybe_unused]] T... args, - [[maybe_unused]] const slns::source_location& location = slns::source_location::current()) + LogDebug(T... args, const slns::source_location& location = slns::source_location::current()) { -#ifndef NDEBUG +#ifdef NDEBUG + const auto discard = [](auto&&...) {}; + discard(location, std::forward(args)...); +#else _log(eLogDebug, location, std::forward(args)...); #endif } From 7d07dea23575a250a34bcab353f45f7fcae99339 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Fri, 24 Sep 2021 12:22:20 -0400 Subject: [PATCH 05/10] fix up gripes in source_location * get rid of columns we dont need those --- llarp/util/logging/source_location.hpp | 33 ++++++-------------------- 1 file changed, 7 insertions(+), 26 deletions(-) diff --git a/llarp/util/logging/source_location.hpp b/llarp/util/logging/source_location.hpp index 69a03fa8d..35f5cc7fc 100644 --- a/llarp/util/logging/source_location.hpp +++ b/llarp/util/logging/source_location.hpp @@ -13,15 +13,9 @@ namespace slns current( const char* fileName = __builtin_FILE(), const char* functionName = __builtin_FUNCTION(), - const uint_least32_t lineNumber = __builtin_LINE(), -#if defined(__GNUC__) and (__GNUC__ > 4 or (__GNUC__ == 4 and __GNUC_MINOR__ >= 8)) - const uint_least32_t columnOffset = 0) -#else - const uint_least32_t columnOffset = __builtin_COLUMN()) -#endif - noexcept + const uint_least32_t lineNumber = __builtin_LINE()) noexcept { - return source_location{fileName, functionName, lineNumber, columnOffset}; + return source_location{fileName, functionName, lineNumber}; } source_location(const source_location&) = default; @@ -45,28 +39,15 @@ namespace slns return lineNumber; } - constexpr std::uint_least32_t - column() const noexcept - { - return columnOffset; - } - private: constexpr explicit source_location( - const char* fileName, - const char* functionName, - const uint_least32_t lineNumber, - const uint_least32_t columnOffset) noexcept - : fileName(fileName) - , functionName(functionName) - , lineNumber(lineNumber) - , columnOffset(columnOffset) + const char* fileName, const char* functionName, const uint_least32_t lineNumber) noexcept + : fileName(fileName), functionName(functionName), lineNumber(lineNumber) {} - const char* fileName; - const char* functionName; - const std::uint_least32_t lineNumber; - const std::uint_least32_t columnOffset; + const char* const fileName; + const char* const functionName; + const uint_least32_t lineNumber; }; } // namespace slns #endif From 2d5faccb9ff13c70e82427216a6600b2e343e18e Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Fri, 24 Sep 2021 12:42:29 -0400 Subject: [PATCH 06/10] refactor for apple --- llarp/apple/apple_logger.cpp | 4 ++-- llarp/apple/apple_logger.hpp | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/llarp/apple/apple_logger.cpp b/llarp/apple/apple_logger.cpp index 4a41c78cf..6dca15fa3 100644 --- a/llarp/apple/apple_logger.cpp +++ b/llarp/apple/apple_logger.cpp @@ -6,7 +6,7 @@ namespace llarp::apple NSLogStream::PreLog( std::stringstream& ss, LogLevel lvl, - const char* fname, + std::string_view fname, int lineno, const std::string& nodename) const { @@ -17,7 +17,7 @@ namespace llarp::apple } void - NSLogStream::Print(LogLevel, const char*, const std::string& msg) + NSLogStream::Print(LogLevel, std::string_view, const std::string& msg) { ns_logger(msg.c_str()); } diff --git a/llarp/apple/apple_logger.hpp b/llarp/apple/apple_logger.hpp index b97810c76..954d5b402 100644 --- a/llarp/apple/apple_logger.hpp +++ b/llarp/apple/apple_logger.hpp @@ -16,15 +16,15 @@ namespace llarp::apple PreLog( std::stringstream& s, LogLevel lvl, - const char* fname, + std::string_view fname, int lineno, const std::string& nodename) const override; void - Print(LogLevel lvl, const char* tag, const std::string& msg) override; + Print(LogLevel lvl, std::string_view tag, const std::string& msg) override; void - PostLog(std::stringstream& ss) const override + PostLog(std::stringstream&) const override {} void From 1b06e263fb9d3d9bdb99f5cb7916a46a4bcbabd4 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Fri, 24 Sep 2021 12:51:31 -0400 Subject: [PATCH 07/10] update log statement for win32 --- llarp/vpn/win32.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llarp/vpn/win32.hpp b/llarp/vpn/win32.hpp index e52c73e53..9ecd15e79 100644 --- a/llarp/vpn/win32.hpp +++ b/llarp/vpn/win32.hpp @@ -500,7 +500,7 @@ namespace llarp::vpn void Execute(std::string cmd) const { - LogInfo(cmd); + llarp::LogInfo("exec: ", cmd); ::system(cmd.c_str()); } From 20814a4adce79f4ecb48a0ff21030b1eec35b7cb Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Fri, 24 Sep 2021 13:03:09 -0400 Subject: [PATCH 08/10] use llarp:: prefix for LogError --- llarp/apple/context_wrapper.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/llarp/apple/context_wrapper.cpp b/llarp/apple/context_wrapper.cpp index 09b45ba9a..fd662967c 100644 --- a/llarp/apple/context_wrapper.cpp +++ b/llarp/apple/context_wrapper.cpp @@ -105,7 +105,7 @@ llarp_apple_init(llarp_apple_config* appleconf) } catch (const std::exception& e) { - LogError("Failed to initialize lokinet from config: ", e.what()); + llarp::LogError("Failed to initialize lokinet from config: ", e.what()); } return nullptr; } @@ -149,7 +149,7 @@ llarp_apple_start(void* lokinet, void* callback_context) } catch (const std::exception& e) { - LogError("Failed to initialize lokinet: ", e.what()); + llarp::LogError("Failed to initialize lokinet: ", e.what()); return -1; } @@ -178,7 +178,7 @@ llarp_apple_incoming(void* lokinet, const void* bytes, size_t size) if (iface->OfferReadPacket(buf)) return 0; - LogError("invalid IP packet: ", llarp::buffer_printer(buf)); + llarp::LogError("invalid IP packet: ", llarp::buffer_printer(buf)); return -1; } From cf187ddffc94fccec462e3bead5cde4447339602 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Fri, 24 Sep 2021 13:44:14 -0400 Subject: [PATCH 09/10] fix up logging, put _log into anonynous namespace, make only log trace nop in release builds --- llarp/util/logging/logger.hpp | 51 +++++++++++++++++------------------ 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/llarp/util/logging/logger.hpp b/llarp/util/logging/logger.hpp index 6746fd1e9..f88d63a9f 100644 --- a/llarp/util/logging/logger.hpp +++ b/llarp/util/logging/logger.hpp @@ -78,34 +78,36 @@ namespace llarp LogLevel GetLogLevel(); - /** internal */ - template - inline static void - _log(LogLevel lvl, const slns::source_location& location, TArgs&&... args) noexcept + namespace { - auto& log = LogContext::Instance(); - if (log.curLevel > lvl || log.logStream == nullptr) - return; - std::ostringstream ss; - if constexpr (sizeof...(args) > 0) - LogAppend(ss, std::forward(args)...); - log.logStream->AppendLog( - lvl, - strip_prefix(location.file_name(), SOURCE_ROOT), - location.line(), - log.nodeName, - ss.str()); - } + /** internal */ + template + inline static void + _log(LogLevel lvl, const slns::source_location& location, TArgs&&... args) noexcept + { + auto& log = LogContext::Instance(); + if (log.curLevel > lvl || log.logStream == nullptr) + return; + std::ostringstream ss; + if constexpr (sizeof...(args) > 0) + LogAppend(ss, std::forward(args)...); + log.logStream->AppendLog( + lvl, + strip_prefix(location.file_name(), SOURCE_ROOT), + location.line(), + log.nodeName, + ss.str()); + } + } // namespace template struct LogTrace { - LogTrace(T... args, const slns::source_location& location = slns::source_location::current()) + LogTrace( + [[maybe_unused]] T... args, + [[maybe_unused]] const slns::source_location& location = slns::source_location::current()) { -#ifdef NDEBUG - const auto discard = [](auto&&...) {}; - discard(location, std::forward(args)...); -#else +#ifndef NDEBUG _log(eLogTrace, location, std::forward(args)...); #endif } @@ -119,12 +121,7 @@ namespace llarp { LogDebug(T... args, const slns::source_location& location = slns::source_location::current()) { -#ifdef NDEBUG - const auto discard = [](auto&&...) {}; - discard(location, std::forward(args)...); -#else _log(eLogDebug, location, std::forward(args)...); -#endif } }; From d882f1a3022d88cdff149f791a7b0bba47ff64a7 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Wed, 6 Oct 2021 09:04:28 -0400 Subject: [PATCH 10/10] some compilers hated [[maybe_unused]] so use (void) when they are unused --- llarp/util/logging/logger.hpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/llarp/util/logging/logger.hpp b/llarp/util/logging/logger.hpp index f88d63a9f..fd5b6ef60 100644 --- a/llarp/util/logging/logger.hpp +++ b/llarp/util/logging/logger.hpp @@ -103,11 +103,12 @@ namespace llarp template struct LogTrace { - LogTrace( - [[maybe_unused]] T... args, - [[maybe_unused]] const slns::source_location& location = slns::source_location::current()) + LogTrace(T... args, const slns::source_location& location = slns::source_location::current()) { -#ifndef NDEBUG +#ifdef NDEBUG + ((void)args, ...); + (void)location; +#else _log(eLogTrace, location, std::forward(args)...); #endif }