首页 > 代码库 > Boost log 简单使用
Boost log 简单使用
Boost log 简单使用
flyfish 2014-11-8
该示例是在VC2010 MFC Unicode环境下使用
内容包括
flyfish 2014-11-8
该示例是在VC2010 MFC Unicode环境下使用
内容包括
1 启动关闭日志
2 设置日志存储路径3 设置输出日志等级
4 日志是否立即写入文件
5 设置单个文件的大小6 设置磁盘最小可利用空间
Logger.h
#pragma once #include <cassert> #include <iostream> #include <fstream> #include <boost/locale/generator.hpp> #include <boost/date_time/posix_time/posix_time_types.hpp> #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/sources/logger.hpp> #include <boost/log/support/date_time.hpp> #include <boost/filesystem.hpp> #include <boost/log/detail/thread_id.hpp> #include <boost/log/sources/global_logger_storage.hpp> namespace logging = boost::log; namespace sinks = boost::log::sinks; namespace attrs = boost::log::attributes; namespace src = http://www.mamicode.com/boost::log::sources;>
Logger.cpp#include "StdAfx.h" #include "Logger.h" #include <iostream> size_t CLogger::m_szMinFreeSpace=10*1024*1024; size_t CLogger::m_szRotationSize=1*1024*1024; std::wstring CLogger::m_strFilePath=_T("C:\\Log\\"); bool CLogger::m_bAutoFlush=true;; CLogger::CLogger(void) { } CLogger::~CLogger(void) { } void CLogger::Init() { std::locale::global(std::locale("chs")); auto pSink = logging::add_file_log ( keywords::open_mode = std::ios::app,//追加写入 keywords::file_name =m_strFilePath+_T("%Y-%m-%d.log"), keywords::rotation_size = m_szRotationSize, keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), keywords::min_free_space=m_szMinFreeSpace, keywords::format =( expr::stream << "["<<expr::attr<UINT>("RecordID") << "]["<<expr::format_date_time(_timestamp,"%Y-%m-%d %H:%M:%S.%f") << "]["<<_severity << "]" <<expr::wmessage) ); pSink->locked_backend()->auto_flush(m_bAutoFlush); logging::add_common_attributes(); attrs::counter<UINT> RecordID(1); logging::core::get()->add_global_attribute("RecordID", RecordID); ; } void CLogger::Start() { logging::core::get()->set_logging_enabled(true); } void CLogger::Stop() { logging::core::get()->set_logging_enabled(false); } void CLogger::SetFilterTrace() { logging::core::get()->set_filter(expr::attr<severity_level>("Severity") >= trace); } void CLogger::SetFilterWarning() { logging::core::get()->set_filter(expr::attr<severity_level>("Severity")>= warning); } void CLogger::SetFilterError() { logging::core::get()->set_filter(expr::attr<severity_level>("Severity")>= error); } void CLogger::SetLogFilePath(std::wstring strPath) { m_strFilePath=strPath; } void CLogger::SetMinFreeSpace(size_t size) { m_szMinFreeSpace=size* 1024 * 1024; } void CLogger::SetRotationsize(size_t size) { m_szRotationSize=size* 1024 * 1024; }
使用CLogger::Init():src::wseverity_logger< severity_level > slg; BOOST_LOG_SEV(slg, trace) << _T("trace 输出日志ABC"); BOOST_LOG_SEV(slg, warning) << _T("warning 输出日志ABC"); BOOST_LOG_SEV(slg, error) << _T("error 输出日志ABC");
Boost log 简单使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。