boost写日志

单个写日志

#include 
#include 
#include 
#include 
#include 
#include 

int main() {
    boost::log::add_file_log(
        boost::log::keywords::file_name = "example.log",
        boost::log::keywords::rotation_size = 10 * 1024 * 1024,
        boost::log::keywords::format = (
            boost::log::expressions::stream
            << "[" << boost::log::expressions::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S.%f") << "]"
            << "[" << boost::log::trivial::severity << "]"
            << " " << boost::log::expressions::smessage
        )
    );

    boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::debug);
    boost::log::add_common_attributes();

    BOOST_LOG_TRIVIAL(debug) << "Hello, Boost log!";

    return 0;
}

根据日志等级写日志

#include 
#include 
#include 
#include 
#include 
#include 

int main() {
    // 为 info 级别日志设置接收器
    boost::log::add_file_log(
        boost::log::keywords::file_name = "info.log",
        boost::log::keywords::filter = boost::log::trivial::severity == boost::log::trivial::info,
        boost::log::keywords::format = (
            boost::log::expressions::stream
            << "[" << boost::log::expressions::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S.%f") << "]"
            << "[" << boost::log::trivial::severity << "]"
            << " " << boost::log::expressions::smessage
        )
    );

    // 为 error 级别日志设置接收器
    boost::log::add_file_log(
        boost::log::keywords::file_name = "error.log",
        boost::log::keywords::filter = boost::log::trivial::severity == boost::log::trivial::error,
        boost::log::keywords::format = (
            boost::log::expressions::stream
            << "[" << boost::log::expressions::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S.%f") << "]"
            << "[" << boost::log::trivial::severity << "]"
            << " " << boost::log::expressions::smessage
        )
    );

    boost::log::add_common_attributes();

    BOOST_LOG_TRIVIAL(info) << "This is an info message.";
    BOOST_LOG_TRIVIAL(error) << "This is an error message.";

    return 0;
}

自定义过滤器

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

namespace logging = boost::log;
namespace attrs = boost::log::attributes;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;

// 自定义属性
BOOST_LOG_ATTRIBUTE_KEYWORD(module, "Module", std::string)

int main() {
    // 为 ModuleA 设置接收器
    logging::add_file_log(
        logging::keywords::file_name = "moduleA.log",
        logging::keywords::filter = module == "ModuleA",
        logging::keywords::format = (
            expr::stream
            << "[" << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S.%f") << "]"
            << "[" << logging::trivial::severity << "]"
            << "[" << module << "]"
            << " " << expr::smessage
        )
    );

    // 为 ModuleB 设置接收器
    logging::add_file_log(
        logging::keywords::file_name = "moduleB.log",
        logging::keywords::filter = module == "ModuleB",
        logging::keywords::format = (
            expr::stream
            << "[" << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S.%f") << "]"
            << "[" << logging::trivial::severity << "]"
            << "[" << module << "]"
            << " " << expr::smessage
        )
    );

    logging::add_common_attributes();

	src::severity_logger<logging::trivial::severity_level> lg;
	auto modulea=lg.add_attribute("Module", attrs::constant<std::string>("ModuleA"));
	BOOST_LOG_SEV(lg, logging::trivial::info) << "This is a message from ModuleA.";

	lg.remove_attribute(modulea.first);
	lg.add_attribute("Module", attrs::constant<std::string>("ModuleB"));
	BOOST_LOG_SEV(lg, logging::trivial::info) << "This is a message from ModuleB.";

    return 0;
}

你可能感兴趣的:(c++,pcl以及三维,算法,c++)