From 4c897f583c934ed35caede094edb614cbfff96bf Mon Sep 17 00:00:00 2001 From: Jeff Date: Thu, 18 Aug 2022 10:31:39 -0400 Subject: [PATCH] fix up log statements * make socket bind errors have a distinct message reported when caught using their own exception type * omit printing banner in setup when we run from the lokinet executable (but not the liblokinet.so entry point) --- daemon/lokinet.cpp | 15 ++++++++++----- include/llarp.hpp | 2 +- llarp/context.cpp | 14 ++++++-------- llarp/ev/ev_libuv.cpp | 14 ++++++-------- llarp/util/exceptions.hpp | 10 ++++++++++ 5 files changed, 33 insertions(+), 22 deletions(-) create mode 100644 llarp/util/exceptions.hpp diff --git a/daemon/lokinet.cpp b/daemon/lokinet.cpp index 0cb1e20df..53655c860 100644 --- a/daemon/lokinet.cpp +++ b/daemon/lokinet.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -227,7 +228,7 @@ uninstall_win32_daemon() static void run_main_context(std::optional confFile, const llarp::RuntimeOptions opts) { - llarp::LogTrace("start of run_main_context()"); + llarp::LogInfo(fmt::format("starting up {} {}", llarp::VERSION_FULL, llarp::RELEASE_MOTTO)); try { std::shared_ptr conf; @@ -261,14 +262,18 @@ run_main_context(std::optional confFile, const llarp::RuntimeOptions o { ctx->Setup(opts); } + catch (llarp::util::bind_socket_error& ex) + { + llarp::LogError(fmt::format("{}, is lokinet already running? 🤔", ex.what())); + exit_code.set_value(1); + return; + } catch (std::exception& ex) { - llarp::LogError( - "failed to set up lokinet: ", ex.what(), ", is lokinet already running? 🤔"); + llarp::LogError("failed to start up lokinet: {}", ex.what()); exit_code.set_value(1); return; } - llarp::util::SetThreadName("llarp-mainloop"); auto result = ctx->Run(opts); @@ -385,6 +390,7 @@ lokinet_main(int argc, char* argv[]) llarp::log::reset_level(llarp::log::Level::info); llarp::RuntimeOptions opts; + opts.showBanner = false; #ifdef _WIN32 WindowsServiceStopped stopped_raii; @@ -428,7 +434,6 @@ lokinet_main(int argc, char* argv[]) std::cout << options.help() << std::endl; return 0; } - if (result.count("version")) { std::cout << llarp::VERSION_FULL << std::endl; diff --git a/include/llarp.hpp b/include/llarp.hpp index f6bf5dad2..402ffa52e 100644 --- a/include/llarp.hpp +++ b/include/llarp.hpp @@ -31,7 +31,7 @@ namespace llarp struct RuntimeOptions { - bool background = false; + bool showBanner = true; bool debug = false; bool isSNode = false; }; diff --git a/llarp/context.cpp b/llarp/context.cpp index 64636ea77..e5b2074e2 100644 --- a/llarp/context.cpp +++ b/llarp/context.cpp @@ -61,8 +61,9 @@ namespace llarp if (not config) throw std::runtime_error("Cannot call Setup() on context without a Config"); - llarp::LogInfo(llarp::VERSION_FULL, " ", llarp::RELEASE_MOTTO); - llarp::LogInfo("starting up"); + if (opts.showBanner) + llarp::LogInfo(fmt::format("{} {}", llarp::VERSION_FULL, llarp::RELEASE_MOTTO)); + if (!loop) { auto jobQueueSize = std::max(event_loop_queue_size, config->router.m_JobQueueSize); @@ -104,7 +105,7 @@ namespace llarp } int - Context::Run(const RuntimeOptions& opts) + Context::Run(const RuntimeOptions&) { if (router == nullptr) { @@ -113,11 +114,8 @@ namespace llarp return 1; } - if (!opts.background) - { - if (!router->Run()) - return 2; - } + if (not router->Run()) + return 2; // run net io thread llarp::LogInfo("running mainloop"); diff --git a/llarp/ev/ev_libuv.cpp b/llarp/ev/ev_libuv.cpp index 1fd2476ca..d5f009f16 100644 --- a/llarp/ev/ev_libuv.cpp +++ b/llarp/ev/ev_libuv.cpp @@ -3,8 +3,8 @@ #include #include #include +#include #include - #include #include "ev.hpp" @@ -315,16 +315,14 @@ namespace llarp::uv if (handle->active()) reset_handle(handle->loop()); - bool good = true; - auto err = handle->on([&](auto& event, auto&) { - llarp::LogError("failed to bind and start receiving on ", addr, ": ", event.what()); - good = false; + auto err = handle->on([addr](auto& event, auto&) { + throw llarp::util::bind_socket_error{ + fmt::format("failed to bind udp socket on {}: {}", addr, event.what())}; }); handle->bind(*static_cast(addr)); - if (good) - handle->recv(); + handle->recv(); handle->erase(err); - return good; + return true; } bool diff --git a/llarp/util/exceptions.hpp b/llarp/util/exceptions.hpp new file mode 100644 index 000000000..c7d7dbc23 --- /dev/null +++ b/llarp/util/exceptions.hpp @@ -0,0 +1,10 @@ +#pragma once + +namespace llarp::util +{ + class bind_socket_error : public std::runtime_error + { + public: + using std::runtime_error::runtime_error; + }; +} // namespace llarp::util