boost log
2015-10-06 22:33
218 查看
#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;
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;
}
void TestBoostLog()
{
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") >= normal,
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!";
// Now, let's try logging with severity
src::severity_logger< severity_level > slg;
// Let's pretend we also want to profile our code, so add a special timer attribute.
slg.add_attribute("Uptime", attrs::timer());
BOOST_LOG_SEV(slg, normal) << "A normal severity message, will not pass to the file";
BOOST_LOG_SEV(slg, warning) << "A warning severity message, will pass to the file";
BOOST_LOG_SEV(slg, error) << "An error severity message, will pass to the file";
/*
//#include <boost/log/trivial.hpp>
cout << "hello, world" << endl;
BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
BOOST_LOG_TRIVIAL(debug) << "A debug severity message";
BOOST_LOG_TRIVIAL(info) << "An informational severity message";
BOOST_LOG_TRIVIAL(warning) << "A warning severity message";
BOOST_LOG_TRIVIAL(error) << "An error severity message";
BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";
*/
}
#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;
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;
}
void TestBoostLog()
{
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") >= normal,
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!";
// Now, let's try logging with severity
src::severity_logger< severity_level > slg;
// Let's pretend we also want to profile our code, so add a special timer attribute.
slg.add_attribute("Uptime", attrs::timer());
BOOST_LOG_SEV(slg, normal) << "A normal severity message, will not pass to the file";
BOOST_LOG_SEV(slg, warning) << "A warning severity message, will pass to the file";
BOOST_LOG_SEV(slg, error) << "An error severity message, will pass to the file";
/*
//#include <boost/log/trivial.hpp>
cout << "hello, world" << endl;
BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
BOOST_LOG_TRIVIAL(debug) << "A debug severity message";
BOOST_LOG_TRIVIAL(info) << "An informational severity message";
BOOST_LOG_TRIVIAL(warning) << "A warning severity message";
BOOST_LOG_TRIVIAL(error) << "An error severity message";
BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";
*/
}
相关文章推荐
- Linux多线程基础学习(一)基本概念
- android源码编译出错(1)
- 【第3节】简单排序--冒泡排序
- 人工鱼群算法-python实现
- border --- 透明边框
- HDU 1166 树状数组
- 新公司合伙人
- 干勾美食——超爽牛板筋火锅
- 笔试题目
- Java基础知识学习
- Getting the department_id through the user on domain filter
- Linux 变量
- [HTTP那些事]网络请求API
- iOS中一些常见的方法调用说明
- 项目成本管理/质量管理/人力资源管理重点
- strstr的实现
- 打工图个什么
- 人工蜂群算法-python实现
- [wordpress使用]004_导入多媒体
- 所有这一切为了什么