boost log 和 log4cplus

boost log 

编译
-DBOOST_LOG_DYN_LINK
-lboost_log
#include <iostream>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/core.hpp>
#include <boost/log/utility/setup/file.hpp>
using  namespace  boost;


void init()
{
    logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::info);
}

int main(int, char*[])
{
    init();

    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";

    return 0;
}


 
[2015-03-22 16:52:12.282451] [0x0b77e96e] [info]    An informational severity message
[2015-03-22 16:52:12.282843] [0x0b77e96e] [warning] A warning severity message
[2015-03-22 16:52:12.282854] [0x0b77e96e] [error]   An error severity message
[2015-03-22 16:52:12.282863] [0x0b77e96e] [fatal]   A fatal severity message

boost文档上说是线程安全的:
It is safe to write logs from different threads concurrently, log messages will not be corrupted.


如果需要输出到文件,只需要在上面的init函数中加入
logging::add_file_log("sample.log");

如果需要格式化输出



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


#include <fstream>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/thread/thread.hpp>
#include <boost/log/attributes/scoped_attribute.hpp>
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;

using namespace logging::trivial;
void InitLog() {
  typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
  boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();
  sink->locked_backend()->add_stream(
					 boost::make_shared< std::ofstream >("sign.log"));
  sink->set_formatter (
			   expr::format("[%1%]<%2%>(%3%): %4%")
			   % expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S")
			   % logging::trivial::severity
			   % expr::attr<boost::log::attributes::current_thread_id::value_type >("ThreadID")
			   % expr::smessage
			   );
  logging::core::get()->add_sink(sink);
  logging::add_common_attributes();

  sink->set_filter(expr::attr< severity_level >("Severity") < warning);

  BOOST_LOG_SCOPED_THREAD_TAG("ThreadID", boost::this_thread::get_id());
}
int main(int, char*[]) {
  InitLog();

  src::severity_logger< severity_level > lg;
  BOOST_LOG_SEV(lg, trace) << "A trace severity message";
  BOOST_LOG_SEV(lg, debug) << "A debug severity message";
  BOOST_LOG_SEV(lg, info) << "An informational severity message";
  BOOST_LOG_SEV(lg, warning) << "A warning severity message";
  BOOST_LOG_SEV(lg, error) << "An error severity message";
  BOOST_LOG_SEV(lg, fatal) << "A fatal severity message";
  return 0;
}




 
log4cplus
交叉编译log4cplus
cd /boost/log4cplus-1.0.4
mkdir __INSTALL
cd __INSTALL/
export PATH=$PATH:/opt/freescale-mx5/usr/local/gcc-4.4.4-glibc-2.11.1-multilib-1.0/arm-fsl-linux-gnueabi/bin/
CC=arm-linux-gcc
./configure --prefix=/boost/log4cplus-1.0.4/__INSTALL/ --host=arm-linux

mips下的log4c交叉编译
1.cd /opt/log4c-1.2.4
2.在此目录下创建__INSTALL目录,作为输出目录,即执行configure的时候需要设置--prefix=/opt/log4c-1.2.4/__INSTALL/
3.设置编译器路径export PATH=$PATH:/home/vmuser/wtrb-env/openwrt-r32683/trunk/staging_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/bin/
4.编译器以mips-openwrt-linux-开头,所以执行configure的时候需要设置--host=mips-openwrt-linux ,make的时候会自动调用mips-openwrt-linux-gcc等
5.头文件,库文件什么的需要CFLAGS环境变量来设置
6.最终
CFLAGS="-I/home/vmuser/wtrb-env/openwrt-r32683/trunk/staging_dir/target-mips_r2_uClibc-0.9.33.2/usr/include -L/home/vmuser/wtrb-env/openwrt-r32683/trunk/staging_dir/target-mips_r2_uClibc-0.9.33.2/usr/lib" ./configure --prefix=/opt/log4c-1.2.4/__INSTALL/ --host=mips-openwrt-linux
make
make install

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值