SEED_LOG----基于Boost Log 开发的本地日志模块
- boost log 简介:
boost log模块简介地址
boost log日志库由3个层次构成:收集日志数据,处理日志数据,以及在收集日志数据和处理日志数据2个层次之间的一个中间层。
boost log 的设计主要由日志器( Logger )、日志核心( Logging core )、 Sink 前后端( frontend,backend )组成。
Logger负责搜集日志文本初始化日志环境,Logging core负责处理日志数据(例如全局过滤、将日志记录传递给 Sink ),
Sink 前端分为同步、异步以及不考虑线程同步问题的版本,它们负责将日志记录传递给 Sink 后端处理。
Sink 后端负责把日志记录格式化并输出到不同的介质中(例如日志文件、报警以及统计源中)
- 代码结构:
SEED_BP--|----BP项目总目录
|
|---include----|
| |---seed_log_api.h----->对外接口头文件,用于调用
|
|
|---src--------|
| |----SEED_LOG----|----seed_log_api.cpp------->对外接口头文件,用于调用
| | |----seed_log_local.h-------->本地日志具体实现(boost log 具体实现头文件)
| | |----seed_log_local.cpp----->本地日志具体实现(boost log 具体实现cpp文件)
| |
| |---SEED_EXAMPLE----|
| |----seed_log_example.h---库使用头文件
| |----seed_log_example.cpp---库使用实例文件
| |----seed_example_main.cpp---主函数
|----mk--------|
| |----SEED_LOG-----|-----CMakefile----->编译文件(支持windows linux 跨平台编译)
| |-----SEED_LOG.sln---->windows 项目配置 (vs2017)
| |-----*.vcxproj---->windows 项目配置 (vs2017)
|
|---exlib------|----linux--->boost linux库
| |----windows--->boost windows库
| |----windows--->boost windows库
|
|--------->README--工程说明文件
UML 类图:
3.SEED_LOG库说明:
SEED_LOG_LOCAL 是基于Boost.Log的模块的本地日志模块,默认配置每天自动生成日志模块,并且有保留天数的配置,日志超过配置开始自动清理。该模块主要依赖 Boost.Log 和Boost.Thread两大模块,并且可以支持 windows 和 linux 跨平台使用。
github 地址:待更新
4.代码说明:
/*
@filename: seed_engine_def.h
@comment:
屏蔽系统和编译器差异,
公共宏定义,
注: 1.支持linux和windows 系统
2.cpu只支持32bit以上的架构
*/
#ifndef SEED_EINGIE_DEF_HPP
#define SEED_EINGIE_DEF_HPP
#ifdef WIN32
#ifdef _WINDLL
#define SEED_API __declspec(dllexport)
#else
#define SEED_API __declspec(dllimport)
#endif
#else
#define SEED_API
#endif
#ifndef INTERFACE
#define INTERFACE struct
#endif
#ifndef IN
#define IN
#endif
#ifndef OUT
#define OUT
#endif
#ifndef INOUT
#define INOUT
#endif
#ifndef SD_CHAR
#define SD_CHAR char
#endif
#ifndef SD_VOID
#define SD_VOID void
#endif
#ifndef SD_ULONG8
#define SD_ULONG8 unsigned char
#endif
#ifndef SD_LONG8
#define SD_LONG8 char
#endif
#ifndef SD_ULONG16
#define SD_ULONG16 unsigned short
#endif
#ifndef SD_LONG16
#define SD_LONG16 short
#endif
#ifndef SD_ULONG32
#define SD_ULONG32 unsigned int
#endif
#ifndef SD_LONG32
#define SD_LONG32 int
#endif
#ifndef SD_ULONG64
#define SD_ULONG64 unsigned long long
#endif
#ifndef SD_LONG64
#define SD_LONG64 long long
#endif
#ifndef SD_BOOL
#define SD_BOOL int
#endif
#ifndef SD_FALSE
#define SD_FALSE 0
#endif
#ifndef SD_TRUE
#define SD_TRUE 1
#endif
#ifdef WIN32
#define BP_Sleep(seconds) Sleep((seconds)*1000)
#define BP_MSleep(seconds) Sleep((seconds))
#else
#include <unistd.h>
#define BP_Sleep(seconds) sleep((seconds))
#define BP_MSleep(seconds) usleep((seconds)*1000)
#endif
#ifndef WIN32
#define BOOST_ALL_DYN_LINK
#endif
#ifdef WIN32
#define MFILENAME(x) strrchr(x,'\\')?strrchr(x,'\\')+1:x
#else
#define MFILENAME(x) strrchr(x,'/')?strrchr(x,'/')+1:x
#endif
#ifdef WIN32
#pragma warning(disable:4996)
#endif
enum BASE_ERROR_CODE
{
ERROR_BASE_SUCCEED=0,
ERROR_BASE_FALT,
ERROR_BASE_INVALID
};
#endif
#ifndef SEED_LOG_H
#define SEED_LOG_H
/*
@filename: seed_log_api.h
@comment:
head file of Log-interface for public
*/
#include <seed_engine_def.hpp>
#include <cstdarg>
#include <map>
#include <vector>
#include <boost/shared_ptr.hpp>
// bytes of log in one line
#ifndef MAXLOGLEN
#define MAXLOGLEN 2048
#endif
enum SEED_LOG_LEVEL
{
SD_LOG_DEBUG=0,
SD_LOG_INFO,
SD_LOG_WARNING,
SD_LOG_ERROR,
SD_LOG_KEY,
SD_LOG_STOP
};
/*
@class name : SeedLogSimpleManager
*/
struct SeedLogCfg
{
std::string m_path;
std::string filename;
SD_BOOL m_isDebug = SD_TRUE;
SD_ULONG32 m_LogId = 0;
//SD_ULONG32 m_ulFileSize = 10*1024*1024;
//default 10M
};
INTERFACE SEED_API SeedLogApi
{
/*
@function name : init
*/
static SD_ULONG32 LogSystemInit(SD_BOOL isconsole);
/*
@function name : init
*/
virtual SD_ULONG32 LogInit(IN SeedLogCfg cfgs) = 0;
/*
@function name : GetLogId
*/
virtual SD_ULONG32 GetLogId() = 0;
/*
@function name : GetLogId
*/
virtual SD_BOOL GetDebug() = 0;
/*
@function name : log
*/
virtual SD_VOID log(IN SD_ULONG32 logid, IN SD_BOOL debug, IN int level, IN const char* filename, IN SD_ULONG32 line, IN std::string& message) = 0;
/*
@function name : log
*/
virtual SD_VOID log(IN SD_ULONG32 logid, IN SD_BOOL debug, IN int level, IN const char* filename, IN SD_ULONG32 line, IN const char* message) = 0;
};
/*
@class name : Mutilple log-class
*/
class SEED_API SeedLogMutilpleManager
{
public:
static SeedLogMutilpleManager& instance();
/*
@function name : init
*/
static SD_ULONG32 LogSystemInit(SD_BOOL isconsole);
/*
@function name : log init;
*/
SD_ULONG32 SeedLogMutilpleInit(IN std::vector<SeedLogCfg>& configs);
/*
@function name : log_write
*/
static SD_VOID log_write(IN SD_ULONG32 logid, IN SD_ULONG32 level, const char* filename, SD_ULONG32 lineid, const char* fmt, ...);
/*
@function name : callback_write
*/
static SD_LONG32 callback_write(IN SD_ULONG32 logid, IN SD_ULONG32 level, const char* filename, SD_ULONG32 lineid, const char* fmt, va_list va);
private:
/*
@function name : log_write
*/
SeedLogMutilpleManager(){}
private:
static SeedLogMutilpleManager * s_instance;
std::map<SD_ULONG32, SeedLogApi*> m_logs;
SD_ULONG32 m_adapter_id = 0;
};
/*
@class name : simple model of log-model
*/
class SEED_API SeedLogSimpleManager
{
public:
static SeedLogSimpleManager& instance();
/*
@function name : init
*/
static SD_ULONG32 LogSystemInit(SD_BOOL isconsole);
/*
@function name : log init;
*/
SD_ULONG32 SeedLogSimpleInit(IN SeedLogCfg config);
/*
@function name : log_write
*/
static SD_VOID log_write(IN SD_ULONG32 level, const char* filename, SD_ULONG32 lineid, const char* fmt, ...);
/*
@function name : callback_write
*/
static SD_LONG32 callback_write(IN SD_ULONG32 level, const char* filename, SD_ULONG32 lineid, const char* fmt, va_list va);
private:
/*
@function name : log_write
*/
SeedLogSimpleManager(){}
private:
static SeedLogSimpleManager * s_instance;
SeedLogApi* m_log = nullptr;
SeedLogCfg m_config;
};
/*
@function name : LOG宏定义,方便调用
*/
#define SEED_LOG0(level,message)\
if(1){SeedLogSimpleManager::log_write(level,__FILE__,__LINE__,message);}
#define SEED_LOG1(level,fmt,v1)\
if(1){SeedLogSimpleManager::log_write(level,__FILE__,__LINE__,fmt,v1);}
#define SEED_LOG2(level,message,v1,v2)\
if(1){SeedLogSimpleManager::log_write(level,__FILE__,__LINE__,message,v1,v2);}
#define SEED_LOG3(level,message,v1,v2,v3)\
if(1){SeedLogSimpleManager::log_write(level,__FILE__,__LINE__,message,v1,v2,v3);}
#define SEED_LOG4(level,message,v1,v2,v3,v4)\
if(1){SeedLogSimpleManager::log_write(level,__FILE__,__LINE__,message,v1,v2,v3,v4);}
#endif
/*
@filename: seed_log_api.cpp
@comment:
Log interface
*/
#include <seed_log_api.h>
#include "seed_log_local.h"
#include <boost/foreach.hpp>
#include <iostream>
SeedLogSimpleManager * SeedLogSimpleManager::s_instance = nullptr;
SD_VOID SeedLogSimpleManager::log_write(IN SD_ULONG32 level, const char* filename, SD_ULONG32 lineid, const char* fmt, ...)
{
SD_ULONG32 ulret = ERROR_BASE_SUCCEED;
SeedLogSimpleManager& ref_instance = SeedLogSimpleManager::instance();
if (ref_instance.m_log)
{
char message[MAXLOGLEN + 1] = { 0 };
va_list vArgList;
va_start(vArgList, fmt);
vsnprintf(message, MAXLOGLEN, fmt, vArgList);
va_end(vArgList);
ref_instance.m_log->log(ref_instance.m_config.m_LogId, ref_instance.m_config.m_isDebug, level, MFILENAME(filename), lineid, message);
}
return ;
}
SD_LONG32 SeedLogSimpleManager::callback_write(IN SD_ULONG32 level, const char* filename, SD_ULONG32 lineid, const char* fmt, va_list va)
{
SD_ULONG32 ulret = ERROR_BASE_SUCCEED;
SeedLogSimpleManager& ref_instance = SeedLogSimpleManager::instance();
if (ref_instance.m_log)
{
char message[MAXLOGLEN + 1] = { 0 };
vsnprintf(message, MAXLOGLEN, fmt, va);
ref_instance.m_log->log(ref_instance.m_config.m_LogId, ref_instance.m_config.m_isDebug, level, MFILENAME(filename), lineid, message);
return ERROR_BASE_SUCCEED;
}
return ulret;
}
SD_ULONG32 SeedLogSimpleManager::SeedLogSimpleInit(IN SeedLogCfg config)
{
SD_ULONG32 ulret = ERROR_BASE_SUCCEED;
m_log = new SeedLogLocal();
ulret = m_log->LogInit(config);
m_config = config;
m_config.m_LogId = m_log->GetLogId();
return ulret;
}
SeedLogSimpleManager& SeedLogSimpleManager::instance()
{
if (s_instance == nullptr)
{
s_instance = new SeedLogSimpleManager;
}
return *s_instance;
}
SD_ULONG32 SeedLogSimpleManager::LogSystemInit(SD_BOOL isconsole)
{
try{
return SeedLogApi::LogSystemInit(isconsole);
}
catch (std::exception & e){
std::cout << e.what() << std::endl;
return ERROR_BASE_FALT;
}
}
SD_ULONG32 SeedLogApi::LogSystemInit(SD_BOOL isconsole)
{
try{
return SeedLogApi::LogSystemInit(isconsole);
}
catch (std::exception & e){
std::cout << e.what() << std::endl;
return ERROR_BASE_FALT;
}
}
SeedLogMutilpleManager * SeedLogMutilpleManager::s_instance=nullptr;
SeedLogMutilpleManager& SeedLogMutilpleManager::instance()
{
if (s_instance == nullptr)
{
s_instance = new SeedLogMutilpleManager;
}
return *s_instance;
}
SD_ULONG32 SeedLogMutilpleManager::LogSystemInit(SD_BOOL isconsole)
{
return SeedLogApi::LogSystemInit(isconsole);
}
SD_ULONG32 SeedLogMutilpleManager::SeedLogMutilpleInit(IN std::vector<SeedLogCfg>& configs)
{
SD_ULONG32 ulret = ERROR_BASE_SUCCEED;
try{
BOOST_FOREACH(auto& id, configs){
SeedLogLocal* ptr = new SeedLogLocal();
id.m_LogId = ++m_adapter_id;
ulret = ptr->LogInit(id);
if (ulret != ERROR_BASE_SUCCEED){
return ulret;
}
}
}
catch (std::exception & e){
return ERROR_BASE_FALT;
}
return ulret;
}
SD_VOID SeedLogMutilpleManager::log_write(IN SD_ULONG32 logid,IN SD_ULONG32 level, const char* filename, SD_ULONG32 lineid, const char* fmt, ...)
{
SD_ULONG32 ulret = ERROR_BASE_SUCCEED;
SeedLogMutilpleManager& ref_instance = SeedLogMutilpleManager::instance();
if (ref_instance.m_logs.find(logid) != ref_instance.m_logs.end() &&
ref_instance.m_logs[logid] != nullptr)
{
char message[MAXLOGLEN + 1] = { 0 };
va_list vArgList;
va_start(vArgList, fmt);
vsnprintf(message, MAXLOGLEN, fmt, vArgList);
va_end(vArgList);
ref_instance.m_logs[logid]->log(ref_instance.m_logs[logid]->GetLogId(), ref_instance.m_logs[logid]->GetDebug(), level, MFILENAME(filename), lineid, message);
}
return;
}
SD_LONG32 SeedLogMutilpleManager::callback_write(IN SD_ULONG32 logid,IN SD_ULONG32 level, const char* filename, SD_ULONG32 lineid, const char* fmt, va_list va)
{
SD_ULONG32 ulret = ERROR_BASE_SUCCEED;
SeedLogMutilpleManager& ref_instance = SeedLogMutilpleManager::instance();
if (ref_instance.m_logs.find(logid) != ref_instance.m_logs.end() &&
ref_instance.m_logs[logid] != nullptr)
{
char message[MAXLOGLEN + 1] = { 0 };
vsnprintf(message, MAXLOGLEN, fmt, va);
ref_instance.m_logs[logid]->log(ref_instance.m_logs[logid]->GetLogId(), ref_instance.m_logs[logid]->GetDebug(), level, MFILENAME(filename), lineid, message);
return ERROR_BASE_SUCCEED;
}
return ulret;
}
/*
@filename: seed_log_local.h
@comment:
head file of specific code in model of log
*/
#ifndef SEED_LOG_LOCAL_H
#define SEED_LOG_LOCAL_H
#include <seed_engine_def.hpp>
#include <seed_log_api.h>
#include <vector>
template< typename CharT, typename TraitsT >
inline std::basic_ostream< CharT, TraitsT >& operator<< (
std::basic_ostream< CharT, TraitsT >& strm, SEED_LOG_LEVEL lvl)
{
static const char* const str[] =
{
"debug",
"Info",
"Warning",
"Error",
"Key"
};
if (static_cast<std::size_t>(lvl) < (sizeof(str) / sizeof(*str)))
strm << str[lvl];
else
strm << static_cast<int>(lvl);
return strm;
}
class SeedLogLocal: public SeedLogApi
{
public:
/*
@function name : init
*/
static SD_ULONG32 InitLogSystem(SD_BOOL isconsole);
/*
@function name : LogInit
*/
SD_ULONG32 LogInit(IN SeedLogCfg cfgs) override;
/*
@function name : GetLogId
*/
SD_ULONG32 GetLogId() override;
/*
@function name : GetLogId
*/
SD_BOOL GetDebug() override;
/*
@function name : log
*/
SD_VOID log(IN SD_ULONG32 logid, IN SD_BOOL debug, IN int level, IN const char* filename, IN SD_ULONG32 line, IN std::string& message) override;
/*
@function name : log
*/
SD_VOID log(IN SD_ULONG32 logid, IN SD_BOOL debug, IN int level, IN const char* filename, IN SD_ULONG32 line, IN const char* message) override;
private:
/*
@function name : add_log_file
*/
SD_ULONG32 add_log_file(IN SeedLogCfg cfg);
private:
SeedLogCfg m_cfg;
};
#endif
/*
@filename: seed_log_local.cpp
@comment:
specific code in log model
*/
#include "seed_log_local.h"
#include <exception>
#include <boost/shared_ptr.hpp>
#include <boost/date_time/posix_time/posix_time.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/attributes/timer.hpp>
#include <boost/log/attributes/named_scope.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/attributes/named_scope.hpp>
#include <boost/log/attributes/current_thread_id.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup/from_stream.hpp>
#include <boost/log/utility/setup/formatter_parser.hpp>
#include <boost/log/utility/formatting_ostream.hpp>
#include <boost/log/utility/setup/filter_parser.hpp>
#include <boost/filesystem.hpp>
#include <boost/log/sources/logger.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;
SD_ULONG32 SeedLogLocal::InitLogSystem(SD_BOOL isconsole)
{
SD_ULONG32 ulret = ERROR_BASE_SUCCEED;
if (isconsole == SD_TRUE)
{
logging::add_console_log(std::clog,
keywords::format =
(
expr::stream
<< expr::attr< unsigned int >("LineID")
<< " [" << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S.%f") << "]"
<< " [" << expr::attr< SEED_LOG_LEVEL >("Severity") << "]"
<< " [" << expr::attr< boost::log::aux::thread::id >("ThreadID") << "]"
<< expr::smessage
)
);
}
// LineID是一个计数器,先创建一个初始值为1的计数器.
boost::shared_ptr< logging::attribute > pCounter(new attrs::counter< unsigned int >(1));
// 将它加入到全局属性中,如果要求将不同的内容输出到不同的日志文件中去,这里设置为全局属性可能就是不太合适了.
logging::core::get()->add_global_attribute("LineID", *pCounter);
// 下面是设置TimeStamp属性
boost::shared_ptr< logging::attribute > pTimeStamp(new attrs::local_clock());
logging::core::get()->add_global_attribute("TimeStamp", *pTimeStamp);
boost::shared_ptr< logging::attribute > pNamedScope(new attrs::named_scope());
logging::core::get()->add_thread_attribute("Scope", *pNamedScope);
logging::add_common_attributes();
//pSink->locked_backend()->auto_flush(true);
return ulret;
}
SD_ULONG32 SeedLogLocal::LogInit(IN SeedLogCfg cfg)
{
SD_ULONG32 ulret = ERROR_BASE_SUCCEED;
try{
ulret = this->add_log_file(cfg);
if (ulret != ERROR_BASE_SUCCEED){
return ulret;
}
m_cfg = cfg;
}
catch (std::exception& e){
//fail init exception
std::cout << e.what() << std::endl;
}
return ulret;
}
SD_ULONG32 SeedLogLocal::add_log_file(IN SeedLogCfg cfg)
{
SD_ULONG32 ulret = ERROR_BASE_SUCCEED;
//每天0点更新日志文件
boost::shared_ptr<sinks::synchronous_sink< boost::log::sinks::text_file_backend> > pSink = logging::add_file_log
(
keywords::open_mode = std::ios::app,
keywords::filter = (expr::attr< SEED_LOG_LEVEL >("Severity") >= SD_LOG_INFO && expr::attr< SD_ULONG32 >("Channel") == cfg.m_LogId),
keywords::file_name = cfg.m_path + "/" + cfg.filename + "_%Y%m%d.log",
keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
keywords::format =
(
expr::stream
<< "[" << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S.%f") << "]"
<< " [" << expr::attr< SEED_LOG_LEVEL >("Severity") << "]"
<< " [" << expr::attr< boost::log::aux::thread::id >("ThreadID") << "]"
<< expr::smessage
)
);
logging::core::get()->add_sink(pSink);
pSink->locked_backend()->auto_flush(true);
if (cfg.m_isDebug == SD_TRUE)
{
boost::shared_ptr<sinks::synchronous_sink< boost::log::sinks::text_file_backend> > pSink2 = logging::add_file_log
(
keywords::open_mode = std::ios::app,
keywords::filter = (expr::attr< SEED_LOG_LEVEL >("Severity") <= SD_LOG_DEBUG && expr::attr< SD_ULONG32 >("Channel") == cfg.m_LogId),
keywords::file_name = cfg.m_path + "/" + cfg.filename + "_%Y%m%d.log.debug",
keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
keywords::format =
(
expr::stream
<< "[" << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S.%f") << "]"
<< " [" << expr::attr< SEED_LOG_LEVEL >("Severity") << "]"
<< " [" << expr::attr< boost::log::aux::thread::id >("ThreadID") << "]"
<< expr::smessage
)
);
pSink2->locked_backend()->auto_flush(true);
logging::core::get()->add_sink(pSink2);
//keywords::filter = expr::attr< ULONG_32 >("Channel") == ID,
}
// LineID是一个计数器,先创建一个初始值为1的计数器.
boost::shared_ptr< logging::attribute > pCounter(new attrs::counter< unsigned int >(1));
// 将它加入到全局属性中,如果要求将不同的内容输出到不同的日志文件中去,这里设置为全局属性可能就是不太合适了.
logging::core::get()->add_global_attribute("LineID", *pCounter);
// 下面是设置TimeStamp属性
boost::shared_ptr< logging::attribute > pTimeStamp(new attrs::local_clock());
logging::core::get()->add_global_attribute("TimeStamp", *pTimeStamp);
boost::shared_ptr< logging::attribute > pNamedScope(new attrs::named_scope());
logging::core::get()->add_thread_attribute("Scope", *pNamedScope);
logging::add_common_attributes();
return ulret;
}
SD_VOID SeedLogLocal::log(IN SD_ULONG32 logid,IN SD_BOOL debug, IN int level, IN const char* filename, IN SD_ULONG32 line, IN std::string& message)
{
if (level <= SD_LOG_DEBUG && debug == SD_FALSE){
return;
}
//write_lock wlock(read_write_mutex);
src::severity_channel_logger_mt< SEED_LOG_LEVEL, SD_ULONG32> slg(keywords::channel = logid);
switch (level)
{
case SD_LOG_DEBUG:
BOOST_LOG_SEV(slg, SD_LOG_DEBUG) << " [" << filename << ":" << line << "]" << message;
break;
case SD_LOG_KEY:
BOOST_LOG_SEV(slg, SD_LOG_KEY) << " [" << filename << ":" << line << "]" << message;
break;
case SD_LOG_INFO:
BOOST_LOG_SEV(slg, SD_LOG_INFO) << " [" << filename << ":" << line << "]" << message;
break;
case SD_LOG_WARNING:
BOOST_LOG_SEV(slg, SD_LOG_WARNING) << " [" << filename << ":" << line << "]" << message;
break;
case SD_LOG_ERROR:
BOOST_LOG_SEV(slg, SD_LOG_ERROR) << " [" << filename << ":" << line << "]" << message;
break;
default:
BOOST_LOG_SEV(slg, SD_LOG_INFO) << " [" << filename << ":" << line << "]" << message;
break;
}
}
SD_VOID SeedLogLocal::log(IN SD_ULONG32 logid ,IN SD_BOOL debug, IN int level, IN const char* filename, IN SD_ULONG32 line, IN const char* message)
{
if (level <= SD_LOG_DEBUG && debug == SD_FALSE){
return;
}
//write_lock wlock(read_write_mutex);
src::severity_channel_logger_mt< SEED_LOG_LEVEL, SD_ULONG32> slg(keywords::channel = logid);
switch (level)
{
case SD_LOG_DEBUG:
BOOST_LOG_SEV(slg, SD_LOG_DEBUG) << " [" << filename << ":" << line << "]" << message;
break;
case SD_LOG_KEY:
BOOST_LOG_SEV(slg, SD_LOG_KEY) << " [" << filename << ":" << line << "]" << message;
break;
case SD_LOG_INFO:
BOOST_LOG_SEV(slg, SD_LOG_INFO) << " [" << filename << ":" << line << "]" << message;
break;
case SD_LOG_WARNING:
BOOST_LOG_SEV(slg, SD_LOG_WARNING) << " [" << filename << ":" << line << "]" << message;
break;
case SD_LOG_ERROR:
BOOST_LOG_SEV(slg, SD_LOG_ERROR) << " [" << filename << ":" << line << "]" << message;
break;
default:
BOOST_LOG_SEV(slg, SD_LOG_INFO) << " [" << filename << ":" << line << "]" << message;
break;
}
}
SD_ULONG32 SeedLogLocal::GetLogId()
{
return m_cfg.m_LogId;
}
SD_BOOL SeedLogLocal::GetDebug()
{
return m_cfg.m_isDebug;
}
/*
@filename: seed_log_example.h
@comment:
log example
*/
#ifndef SEED_LOG_EXAMPLE_H
#define SEED_LOG_EXAMPLE_H
#include <seed_log_api.h>
class seed_log_example
{
public:
static SD_ULONG32 seed_log_test();
};
#endif
/*
@filename: seed_log_example.cpp
@comment:
log example
*/
#include "seed_log_example.h"
#include <iostream>
#include <string>
//懒汉模式
static SeedLogSimpleManager& s_instance = SeedLogSimpleManager::instance();
/*
@function name : LOG宏定义,方便调用
*/
#define SEED_EXAMPLE_LOG0(level,message)\
if(1){SeedLogSimpleManager::log_write(level,__FILE__,__LINE__,message);}
#define SEED_EXAMPLE_LOG1(level,fmt,v1)\
if(1){SeedLogSimpleManager::log_write(level,__FILE__,__LINE__,fmt,v1);}
#define SEED_EXAMPLE_LOG2(level,message,v1,v2)\
if(1){SeedLogSimpleManager::log_write(level,__FILE__,__LINE__,message,v1,v2);}
#define SEED_EXAMPLE_LOG3(level,message,v1,v2,v3)\
if(1){SeedLogSimpleManager::log_write(level,__FILE__,__LINE__,message,v1,v2,v3);}
#define SEED_EXAMPLE_LOG4(level,message,v1,v2,v3,v4)\
if(1){SeedLogSimpleManager::log_write(level,__FILE__,__LINE__,message,v1,v2,v3,v4);}
SD_ULONG32 seed_log_example::seed_log_test()
{
try
{
//初始化日志系统(global配置)
SeedLogSimpleManager::LogSystemInit(SD_TRUE);
SeedLogCfg config;
config.m_path = std::string("../../log");
config.m_filename = std::string("example");
config.m_isDebug = SD_TRUE;
config.m_LogId = 0;
s_instance.SeedLogSimpleInit(config);
//测试代码:
SEED_EXAMPLE_LOG1(SD_LOG_INFO, "log init path [%s]", config.m_path.c_str());
SEED_EXAMPLE_LOG2(SD_LOG_INFO, "log init path [%s] filename [%s]", config.m_path.c_str(), config.m_filename.c_str());
SEED_EXAMPLE_LOG2(SD_LOG_INFO, "log init path debug [%u] log id[%u]", config.m_isDebug, config.m_LogId);
//....
}
catch (std::exception &e){
std::cout << e.what() << std::endl;
}
return ERROR_BASE_SUCCEED;
}
/*
@filename: seed_example_main.cpp
@comment:
main function
*/
#include "seed_log_example.h"
int main()
{
seed_log_example::seed_log_test();
return 0;
}