2021-05-31 18:50:41 +02:00
|
|
|
/* 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
|
|
|
|
{
|
|
|
|
|
2021-06-01 10:56:01 +02:00
|
|
|
namespace blog = boost::log;
|
|
|
|
namespace keywords = boost::log::keywords;
|
|
|
|
using boost::locale::translate;
|
2021-05-31 18:50:41 +02:00
|
|
|
using sev = boost::log::trivial::severity_level;
|
|
|
|
|
2021-06-01 10:56:01 +02:00
|
|
|
inline static global_variables global;
|
|
|
|
|
|
|
|
void init()
|
|
|
|
{
|
2021-06-01 11:07:34 +02:00
|
|
|
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())
|
|
|
|
{
|
|
|
|
path /= "epubgrep";
|
|
|
|
}
|
|
|
|
return path / "epubgrep.log";
|
|
|
|
}()};
|
|
|
|
|
2021-06-01 10:56:01 +02:00
|
|
|
global.textlog = blog::add_file_log(
|
2021-06-01 11:07:34 +02:00
|
|
|
keywords::file_name = log_path.c_str(),
|
2021-06-01 10:56:01 +02:00
|
|
|
keywords::format = "%LineID% [%TimeStamp%] "
|
|
|
|
"[%ThreadID%]: [%Severity%] %Message%");
|
|
|
|
global.textlog->set_filter(blog::trivial::severity >= sev::info);
|
2021-05-31 18:50:41 +02:00
|
|
|
|
2021-06-01 10:56:01 +02:00
|
|
|
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);
|
2021-05-31 18:50:41 +02:00
|
|
|
|
2021-06-01 10:56:01 +02:00
|
|
|
blog::add_common_attributes();
|
|
|
|
}
|
2021-05-31 22:29:35 +02:00
|
|
|
|
2021-06-01 10:56:01 +02:00
|
|
|
void init_debug()
|
|
|
|
{
|
|
|
|
global.textlog->set_filter(blog::trivial::severity == sev::debug);
|
2021-05-31 18:50:41 +02:00
|
|
|
|
2021-06-01 10:56:01 +02:00
|
|
|
blog::add_console_log(std::cerr,
|
|
|
|
keywords::format = "%LineID% DEBUG: %Message%")
|
|
|
|
->set_filter(blog::trivial::severity >= sev::debug);
|
2021-05-31 18:50:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace epubgrep::log
|