标签:boost log
Boost log 简单使用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 = boost::log::sources; namespace expr = boost::log::expressions; namespace keywords = boost::log::keywords; enum severity_level { trace, warning, error }; 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[] = { "trace", "warning", "error" }; if (static_cast< std::size_t >(lvl) < (sizeof(str) / sizeof(*str))) strm << str[lvl]; else strm << static_cast< int >(lvl); return strm; } BOOST_LOG_ATTRIBUTE_KEYWORD(_severity, "Severity", severity_level) BOOST_LOG_ATTRIBUTE_KEYWORD(_timestamp, "TimeStamp", boost::posix_time::ptime) class CLogger { public: CLogger(void); ~CLogger(void); static void Init(); static void Start(); static void Stop(); //日志严重等级过滤输出 static void SetFilterTrace(); static void SetFilterWarning(); static void SetFilterError(); void SetLogFilePath(std::wstring strPath); void SetMinFreeSpace(size_t size); void SetRotationsize(size_t size); private: static size_t m_szMinFreeSpace; static size_t m_szRotationSize; static std::wstring m_strFilePath; static bool m_bAutoFlush; };
#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
原文地址:http://blog.csdn.net/flyfish1986/article/details/40921853