Add logger.

This commit is contained in:
tastytea 2021-05-31 18:50:41 +02:00
parent cf583c6d7f
commit ac5b31f2d5
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
7 changed files with 135 additions and 14 deletions

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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

77
src/log.cpp Normal file
View File

@ -0,0 +1,77 @@
/* This file is part of epubgrep.
* Copyright © 2021 tastytea <tastytea@tastytea.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "log.hpp"
#include "fs-compat.hpp"
#include "helpers.hpp"
#include <boost/locale/message.hpp>
#include <boost/log/core.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <iostream>
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

38
src/log.hpp Normal file
View File

@ -0,0 +1,38 @@
/* This file is part of epubgrep.
* Copyright © 2021 tastytea <tastytea@tastytea.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef EPUBGREP_LOG_HPP
#define EPUBGREP_LOG_HPP
#include <boost/log/sources/global_logger_storage.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/trivial.hpp>
#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<sev>)
void init();
} // namespace epubgrep::log
#endif // EPUBGREP_LOG_HPP

View File

@ -17,6 +17,7 @@
#include "options.hpp"
#include "fs-compat.hpp"
#include "helpers.hpp"
#include "version.hpp"
#include <boost/locale/message.hpp>
@ -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";