Skip to content

Instantly share code, notes, and snippets.

@zhangfuwen
Forked from jiahaowu/log-system.cpp
Created July 7, 2021 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangfuwen/525bc2685fda962a405d17787703d8a1 to your computer and use it in GitHub Desktop.
Save zhangfuwen/525bc2685fda962a405d17787703d8a1 to your computer and use it in GitHub Desktop.
example of using boost log, which includes stdout, file output, log rotation, message format
#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/sources/severity_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>
namespace is = ImageStitching;
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;
void log_init() {
logging::register_simple_formatter_factory<
boost::log::trivial::severity_level, char>("Severity");
logging::add_file_log(
keywords::target = "logs/", keywords::file_name = "%y%m%d_%3N.log",
keywords::rotation_size = 10 * 1024 * 1024,
keywords::scan_method = sinks::file::scan_matching,
keywords::format = "[%TimeStamp%][%Severity%]: %Message%");
logging::add_console_log(std::cout,
boost::log::keywords::format = ">> %Message%");
logging::core::get()->set_filter(logging::trivial::severity >=
logging::trivial::info);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment