Boost Log 1
Posted zhanghuan_wangkai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Boost Log 1相关的知识,希望对你有一定的参考价值。
Boost Log 基本用法flyfish 2014-11-5
根据boost提供的代码示例,学习Boost Log 的基本用法
前提
boost版本boost_1_56_0
示例代码文件夹 boost_1_56_0\\libs\\log\\example\\basic_usage
使用的单词很形象,整个过程就像流水一样
假设要输出的日志比作水
水 (Hello, World!)
水槽 (sink)
流向哪里 (console,file)
从哪里取 (source)
水的等级 (severity level)
过滤输出 (filter)
格式输出 (format)
各部分连接者(core)
示例
[cpp] view plain copy
- #include <iostream>
- #include <boost/log/common.hpp>
- #include <boost/log/expressions.hpp>
- #include <boost/log/utility/setup/file.hpp>
- #include <boost/log/utility/setup/console.hpp>
- #include <boost/log/utility/setup/common_attributes.hpp>
- #include <boost/log/attributes/timer.hpp>
- #include <boost/log/attributes/named_scope.hpp>
- #include <boost/log/sources/logger.hpp>
- #include <boost/log/support/date_time.hpp>
- namespace logging = boost::log;
- namespace sinks = boost::log::sinks;
- namespace attrs = boost::log::attributes;
- namespace src = boost::log::sources;
- namespace expr = boost::log::expressions;
- namespace keywords = boost::log::keywords;
- using boost::shared_ptr;
- // Here we define our application severity levels.
- enum severity_level
- normal,
- notification,
- warning,
- error,
- critical
- ;
- // The formatting logic for the severity level
- template< typename CharT, typename TraitsT >
- inline std::basic_ostream< CharT, TraitsT >& operator<< (
- std::basic_ostream< CharT, TraitsT >& strm, severity_level lvl)
- static const char* const str[] =
- "normal",
- "notification",
- "warning",
- "error",
- "critical"
- ;
- if (static_cast< std::size_t >(lvl) < (sizeof(str) / sizeof(*str)))
- strm << str[lvl];
- else
- strm << static_cast< int >(lvl);
- return strm;
- int _tmain(int argc, char* argv[])
- // This is a simple tutorial/example of Boost.Log usage
- // The first thing we have to do to get using the library is
- // to set up the logging sinks - i.e. where the logs will be written to.
- logging::add_console_log(std::clog, keywords::format = "%TimeStamp%: %Message%");
- // One can also use lambda expressions to setup filters and formatters
- logging::add_file_log
- (
- "sample.log",
- keywords::filter = expr::attr< severity_level >("Severity") >= warning,
- keywords::format = expr::stream
- << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d, %H:%M:%S.%f")
- << " [" << expr::format_date_time< attrs::timer::value_type >("Uptime", "%O:%M:%S")
- << "] [" << expr::format_named_scope("Scope", keywords::format = "%n (%f:%l)")
- << "] <" << expr::attr< severity_level >("Severity")
- << "> " << expr::message
- /*
- keywords::format = expr::format("%1% [%2%] [%3%] <%4%> %5%")
- % expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d, %H:%M:%S.%f")
- % expr::format_date_time< attrs::timer::value_type >("Uptime", "%O:%M:%S")
- % expr::format_named_scope("Scope", keywords::format = "%n (%f:%l)")
- % expr::attr< severity_level >("Severity")
- % expr::message
- */
- );
- // Also let's add some commonly used attributes, like timestamp and record counter.
- logging::add_common_attributes();
- logging::core::get()->add_thread_attribute("Scope", attrs::named_scope());
- BOOST_LOG_FUNCTION();
- // Now our logs will be written both to the console and to the file.
- // Let's do a quick test and output something. We have to create a logger for this.
- src::logger lg;
- // And output...
- BOOST_LOG(lg) << "Hello, World!";
- Boost Log 运行时优化