diff --git a/src/log.cpp b/src/log.cpp index 9cb806d..6f376c5 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -32,53 +32,62 @@ namespace epubgrep::log { +namespace blog = boost::log; +namespace keywords = boost::log::keywords; +using boost::locale::translate; using sev = boost::log::trivial::severity_level; +inline static global_variables global; + +fs::path log_dir() +{ + fs::path path{helpers::get_env("XDG_STATE_HOME")}; + if (path.empty()) + { + path = helpers::get_env("HOME"); + if (!path.empty()) + { + path /= ".local"; + path /= "state"; + } + } + if (!path.empty()) + { + return path /= "epubgrep"; + } + return fs::path{}; +} + void init() { - namespace keywords = boost::log::keywords; + global.textlog = blog::add_file_log( + keywords::file_name = (log_dir() / "epubgrep.log").c_str(), + keywords::format = "%LineID% [%TimeStamp%] " + "[%ThreadID%]: [%Severity%] %Message%"); + global.textlog->set_filter(blog::trivial::severity >= sev::info); - using boost::locale::translate; + blog::add_console_log(std::cerr, + keywords::format = translate("WARNING").str() + + ": %Message%") + ->set_filter(blog::trivial::severity == sev::warning); + blog::add_console_log(std::cerr, keywords::format = translate("ERROR").str() + + ": %Message%") + ->set_filter(blog::trivial::severity == sev::error); + blog::add_console_log(std::cerr, + keywords::format = translate("FATAL ERROR").str() + + ": %Message%") + ->set_filter(blog::trivial::severity == sev::fatal); - 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"; - path /= "state"; - } - } - if (!path.empty()) - { - return (path /= "epubgrep") /= "epubgrep.log"; - } - return fs::path{}; - }()}; + blog::add_common_attributes(); +} - boost::log::add_file_log(keywords::file_name = log_path.c_str(), - keywords::format = - "%LineID% [%TimeStamp%] " - "[%ThreadID%]: [%Severity%] %Message%"); +void init_debug() +{ + global.textlog->set_filter(blog::trivial::severity == sev::debug); - 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_console_log(std::cerr, - keywords::format = translate("FATAL ERROR") - .str() - + ": %Message%") - ->set_filter(boost::log::trivial::severity == sev::fatal); - - boost::log::add_common_attributes(); + blog::add_console_log(std::cerr, + keywords::format = "%LineID% DEBUG: %Message%") + ->set_filter(blog::trivial::severity >= sev::debug); } } // namespace epubgrep::log diff --git a/src/log.hpp b/src/log.hpp index dee7886..338ed95 100644 --- a/src/log.hpp +++ b/src/log.hpp @@ -17,22 +17,40 @@ #ifndef EPUBGREP_LOG_HPP #define EPUBGREP_LOG_HPP +#include "fs-compat.hpp" + +#include +#include #include #include #include +#include #define LOG(severity) BOOST_LOG_SEV(epubgrep::log::logger::get(), severity) namespace epubgrep::log { +namespace blog = boost::log; using sev = boost::log::trivial::severity_level; +struct global_variables +{ + + boost::shared_ptr< + blog::sinks::synchronous_sink> + textlog; +}; + BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT( logger, boost::log::sources::severity_logger_mt) +fs::path log_dir(); + void init(); +void init_debug(); + } // namespace epubgrep::log #endif // EPUBGREP_LOG_HPP