From 95119a0fe1cd4af07d70f109802c03dd02927b99 Mon Sep 17 00:00:00 2001 From: Jeff Date: Tue, 9 Apr 2019 15:20:02 -0400 Subject: [PATCH 01/11] always use random paths for lookups --- llarp/service/endpoint.cpp | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index a8fca07c9..cc704062c 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -1206,25 +1206,6 @@ namespace llarp return false; } - { - RouterID endpoint = path->Endpoint(); - auto itr = m_ServiceLookupFails.find(endpoint); - if(itr != m_ServiceLookupFails.end()) - { - path = PickRandomEstablishedPath(); - } - } - if(!path) - { - path = PickRandomEstablishedPath(); - if(!path) - { - llarp::LogError(Name(), "no working paths for lookup"); - hook(remote, nullptr); - return false; - } - } - HiddenServiceAddressLookup* job = new HiddenServiceAddressLookup( this, std::bind(&Endpoint::OnLookup, this, std::placeholders::_1, @@ -1551,7 +1532,7 @@ namespace llarp else { llarp::LogInfo(Name(), " updating introset"); - UpdateIntroSet(false); + UpdateIntroSet(true); } return shiftedIntro; } From c45a3201c50809e80d6535768f14995b7db19c91 Mon Sep 17 00:00:00 2001 From: Jeff Date: Wed, 10 Apr 2019 11:26:50 -0400 Subject: [PATCH 02/11] disable llarp_ev_pkt_pipe on win32 --- llarp/ev/pipe.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/llarp/ev/pipe.cpp b/llarp/ev/pipe.cpp index 549f5fefa..7c5ce4493 100644 --- a/llarp/ev/pipe.cpp +++ b/llarp/ev/pipe.cpp @@ -11,6 +11,10 @@ llarp_ev_pkt_pipe::llarp_ev_pkt_pipe(llarp_ev_loop_ptr loop) bool llarp_ev_pkt_pipe::Start() { +#if defined(_WIN32) + llarp::LogError("llarp_ev_pkt_pipe not supported on win32"); + return false; +#else int _fds[2]; #if defined(__APPLE__) if(pipe(_fds) == -1 @@ -27,6 +31,7 @@ llarp_ev_pkt_pipe::Start() fd = _fds[0]; writefd = _fds[1]; return true; +#endif } int From 784a25634bea31e3a5c9dc9419dcb2e1033e94b9 Mon Sep 17 00:00:00 2001 From: Jeff Date: Wed, 10 Apr 2019 11:32:29 -0400 Subject: [PATCH 03/11] exit tun loop in destructor --- llarp/ev/ev_win32.cpp | 7 ++++++- llarp/ev/ev_win32.hpp | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/llarp/ev/ev_win32.cpp b/llarp/ev/ev_win32.cpp index 06531442d..f0d60a402 100644 --- a/llarp/ev/ev_win32.cpp +++ b/llarp/ev/ev_win32.cpp @@ -230,6 +230,11 @@ exit_tun_loop() } } +llarp_win32_loop::~llarp_win32_loop() +{ + exit_tun_loop(); +} + namespace llarp { int @@ -677,4 +682,4 @@ llarp_win32_loop::stop() upoll_destroy(upollfd); upollfd = nullptr; llarp::LogDebug("destroy upoll"); -} \ No newline at end of file +} diff --git a/llarp/ev/ev_win32.hpp b/llarp/ev/ev_win32.hpp index 0d9c31fd8..f9f8b65eb 100644 --- a/llarp/ev/ev_win32.hpp +++ b/llarp/ev/ev_win32.hpp @@ -104,6 +104,8 @@ struct llarp_win32_loop : public llarp_ev_loop { } + ~llarp_win32_loop(); + bool tcp_connect(struct llarp_tcp_connecter* tcp, const sockaddr* remoteaddr); @@ -152,4 +154,4 @@ struct llarp_win32_loop : public llarp_ev_loop stop(); }; -#endif \ No newline at end of file +#endif From bcb5933fc9e820f526d738a3749ec5eb5d85fb78 Mon Sep 17 00:00:00 2001 From: Jeff Date: Wed, 10 Apr 2019 11:42:36 -0400 Subject: [PATCH 04/11] ooofff --- llarp/ev/ev_win32.hpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/llarp/ev/ev_win32.hpp b/llarp/ev/ev_win32.hpp index f9f8b65eb..df4dcc915 100644 --- a/llarp/ev/ev_win32.hpp +++ b/llarp/ev/ev_win32.hpp @@ -104,8 +104,6 @@ struct llarp_win32_loop : public llarp_ev_loop { } - ~llarp_win32_loop(); - bool tcp_connect(struct llarp_tcp_connecter* tcp, const sockaddr* remoteaddr); @@ -115,9 +113,7 @@ struct llarp_win32_loop : public llarp_ev_loop virtual bool udp_listen(llarp_udp_io* l, const sockaddr* src); - ~llarp_win32_loop() - { - } + ~llarp_win32_loop(); bool running() const; From a844c610493aa92b4a34b9101a85791e2f03c622 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Wed, 10 Apr 2019 11:53:10 -0400 Subject: [PATCH 05/11] unbreak windows target --- CMakeLists.txt | 2 +- Makefile | 2 +- contrib/cross/mingw32.cmake | 4 ++-- llarp/CMakeLists.txt | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index be03d9830..f98fdce68 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,7 +53,7 @@ if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS") endif() if(WIN32) - set(CMAKE_CXX_STANDARD 17) + set(CMAKE_CXX_STANDARD 14) ENABLE_LANGUAGE(RC) set(CMAKE_CXX_STANDARD_LIBRARIES "${CMAKE_CXX_STANDARD_LIBRARIES} -lshlwapi") else() diff --git a/Makefile b/Makefile index 5b78594ec..ab0a15ca1 100644 --- a/Makefile +++ b/Makefile @@ -198,7 +198,7 @@ android: android-gradle windows-release-configure: clean mkdir -p '$(BUILD_ROOT)' - $(CONFIG_CMD) -DCMAKE_TOOLCHAIN_FILE='$(REPO)/contrib/cross/mingw.cmake' -DCMAKE_BUILD_TYPE=Release -DCMAKE_ASM_FLAGS='$(ASFLAGS)' -DCMAKE_C_FLAGS='$(CFLAGS)' -DCMAKE_CXX_FLAGS='$(CXXFLAGS)' + $(CONFIG_CMD) -DCMAKE_TOOLCHAIN_FILE='$(REPO)/contrib/cross/mingw32.cmake' -DCMAKE_BUILD_TYPE=Release -DCMAKE_ASM_FLAGS='$(ASFLAGS)' -DCMAKE_C_FLAGS='$(CFLAGS)' -DCMAKE_CXX_FLAGS='$(CXXFLAGS)' windows-release: windows-release-configure $(MAKE) -C '$(BUILD_ROOT)' diff --git a/contrib/cross/mingw32.cmake b/contrib/cross/mingw32.cmake index 6a853511f..e54713827 100644 --- a/contrib/cross/mingw32.cmake +++ b/contrib/cross/mingw32.cmake @@ -18,8 +18,8 @@ set(USING_CLANG ON) set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-clang) set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-clang++) else() -set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc) -set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++) +set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc-posix) +set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++-posix) endif() set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres) diff --git a/llarp/CMakeLists.txt b/llarp/CMakeLists.txt index 77592a4df..2cd697dd6 100644 --- a/llarp/CMakeLists.txt +++ b/llarp/CMakeLists.txt @@ -44,10 +44,10 @@ add_library(${UTIL_LIB} STATIC ${LIB_UTIL_SRC}) target_include_directories(${UTIL_LIB} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/include) target_link_libraries(${UTIL_LIB} PUBLIC absl::synchronization) target_link_libraries(${UTIL_LIB} PUBLIC nlohmann_json::nlohmann_json) - +target_link_libraries(${UTIL_LIB} PUBLIC absl::optional absl::variant absl::strings absl::hash) # cut back on fluff if (NOT WIN32) - target_link_libraries(${UTIL_LIB} PUBLIC absl::optional absl::variant absl::strings absl::hash cppbackport) +target_link_libraries(${UTIL_LIB} PUBLIC cppbackport) endif(NOT WIN32) if(${CMAKE_SYSTEM_NAME} MATCHES "Linux") From 46a7d02c09aea32fe1a9985ef45c9d63a6810f22 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Wed, 10 Apr 2019 12:04:45 -0400 Subject: [PATCH 06/11] add debug windows target and default to it --- Makefile | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index ab0a15ca1..ed64326b5 100644 --- a/Makefile +++ b/Makefile @@ -196,6 +196,14 @@ android-gradle: android-gradle-prepare android: android-gradle cp -f $(ANDROID_DIR)/build/outputs/apk/*.apk $(REPO) +windows-debug-configure: clean + mkdir -p '$(BUILD_ROOT)' + $(CONFIG_CMD) -DCMAKE_TOOLCHAIN_FILE='$(REPO)/contrib/cross/mingw32.cmake' -DCMAKE_BUILD_TYPE=Debug -DCMAKE_ASM_FLAGS='$(ASFLAGS)' -DCMAKE_C_FLAGS='$(CFLAGS)' -DCMAKE_CXX_FLAGS='$(CXXFLAGS)' + +windows-debug: windows-debug-configure + $(MAKE) -C '$(BUILD_ROOT)' + cp '$(BUILD_ROOT)/lokinet.exe' '$(REPO)/lokinet.exe' + windows-release-configure: clean mkdir -p '$(BUILD_ROOT)' $(CONFIG_CMD) -DCMAKE_TOOLCHAIN_FILE='$(REPO)/contrib/cross/mingw32.cmake' -DCMAKE_BUILD_TYPE=Release -DCMAKE_ASM_FLAGS='$(ASFLAGS)' -DCMAKE_C_FLAGS='$(CFLAGS)' -DCMAKE_CXX_FLAGS='$(CXXFLAGS)' @@ -204,7 +212,7 @@ windows-release: windows-release-configure $(MAKE) -C '$(BUILD_ROOT)' cp '$(BUILD_ROOT)/lokinet.exe' '$(REPO)/lokinet.exe' -windows: windows-release +windows: windows-debug abyss: debug $(ABYSS_EXE) From 9e24557429ace3e041992f6dcddf225ef62a0ed7 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Thu, 11 Apr 2019 08:58:23 -0400 Subject: [PATCH 07/11] refactor logger --- llarp/CMakeLists.txt | 3 + llarp/config.cpp | 15 ++++- llarp/config.hpp | 1 + llarp/router/router.cpp | 2 +- llarp/util/android_logger.cpp | 62 +++++++++++++++++++ llarp/util/android_logger.hpp | 20 ++++++ llarp/util/logger.cpp | 36 ++++++++++- llarp/util/logger.hpp | 110 +++++++++++++-------------------- llarp/util/logger_internal.hpp | 66 ++++++++++++++++++++ llarp/util/loglevel.hpp | 18 ++++++ llarp/util/logstream.hpp | 27 ++++++++ llarp/util/ostream_logger.cpp | 52 ++++++++++++++++ llarp/util/ostream_logger.hpp | 32 ++++++++++ llarp/util/win32_logger.cpp | 62 +++++++++++++++++++ llarp/util/win32_logger.hpp | 29 +++++++++ 15 files changed, 461 insertions(+), 74 deletions(-) create mode 100644 llarp/util/android_logger.cpp create mode 100644 llarp/util/android_logger.hpp create mode 100644 llarp/util/logger_internal.hpp create mode 100644 llarp/util/loglevel.hpp create mode 100644 llarp/util/logstream.hpp create mode 100644 llarp/util/ostream_logger.cpp create mode 100644 llarp/util/ostream_logger.hpp create mode 100644 llarp/util/win32_logger.cpp create mode 100644 llarp/util/win32_logger.hpp diff --git a/llarp/CMakeLists.txt b/llarp/CMakeLists.txt index 2cd697dd6..04af8b91d 100644 --- a/llarp/CMakeLists.txt +++ b/llarp/CMakeLists.txt @@ -15,6 +15,9 @@ set(LIB_UTIL_SRC util/ini.cpp util/json.cpp util/logger.cpp + util/android_logger.cpp + util/ostream_logger.cpp + util/win32_logger.cpp util/logic.cpp util/mem.cpp util/metrics_core.cpp diff --git a/llarp/config.cpp b/llarp/config.cpp index c086084fd..819b51db1 100644 --- a/llarp/config.cpp +++ b/llarp/config.cpp @@ -52,6 +52,7 @@ namespace llarp api = find_section(parser, "api", section_t{}); lokid = find_section(parser, "lokid", section_t{}); bootstrap = find_section(parser, "bootstrap", section_t{}); + logging = find_section(parser, "logging", section_t{}); return true; }; @@ -66,6 +67,7 @@ namespace llarp {"metrics", metrics}, {"netdb", netdb}, {"api", api}, + {"logging", logging}, {"services", services}}; auto visitor = [&](const char *name, const auto &item) { @@ -178,9 +180,18 @@ llarp_generic_ensure_config(std::ofstream &f, std::string basepath) f << "# nickname=lokinet" << std::endl; f << std::endl << std::endl; + // logging + f << "[logging]" << std::endl; + f << "level=info" << std::endl; + f << "# uncomment for logging to file" << std::endl; + f << "#type=file" << std::endl; + f << "#file=/path/to/logfile" << std::endl; + f << "# uncomment for syslog logging" << std::endl; + f << "#type=syslog" << std::endl; + // metrics - f << "[metrics]\n"; - f << "json-metrics-path=" << basepath << "metrics.json\n"; + f << "[metrics]" << std::endl; + f << "json-metrics-path=" << basepath << "metrics.json" << std::endl; f << std::endl << std::endl; diff --git a/llarp/config.hpp b/llarp/config.hpp index a4ea61eb2..520733bd9 100644 --- a/llarp/config.hpp +++ b/llarp/config.hpp @@ -24,6 +24,7 @@ namespace llarp section_t api; section_t lokid; section_t bootstrap; + section_t logging; bool Load(const char *fname); diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index 4e003e745..2c467d725 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -944,7 +944,7 @@ namespace llarp { _rc.SetNick(val); // set logger name here - _glog.nodeName = rc().Nick(); + LogContext::Instance().nodeName = rc().Nick(); } if(StrEq(key, "encryption-privkey")) { diff --git a/llarp/util/android_logger.cpp b/llarp/util/android_logger.cpp new file mode 100644 index 000000000..5302ecc98 --- /dev/null +++ b/llarp/util/android_logger.cpp @@ -0,0 +1,62 @@ +#if defined(ANDROID) +#include +#include +#include +namespace llarp +{ + void + AndroidLogStream::PreLog(std::stringstream& ss, LogLevel lvl, + const char* fname, int lineno) const + { + switch(lvl) + { + case eLogNone: + break; + case eLogDebug: + ss << "[DBG] "; + break; + case eLogInfo: + ss << "[NFO] "; + break; + case eLogWarn: + ss << "[WRN] "; + break; + case eLogError: + ss << "[ERR] "; + break; + } + + ss << "(" << thread_id_string() << ") " << log_timestamp() << " " << fname + << ":" << lineno << "\t"; + } + + void + AndroidLogStream::PostLog(std::stingstream&) const + { + } + + void + AndroidLogStream::Print(LogLevel lvl, const char* tag, + const std::string& msg) const + { + std::string str("lokinet|"); + str += tag; + switch(lvl) + { + case eLogDebug: + __android_log_write(ANDROID_LOG_DEBUG, str.c_str(), msg.c_str()); + return; + case eLogInfo: + __android_log_write(ANDROID_LOG_INFO, str.c_str(), msg.c_str()); + return; + case eLogWarn: + __android_log_write(ANDROID_LOG_WARN, str.c_str(), msg.c_str()); + return; + case eLogError: + __android_log_write(ANDROID_LOG_ERROR, str.c_str(), msg.c_str()); + return; + } + + } // namespace llarp +} // namespace llarp +#endif diff --git a/llarp/util/android_logger.hpp b/llarp/util/android_logger.hpp new file mode 100644 index 000000000..838cb5bb8 --- /dev/null +++ b/llarp/util/android_logger.hpp @@ -0,0 +1,20 @@ +#ifndef LLARP_UTIL_OSTREAM_LOGGER_HPP +#define LLARP_UTIL_OSTREAM_LOGGER_HPP + +#include +#include + +namespace llarp +{ + struct AndroidLogStream : public ILogStream + { + void + PreLog(std::stringstream& s, LogLevel lvl, const char* fname, + int lineno) const override; + + void + Log(LogLevel lvl, const std::string& msg) const override; + }; +} // namespace llarp + +#endif diff --git a/llarp/util/logger.cpp b/llarp/util/logger.cpp index 35059b4d0..e028adc60 100644 --- a/llarp/util/logger.cpp +++ b/llarp/util/logger.cpp @@ -1,14 +1,44 @@ #include #include +#include +#if defined(_WIN32) +#include +#endif +#if defined(ANDROID) +#include +#endif namespace llarp { - Logger _glog; +#if defined(_WIN32) + using Stream_t = Win32LogStream; +#define _LOGSTREAM_INIT std::cout +#else +#if defined(ANDROID) + using Stream_t = AndroidLogStream; +#define _LOGSTREAM_INIT +#else + using Stream_t = OStreamLogStream; +#define _LOGSTREAM_INIT std::cout +#endif +#endif + + LogContext::LogContext() + : logStream(std::make_unique< Stream_t >(_LOGSTREAM_INIT)) + { + } + + LogContext& + LogContext::Instance() + { + static LogContext ctx; + return ctx; + } void SetLogLevel(LogLevel lvl) { - _glog.minlevel = lvl; + LogContext::Instance().minLevel = lvl; } } // namespace llarp @@ -23,6 +53,6 @@ extern "C" void cSetLogNodeName(const char* name) { - llarp::_glog.nodeName = name; + llarp::LogContext::Instance().nodeName = name; } } diff --git a/llarp/util/logger.hpp b/llarp/util/logger.hpp index 90f8cb3bc..0cf91d800 100644 --- a/llarp/util/logger.hpp +++ b/llarp/util/logger.hpp @@ -1,8 +1,10 @@ -#ifndef LLARP_LOGGER_HPP -#define LLARP_LOGGER_HPP +#ifndef LLARP_UTIL_LOGGER_HPP +#define LLARP_UTIL_LOGGER_HPP #include - +#include +#include +/* #ifdef _WIN32 #define VC_EXTRALEAN #include @@ -21,26 +23,19 @@ #include #include #include +*/ namespace llarp { - // probably will need to move out of llarp namespace for c api - enum LogLevel - { - eLogDebug, - eLogInfo, - eLogWarn, - eLogError, - eLogNone - }; - + /* struct Logger { std::string nodeName; LogLevel minlevel = eLogInfo; - std::ostream& out; + ILogStream_ptr m_stream; + + std::unique_ptr< std::ostream > _altOut; - std::function< void(const std::string&) > customLog; #ifdef _WIN32 bool isConsoleModern = true; // qol fix so oldfag clients don't see ugly escapes @@ -69,74 +64,52 @@ namespace llarp Logger(std::ostream& o) : out(o) { } - }; - extern Logger _glog; + /// open logger to file + /// return false on failure + /// return true on successful open + bool + OpenLogFile(const std::string& file); - void - SetLogLevel(LogLevel lvl); + /// call once to use syslog based logging + void + UseSyslog(); - /** 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)...); - } - - static 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 - } + virtual void + Print(LogLevel lvl, const std::string& msg); + }; + */ - struct log_timestamp + struct LogContext { - const char* format; - - log_timestamp(const char* fmt = "%c %Z") : format(fmt) - { - } - - friend std::ostream& - operator<<(std::ostream& out, const log_timestamp& ts) - { -#if defined(ANDROID) || defined(RPI) - (void)ts; - return out << time_now_ms(); -#else - auto now = llarp::Clock_t::to_time_t(llarp::Clock_t::now()); - return out << std::put_time(std::localtime(&now), ts.format); -#endif - } + LogContext(); + LogLevel minLevel = eLogInfo; + ILogStream_ptr logStream; + std::string nodeName; + static LogContext& + Instance(); }; + void + SetLogLevel(LogLevel lvl); + /** internal */ template < typename... TArgs > void _Log(LogLevel lvl, const char* fname, int lineno, TArgs&&... args) noexcept { - if(_glog.minlevel > lvl) + auto& log = LogContext::Instance(); + if(log.minLevel > lvl) return; std::stringstream ss; + log.logStream->PreLog(ss, lvl, fname, lineno); + LogAppend(ss, std::forward< TArgs >(args)...); + log.logStream->PostLog(ss); + log.logStream->Print(lvl, fname, ss.str()); + } + /* + std::stringstream ss; #ifdef ANDROID int loglev = -1; switch(lvl) @@ -259,6 +232,7 @@ namespace llarp } #endif } + */ } // namespace llarp #define LogDebug(...) _Log(llarp::eLogDebug, LOG_TAG, __LINE__, __VA_ARGS__) diff --git a/llarp/util/logger_internal.hpp b/llarp/util/logger_internal.hpp new file mode 100644 index 000000000..959db7623 --- /dev/null +++ b/llarp/util/logger_internal.hpp @@ -0,0 +1,66 @@ +#ifndef LLARP_UTIL_LOGGER_INTERNAL_HPP +#define LLARP_UTIL_LOGGER_INTERNAL_HPP + +#include +#include +#include +#include +#include + +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)...); + } + + static 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; + + log_timestamp(const char* fmt = "%c %Z") : format(fmt) + { + } + + friend std::ostream& + operator<<(std::ostream& out, const log_timestamp& ts) + { +#if defined(ANDROID) || defined(RPI) + (void)ts; + return out << time_now_ms(); +#else + auto now = llarp::Clock_t::to_time_t(llarp::Clock_t::now()); + return out << std::put_time(std::localtime(&now), ts.format); +#endif + } + }; + +} // namespace llarp + +#endif diff --git a/llarp/util/loglevel.hpp b/llarp/util/loglevel.hpp new file mode 100644 index 000000000..96c5ffe71 --- /dev/null +++ b/llarp/util/loglevel.hpp @@ -0,0 +1,18 @@ +#ifndef LLARP_UTIL_LOG_LEVEL_HPP +#define LLARP_UTIL_LOG_LEVEL_HPP + +namespace llarp +{ + // probably will need to move out of llarp namespace for c api + enum LogLevel + { + eLogDebug, + eLogInfo, + eLogWarn, + eLogError, + eLogNone + }; + +} // namespace llarp + +#endif diff --git a/llarp/util/logstream.hpp b/llarp/util/logstream.hpp new file mode 100644 index 000000000..898ae487d --- /dev/null +++ b/llarp/util/logstream.hpp @@ -0,0 +1,27 @@ +#ifndef LLARP_UTIL_LOG_STREAM_HPP +#define LLARP_UTIL_LOG_STREAM_HPP +#include +#include +#include +#include +namespace llarp +{ + /// logger stream interface + struct ILogStream + { + virtual ~ILogStream(){}; + + virtual void + PreLog(std::stringstream& out, LogLevel lvl, const char* fname, + int lineno) const = 0; + virtual void + Print(LogLevel lvl, const char* filename, const std::string& msg) const = 0; + + virtual void + PostLog(std::stringstream& out) const = 0; + }; + + using ILogStream_ptr = std::unique_ptr< ILogStream >; + +} // namespace llarp +#endif diff --git a/llarp/util/ostream_logger.cpp b/llarp/util/ostream_logger.cpp new file mode 100644 index 000000000..c98eb07ac --- /dev/null +++ b/llarp/util/ostream_logger.cpp @@ -0,0 +1,52 @@ +#include +#include + +namespace llarp +{ + OStreamLogStream::OStreamLogStream(std::ostream& out) : m_Out(out) + { + } + + void + OStreamLogStream::PreLog(std::stringstream& ss, LogLevel lvl, + const char* fname, int lineno) const + { + switch(lvl) + { + case eLogNone: + break; + case eLogDebug: + ss << (char)27 << "[0m"; + ss << "[DBG] "; + break; + case eLogInfo: + ss << (char)27 << "[1m"; + ss << "[NFO] "; + break; + case eLogWarn: + ss << (char)27 << "[1;33m"; + ss << "[WRN] "; + break; + case eLogError: + ss << (char)27 << "[1;31m"; + ss << "[ERR] "; + break; + } + + ss << "(" << thread_id_string() << ") " << log_timestamp() << " " << fname + << ":" << lineno << "\t"; + } + + void + OStreamLogStream::PostLog(std::stringstream& ss) const + { + ss << (char)27 << "[0;0m" << std::endl; + } + + void + OStreamLogStream::Print(LogLevel, const char*, const std::string& msg) const + { + m_Out << msg; + } + +} // namespace llarp diff --git a/llarp/util/ostream_logger.hpp b/llarp/util/ostream_logger.hpp new file mode 100644 index 000000000..0ba91d172 --- /dev/null +++ b/llarp/util/ostream_logger.hpp @@ -0,0 +1,32 @@ +#ifndef LLARP_UTIL_OSTREAM_LOGGER_HPP +#define LLARP_UTIL_OSTREAM_LOGGER_HPP + +#include +#include + +namespace llarp +{ + struct OStreamLogStream : public ILogStream + { + OStreamLogStream(std::ostream& out); + + ~OStreamLogStream() + { + } + + virtual void + PreLog(std::stringstream& s, LogLevel lvl, const char* fname, + int lineno) const override; + + void + Print(LogLevel lvl, const char* tag, const std::string& msg) const override; + + virtual void + PostLog(std::stringstream& ss) const override; + + private: + std::ostream& m_Out; + }; +} // namespace llarp + +#endif diff --git a/llarp/util/win32_logger.cpp b/llarp/util/win32_logger.cpp new file mode 100644 index 000000000..a2603a924 --- /dev/null +++ b/llarp/util/win32_logger.cpp @@ -0,0 +1,62 @@ +#if defined(_WIN32) +#include +#include + +namespace llarp +{ + Win32LogStream::Win32LogStream(std::ostream& out) : OStreamLogStream(out) + { + // Attempt to use ANSI escapes directly + // if the modern console is active. + DWORD mode_flags; + + GetConsoleMode(fd1, &mode_flags); + // since release SDKs don't have ANSI escape support yet + // we get all or nothing: if we can't get it, then we wouldn't + // be able to get any of them individually + mode_flags |= 0x0004 | 0x0008; + BOOL t = SetConsoleMode(fd1, mode_flags); + if(!t) + this->isConsoleModern = false; // fall back to setting colours manually + } + + void + Win32LogStream::PreLog(std::stringstream& ss, LogLevel lvl, const char* fname, + int lineno) const + { + if(!isConsoleModern) + { + switch(lvl) + { + case eLogNone: + break; + case eLogDebug: + ss << "[DBG] "; + break; + case eLogInfo: + ss << "[NFO] "; + break; + case eLogWarn: + ss << "[WRN] "; + break; + case eLogError: + ss << "[ERR] "; + break; + } + ss << "(" << thread_id_string() << ") " << log_timestamp() << " " << fname + << ":" << lineno << "\t"; + } + else + OStreamLogStream::PreLog(ss, lvl, fname, lineno); + } + + void + Win32LogStream::PostLog(std::stringstream& ss) const + { + if(isConsoleModern) + OStreamLogStream::PostLog(ss); + else + ss << std::endl; + } +} // namespace llarp +#endif diff --git a/llarp/util/win32_logger.hpp b/llarp/util/win32_logger.hpp new file mode 100644 index 000000000..32305ff45 --- /dev/null +++ b/llarp/util/win32_logger.hpp @@ -0,0 +1,29 @@ +#ifndef LLARP_UTIL_WIN32_LOGGER_HPP +#define LLARP_UTIL_WIN32_LOGGER_HPP +#if defined(_WIN32) +#include +#define VC_EXTRALEAN +#include + +namespace llarp +{ + struct Win32LogStream : public OStreamLogStream + { + Win32LogStream(std::ostream& out); + + void + PreLog(std::stringstream& s, LogLevel lvl, const char* fname, + int lineno) const override; + + void + PostLog(std::stringstream& s) const override; + + bool isConsoleModern = + true; // qol fix so oldfag clients don't see ugly escapes + HANDLE fd1 = GetStdHandle(STD_OUTPUT_HANDLE); + CONSOLE_SCREEN_BUFFER_INFO consoleInfo; + short old_attrs; + }; +} // namespace llarp +#endif +#endif From 40f8434851e8c40399f0485b36fe60642dfb7909 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Thu, 11 Apr 2019 09:13:29 -0400 Subject: [PATCH 08/11] syslog --- llarp/CMakeLists.txt | 1 + llarp/config.cpp | 4 ++- llarp/router/router.cpp | 9 ++++++ llarp/util/logger_syslog.cpp | 1 + llarp/util/logger_syslog.hpp | 21 +++++++++++++ llarp/util/syslog_logger.cpp | 60 ++++++++++++++++++++++++++++++++++++ 6 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 llarp/util/logger_syslog.cpp create mode 100644 llarp/util/logger_syslog.hpp create mode 100644 llarp/util/syslog_logger.cpp diff --git a/llarp/CMakeLists.txt b/llarp/CMakeLists.txt index 04af8b91d..c1482e379 100644 --- a/llarp/CMakeLists.txt +++ b/llarp/CMakeLists.txt @@ -17,6 +17,7 @@ set(LIB_UTIL_SRC util/logger.cpp util/android_logger.cpp util/ostream_logger.cpp + util/syslog_logger.cpp util/win32_logger.cpp util/logic.cpp util/mem.cpp diff --git a/llarp/config.cpp b/llarp/config.cpp index 819b51db1..f46a6d497 100644 --- a/llarp/config.cpp +++ b/llarp/config.cpp @@ -67,7 +67,6 @@ namespace llarp {"metrics", metrics}, {"netdb", netdb}, {"api", api}, - {"logging", logging}, {"services", services}}; auto visitor = [&](const char *name, const auto &item) { @@ -76,6 +75,9 @@ namespace llarp using namespace std::placeholders; + std::for_each(logging.begin(), logging.end(), + std::bind(visitor, "logging", _1)); + std::for_each(lokid.begin(), lokid.end(), std::bind(visitor, "lokid", _1)); std::for_each(router.begin(), router.end(), std::bind(visitor, "router", _1)); diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index 2c467d725..e55788527 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -833,6 +834,14 @@ namespace llarp llarp::LogWarn("failed to load hidden service config for ", key); } } + else if(StrEq(section, "logger")) + { + if(StrEq(key, "type") && StrEq(val, "syslog")) + { + LogInfo("Switching to syslog"); + LogContext::Instance().logStream = std::make_unique< SysLogStream >(); + } + } else if(StrEq(section, "lokid")) { if(StrEq(key, "service-node-seed")) diff --git a/llarp/util/logger_syslog.cpp b/llarp/util/logger_syslog.cpp new file mode 100644 index 000000000..ef5206072 --- /dev/null +++ b/llarp/util/logger_syslog.cpp @@ -0,0 +1 @@ +#include diff --git a/llarp/util/logger_syslog.hpp b/llarp/util/logger_syslog.hpp new file mode 100644 index 000000000..7f52cfac4 --- /dev/null +++ b/llarp/util/logger_syslog.hpp @@ -0,0 +1,21 @@ +#ifndef LLARP_UTIL_LOGGER_SYSLOG_HPP +#define LLARP_UTIL_LOGGER_SYSLOG_HPP +#include +#include + +namespace llarp +{ + struct SysLogStream : public ILogStream + { + void + PreLog(std::stringstream& s, LogLevel lvl, const char* fname, + int lineno) const override; + + void + Print(LogLevel lvl, const char* tag, const std::string& msg) const override; + + void + PostLog(std::stringstream& ss) const override; + }; +} // namespace llarp +#endif diff --git a/llarp/util/syslog_logger.cpp b/llarp/util/syslog_logger.cpp new file mode 100644 index 000000000..1db55b6ef --- /dev/null +++ b/llarp/util/syslog_logger.cpp @@ -0,0 +1,60 @@ +#ifndef _WIN32 +#include +#include +#include +namespace llarp +{ + void + SysLogStream::PreLog(std::stringstream& ss, LogLevel lvl, const char* fname, + int lineno) const + { + switch(lvl) + { + case eLogNone: + break; + case eLogDebug: + ss << "[DBG] "; + break; + case eLogInfo: + ss << "[NFO] "; + break; + case eLogWarn: + ss << "[WRN] "; + break; + case eLogError: + ss << "[ERR] "; + break; + } + + ss << "(" << thread_id_string() << ") " << log_timestamp() << " " << fname + << ":" << lineno << "\t"; + } + + void + SysLogStream::Print(LogLevel lvl, const char*, const std::string& msg) const + { + switch(lvl) + { + case eLogNone: + return; + case eLogDebug: + ::syslog(LOG_DEBUG, "%s", msg.c_str()); + return; + case eLogInfo: + ::syslog(LOG_INFO, "%s", msg.c_str()); + return; + case eLogWarn: + ::syslog(LOG_WARNING, "%s", msg.c_str()); + return; + case eLogError: + ::syslog(LOG_ERR, "%s", msg.c_str()); + return; + } + } + + void + SysLogStream::PostLog(std::stringstream&) const + { + } +} // namespace llarp +#endif From 351feadaeccfe7e3403fe9a6f1730910d230c5ea Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Thu, 11 Apr 2019 09:19:58 -0400 Subject: [PATCH 09/11] make it work --- llarp/handlers/tun.cpp | 1 - llarp/router/router.cpp | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index b6ae17a90..950d908b3 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -780,7 +780,6 @@ namespace llarp { #if defined(DEBUG) || !defined(RELEASE_MOTTO) llarp::LogInfo("invalid pkt"); - llarp::DumpBuffer(buf.underlying); #endif } } diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index e55788527..0b93f5b00 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -834,7 +834,7 @@ namespace llarp llarp::LogWarn("failed to load hidden service config for ", key); } } - else if(StrEq(section, "logger")) + else if(StrEq(section, "logging")) { if(StrEq(key, "type") && StrEq(val, "syslog")) { From 4948fd50f8d977eae9db782b49e70ec182845455 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Thu, 11 Apr 2019 09:25:42 -0400 Subject: [PATCH 10/11] be consice --- llarp/util/logger_syslog.cpp | 1 - 1 file changed, 1 deletion(-) delete mode 100644 llarp/util/logger_syslog.cpp diff --git a/llarp/util/logger_syslog.cpp b/llarp/util/logger_syslog.cpp deleted file mode 100644 index ef5206072..000000000 --- a/llarp/util/logger_syslog.cpp +++ /dev/null @@ -1 +0,0 @@ -#include From fda7c4779e24b56e1105b41281ed9e0924c618fa Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Thu, 11 Apr 2019 09:29:52 -0400 Subject: [PATCH 11/11] make win32 work --- llarp/router/router.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index 0b93f5b00..8046ddf40 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -838,8 +838,12 @@ namespace llarp { if(StrEq(key, "type") && StrEq(val, "syslog")) { +#if defined(_WIN32) + LogError("syslog not supported on win32"); +#else LogInfo("Switching to syslog"); LogContext::Instance().logStream = std::make_unique< SysLogStream >(); +#endif } } else if(StrEq(section, "lokid"))