diff --git a/src/logging/Sentry.cpp b/src/logging/Sentry.cpp index d9c2bd514..46a283c1c 100644 --- a/src/logging/Sentry.cpp +++ b/src/logging/Sentry.cpp @@ -19,45 +19,21 @@ constexpr int MAC_LEN = 18; namespace spdlog::sinks { - template sentry_api_sink::sentry_api_sink(const std::string &sentryAddress) + /// Set version related information to the Sentry context + inline void setVersionContext() { - if (sentryAddress.empty()) - { - return; - } - _sentryAvailable = true; - - // Set options - sentry_options_t *sentryOptions = sentry_options_new(); - sentry_options_set_release(sentryOptions, PROJECT_FULL_REVISION); - sentry_options_set_dsn(sentryOptions, sentryAddress.c_str()); - - // Init - sentry_init(sentryOptions); - - // Tags - sentry_set_tag("compiler.name", COMPILER_NAME); - sentry_set_tag("compiler.version", COMPILER_VERSION); - sentry_set_tag("build", BUILD_TYPE); - - // Context: Version std::string versionBuffer; const sentry_value_t versionContext = sentry_value_new_object(); versionBuffer = "v" + std::string(PROJECT_FULL_REVISION); sentry_value_set_by_key(versionContext, PROJECT_NAME, sentry_value_new_string(versionBuffer.c_str())); versionBuffer = std::string(PROJECT_BUILD_DATE) + " " + PROJECT_BUILD_TIME; sentry_value_set_by_key(versionContext, "Release Date", sentry_value_new_string(versionBuffer.c_str())); - /* ################################################################################### */ - /* ############################# MAKE MODIFICATIONS HERE ############################# */ - /* ################################################################################### */ - - /* ################################################################################### */ - /* ################################ END MODIFICATIONS ################################ */ - /* ################################################################################### */ - sentry_set_context("Version", versionContext); + } - // Context: Host + /// Set host related information to the Sentry context + inline void setHostContext() + { std::array hostBuffer{}; gethostname(hostBuffer.data(), BUFSIZ); const sentry_value_t hostContext = sentry_value_new_object(); @@ -77,65 +53,99 @@ namespace spdlog::sinks sentry_value_set_by_key(hostContext, "Vendor ID", sentry_value_new_string(word.c_str())); sentry_set_context("Host", hostContext); + } - // Context: Network - const sentry_value_t networkContext = sentry_value_new_object(); + /// Set network related information to the Sentry context + inline void setNetworkContext() + { + ifaddrs *ifaddr = nullptr; + if (getifaddrs(&ifaddr) < 0) + { + return; + } - if (ifaddrs *ifaddr = nullptr; getifaddrs(&ifaddr) != -1) + // Iterate interfaces + const sentry_value_t networkContext = sentry_value_new_object(); + for (ifaddrs *ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) { - // Iterate interfaces - for (ifaddrs *ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) + if (ifa->ifa_addr == nullptr || ((ifa->ifa_flags & IFF_PROMISC) != 0) || ((ifa->ifa_flags & IFF_UP) != 0)) { - if (ifa->ifa_addr == nullptr) - { - continue; - } + continue; + } - switch (ifa->ifa_addr->sa_family) + switch (ifa->ifa_addr->sa_family) + { + case AF_INET: { + std::array host{}; + inet_ntop(AF_INET, &(reinterpret_cast(ifa->ifa_addr))->sin_addr, host.data(), + INET_ADDRSTRLEN); + sentry_value_set_by_key(networkContext, (std::string(ifa->ifa_name) + ".ipv4").c_str(), + sentry_value_new_string(host.data())); + break; + } + case AF_INET6: { + std::array host{}; + inet_ntop(AF_INET6, &(reinterpret_cast(ifa->ifa_addr))->sin6_addr, host.data(), + INET6_ADDRSTRLEN); + sentry_value_set_by_key(networkContext, (std::string(ifa->ifa_name) + ".ipv6").c_str(), + sentry_value_new_string(host.data())); + break; + } + case AF_PACKET: { + std::array host{}; + if (const auto *sock = reinterpret_cast(ifa->ifa_addr); + snprintf(host.data(), MAC_LEN, "%02x:%02x:%02x:%02x:%02x:%02x", sock->sll_addr[0], + sock->sll_addr[1], sock->sll_addr[2], sock->sll_addr[3], sock->sll_addr[4], + sock->sll_addr[5]) > 0) { - case AF_INET: - if (((ifa->ifa_flags & IFF_PROMISC) != 0) || ((ifa->ifa_flags & IFF_UP) != 0)) - { - std::array host{}; - inet_ntop(AF_INET, &(reinterpret_cast(ifa->ifa_addr))->sin_addr, host.data(), - INET_ADDRSTRLEN); - sentry_value_set_by_key(networkContext, (std::string(ifa->ifa_name) + ".ipv4").c_str(), - sentry_value_new_string(host.data())); - } - break; - case AF_INET6: - if (((ifa->ifa_flags & IFF_PROMISC) != 0) || ((ifa->ifa_flags & IFF_UP) != 0)) - { - std::array host{}; - inet_ntop(AF_INET6, &(reinterpret_cast(ifa->ifa_addr))->sin6_addr, host.data(), - INET6_ADDRSTRLEN); - sentry_value_set_by_key(networkContext, (std::string(ifa->ifa_name) + ".ipv6").c_str(), - sentry_value_new_string(host.data())); - } - break; - case AF_PACKET: - if (((ifa->ifa_flags & IFF_PROMISC) != 0) || ((ifa->ifa_flags & IFF_UP) != 0)) - { - std::array host{}; - const auto *sock = reinterpret_cast(ifa->ifa_addr); - if (snprintf(host.data(), MAC_LEN, "%02x:%02x:%02x:%02x:%02x:%02x", sock->sll_addr[0], - sock->sll_addr[1], sock->sll_addr[2], sock->sll_addr[3], sock->sll_addr[4], - sock->sll_addr[5]) > 0) - { - sentry_value_set_by_key(networkContext, (std::string(ifa->ifa_name) + ".mac").c_str(), - sentry_value_new_string(host.data())); - } - } - break; - default: - break; + sentry_value_set_by_key(networkContext, (std::string(ifa->ifa_name) + ".mac").c_str(), + sentry_value_new_string(host.data())); } + break; + } + default: + break; } - freeifaddrs(ifaddr); } + freeifaddrs(ifaddr); + sentry_set_context("Network", networkContext); } + template sentry_api_sink::sentry_api_sink(const std::string &sentryAddress) + { + if (sentryAddress.empty()) + { + return; + } + _sentryAvailable = true; + + // Set options + sentry_options_t *sentryOptions = sentry_options_new(); + sentry_options_set_release(sentryOptions, PROJECT_FULL_REVISION); + sentry_options_set_dsn(sentryOptions, sentryAddress.c_str()); + + // Init + sentry_init(sentryOptions); + + // Tags + sentry_set_tag("compiler.name", COMPILER_NAME); + sentry_set_tag("compiler.version", COMPILER_VERSION); + sentry_set_tag("build", BUILD_TYPE); + + /* ################################################################################### */ + /* ############################# MAKE MODIFICATIONS HERE ############################# */ + /* ################################################################################### */ + + /* ################################################################################### */ + /* ################################ END MODIFICATIONS ################################ */ + /* ################################################################################### */ + + setVersionContext(); + setHostContext(); + setNetworkContext(); + } + template sentry_api_sink::~sentry_api_sink() { sentry_close(); } template void sentry_api_sink::sink_it_(const details::log_msg &msg)