Add equipment for debug logs.
This commit is contained in:
parent
c35434e745
commit
a8f2b7dfb6
87
src/log.cpp
87
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
|
||||
|
|
18
src/log.hpp
18
src/log.hpp
|
@ -17,22 +17,40 @@
|
|||
#ifndef EPUBGREP_LOG_HPP
|
||||
#define EPUBGREP_LOG_HPP
|
||||
|
||||
#include "fs-compat.hpp"
|
||||
|
||||
#include <boost/log/sinks/sync_frontend.hpp>
|
||||
#include <boost/log/sinks/text_file_backend.hpp>
|
||||
#include <boost/log/sources/global_logger_storage.hpp>
|
||||
#include <boost/log/sources/logger.hpp>
|
||||
#include <boost/log/trivial.hpp>
|
||||
#include <boost/smart_ptr/shared_ptr.hpp>
|
||||
|
||||
#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<blog::sinks::text_file_backend>>
|
||||
textlog;
|
||||
};
|
||||
|
||||
BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(
|
||||
logger, boost::log::sources::severity_logger_mt<sev>)
|
||||
|
||||
fs::path log_dir();
|
||||
|
||||
void init();
|
||||
|
||||
void init_debug();
|
||||
|
||||
} // namespace epubgrep::log
|
||||
|
||||
#endif // EPUBGREP_LOG_HPP
|
||||
|
|
Loading…
Reference in New Issue
Block a user