From ac5b31f2d55568ef0b72af9c2f931c5d78d5f4cd Mon Sep 17 00:00:00 2001 From: tastytea Date: Mon, 31 May 2021 18:50:41 +0200 Subject: [PATCH] Add logger. --- CMakeLists.txt | 2 +- src/CMakeLists.txt | 2 ++ src/helpers.cpp | 11 +++++++ src/helpers.hpp | 3 ++ src/log.cpp | 77 ++++++++++++++++++++++++++++++++++++++++++++++ src/log.hpp | 38 +++++++++++++++++++++++ src/options.cpp | 16 ++-------- 7 files changed, 135 insertions(+), 14 deletions(-) create mode 100644 src/log.cpp create mode 100644 src/log.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 2e40e35..26f5dc3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,7 +24,7 @@ include(cmake/debug_flags.cmake) # All dependencies except test dependencies. set(CMAKE_FIND_PACKAGE_PREFER_CONFIG TRUE) -find_package(Boost 1.65.0 REQUIRED COMPONENTS locale program_options regex) +find_package(Boost 1.65.0 REQUIRED COMPONENTS locale log program_options regex) find_package(Gettext REQUIRED) find_package(Filesystem REQUIRED COMPONENTS Final Experimental) find_package(LibArchive 3.2 REQUIRED) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 71d2332..32f2462 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -17,7 +17,9 @@ unset(headers_src) target_link_libraries(${PROJECT_NAME}_lib PUBLIC + Boost::dynamic_linking Boost::locale + Boost::log Boost::program_options Boost::regex std::filesystem diff --git a/src/helpers.cpp b/src/helpers.cpp index 607d505..6638327 100644 --- a/src/helpers.cpp +++ b/src/helpers.cpp @@ -182,4 +182,15 @@ std::string unescape_html(const std::string_view html) return output; } +std::string_view get_env(const std::string_view name) +{ + const char *env = std::getenv(name.data()); + if (env != nullptr) + { + return env; + } + + return {}; +} + } // namespace epubgrep::helpers diff --git a/src/helpers.hpp b/src/helpers.hpp index 7784245..949c6ee 100644 --- a/src/helpers.hpp +++ b/src/helpers.hpp @@ -15,6 +15,9 @@ namespace epubgrep::helpers //! Un-escape &, and so on. [[nodiscard]] std::string unescape_html(std::string_view html); +//! Returns environment variable or an empty string_view. +[[nodiscard]] std::string_view get_env(std::string_view name); + } // namespace epubgrep::helpers #endif // EPUBGREP_HELPERS_HPP diff --git a/src/log.cpp b/src/log.cpp new file mode 100644 index 0000000..1adcae0 --- /dev/null +++ b/src/log.cpp @@ -0,0 +1,77 @@ +/* This file is part of epubgrep. + * Copyright © 2021 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "log.hpp" + +#include "fs-compat.hpp" +#include "helpers.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include + +namespace epubgrep::log +{ + +using sev = boost::log::trivial::severity_level; + +void init() +{ + namespace keywords = boost::log::keywords; + + using boost::locale::translate; + + const auto log_path{[] + { + fs::path path{helpers::get_env("XDG_STATE_HOME")}; + if (path.empty()) + { + path = helpers::get_env("HOME"); + if (!path.empty()) + { + path /= ".local/state"; + } + } + if (!path.empty()) + { + return path /= "epubgrep.log"; + } + return fs::path{}; + }()}; + + boost::log::add_file_log(keywords::file_name = log_path.c_str(), + keywords::format = + "%LineID% [%TimeStamp%] " + "[%ThreadID%]: [%Severity%] %Message%"); + boost::log::add_console_log(std::cerr, + keywords::format = translate("WARNING").str() + + ": %Message%") + ->set_filter(boost::log::trivial::severity == sev::warning); + boost::log::add_console_log(std::cerr, + keywords::format = translate("ERROR").str() + + ": %Message%") + ->set_filter(boost::log::trivial::severity == sev::error); + + boost::log::add_common_attributes(); +} + +} // namespace epubgrep::log diff --git a/src/log.hpp b/src/log.hpp new file mode 100644 index 0000000..b84af35 --- /dev/null +++ b/src/log.hpp @@ -0,0 +1,38 @@ +/* This file is part of epubgrep. + * Copyright © 2021 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef EPUBGREP_LOG_HPP +#define EPUBGREP_LOG_HPP + +#include +#include +#include + +#define LOG BOOST_LOG_SEV + +namespace epubgrep::log +{ + +using sev = boost::log::trivial::severity_level; + +BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT( + logger, boost::log::sources::severity_logger_mt) + +void init(); + +} // namespace epubgrep::log + +#endif // EPUBGREP_LOG_HPP diff --git a/src/options.cpp b/src/options.cpp index a8d2d58..652107e 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -17,6 +17,7 @@ #include "options.hpp" #include "fs-compat.hpp" +#include "helpers.hpp" #include "version.hpp" #include @@ -149,21 +150,10 @@ options parse_options(int argc, char *argv[]) fs::path get_config_path() { - const auto get_env{[](const std::string &name) - { - const char *env = std::getenv(name.c_str()); - if (env != nullptr) - { - return env; - } - - return ""; - }}; - - fs::path path{get_env("XDG_CONFIG_HOME")}; + fs::path path{helpers::get_env("XDG_CONFIG_HOME")}; if (path.empty()) { - path = get_env("HOME"); + path = helpers::get_env("HOME"); if (!path.empty()) { path /= ".config";