Adbase
Adinf C++ base library V2
adbase::Logging

adbase 日志处理工具库(使用方法见该模块的描述信息) More...

Namespaces

 adbase::detail
 

Classes

class  adbase::AsyncLogging
 
class  adbase::LogFile
 
class  adbase::Logger
 
class  adbase::LogStream
 
class  adbase::Fmt
 

Macros

#define LOG_TRACE
 
#define LOG_DEBUG
 
#define LOG_INFO
 
#define LOG_WARN   adbase::Logger(__FILE__, __LINE__, adbase::Logger::WARN).stream()
 
#define LOG_ERROR   adbase::Logger(__FILE__, __LINE__, adbase::Logger::ERROR).stream()
 
#define LOG_FATAL   adbase::Logger(__FILE__, __LINE__, adbase::Logger::FATAL).stream()
 
#define LOG_SYSERR   adbase::Logger(__FILE__, __LINE__, false).stream()
 
#define LOG_SYSFATAL   adbase::Logger(__FILE__, __LINE__, true).stream()
 
#define CHECK_NOTNULL(val)   ::adbase::CheckNotNull(__FILE__, __LINE__, "'" #val "' Must be non NULL", (val))
 

Functions

const char * adbase::strerror_tl (int saveErrno)
 
template<typename T >
T * adbase::CheckNotNull (Logger::SourceFile file, int line, const char *names, T *ptr)
 
LogStream & adbase::operator<< (LogStream &s, const Fmt &fmt)
 
static LogLevel adbase::Logger::logLevel ()
 

Variables

Logger::LogLevel adbase::gLogLevel = initLogLevel()
 

Detailed Description

adbase 日志处理工具库(使用方法见该模块的描述信息)

使用方法
Note
请勿直接引用对应头文件,adbase::Logging 模块统一引用<adbase/Logging.hpp> 文件
Example
.....
LOG_TRACE << "trace test";
LOG_DEBUG << "debug test";
LOG_INFO << "info test";
LOG_ERROR << 45;
.....
日志写入

日志写入是分日志等级通过宏替换来做,分为 TRACE, DEBUG, INFO, WARN, ERROR, FATAL, SYSERR, SYSFATAL

通过 LOG_* 方式写入

默认是 INFO 级别,如果需要修改系统 LOG 级别可以通过两种方式修改

动态修改日志等级
日志落地

日志落地通过 setOutput回调函数来决定落地方式,默认是写入到 stdout 中, 日志库还提供了一种异步写入到文件的落地方式

adbase::AsyncLogging* glogger = nullptr;
void asyncLogger(const char* msg, int len) {
if (glogger != nullptr) {
glogger->append(msg, len);
}
}
static void killSignalMaster(const int sig) {
(void)(sig);
LOG_ERROR << sig;
if (glogger != nullptr) {
LOG_INFO << "Stop...";
delete glogger;
glogger = nullptr;
}
kill(0, SIGTERM);
exit(0);
}
int main() {
signal (SIGINT, killSignalMaster);
signal (SIGKILL, killSignalMaster);
signal (SIGQUIT, killSignalMaster);
signal (SIGTERM, killSignalMaster);
signal (SIGHUP, killSignalMaster);
signal (SIGSEGV, killSignalMaster);
adbase::AsyncLogging*logger = new adbase::AsyncLogging("./test", 5 * 1024 * 1024);
glogger = logger;
glogger->start();
// something process...
while(true) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
return 0;
}
Note
异步落地文件在进程终止的时候一定要保证落地对象正常销毁,保证所有的写入日志都落地到磁盘,一般的做法是在捕获信号回调函数中 delete 掉落地对象,保证落地对象在析构的时候正常调用 stop 函数。

Macro Definition Documentation

#define CHECK_NOTNULL (   val)    ::adbase::CheckNotNull(__FILE__, __LINE__, "'" #val "' Must be non NULL", (val))

Definition at line 122 of file Logging.hpp.

#define LOG_SYSFATAL   adbase::Logger(__FILE__, __LINE__, true).stream()
#define LOG_WARN   adbase::Logger(__FILE__, __LINE__, adbase::Logger::WARN).stream()

Function Documentation

template<typename T >
T* adbase::CheckNotNull ( Logger::SourceFile  file,
int  line,
const char *  names,
T ptr 
)

Definition at line 126 of file Logging.hpp.

References adbase::Logger::FATAL, and adbase::Logger::Logger().

Logger::LogLevel adbase::Logger::logLevel ( )
inlinestatic

Definition at line 104 of file Logging.hpp.

References adbase::gLogLevel.

Referenced by adbase::Logger::stream().

LogStream& adbase::operator<< ( LogStream s,
const Fmt fmt 
)
inline
const char * adbase::strerror_tl ( int  saveErrno)

Definition at line 17 of file Logging.cpp.

Referenced by adbase::Connector::handleError(), and adbase::Connector::handleWrite().

Variable Documentation

Logger::LogLevel adbase::gLogLevel = initLogLevel()

Definition at line 31 of file Logging.cpp.

Referenced by adbase::Logger::logLevel().