-
-
[原创]发些个人库之 log 信息输出
-
发表于: 2013-10-7 12:54 4262
-
鉴于某某发了一份Logger代码,我也来发一份自己的logger简单版本(需要功能自己扩展,以下代码我只用到了这些功能),写的很烂,还望被批斗。
用法:
tsLogger.h
tsLogger.cc
用法:
base::tsLogger::run().Format(base::tsLogger::DEBUG,"%s",var)
tsLogger.h
/* * tsLogger.h * * Created on: Oct 4, 2013 * Author: vscen */ #ifndef TSLOGGER_H_ #define TSLOGGER_H_ #include "xxxxxx.h" namespace base { class tsLogger { static pthread_mutex_t lock_; std::ofstream stream_; public: enum Level{DEBUG,INFO,ERROR}; public: tsLogger(); ~tsLogger(); static tsLogger& Run(); inline static void lock(); inline static void unlock(); void Format(Level level,const char* format, ...); private: void FmtLevel(Level level); }; } #endif /* TSLOGGER_H_ */
tsLogger.cc
/* * tsLogger.cc * * Created on: Oct 4, 2013 * Author: vscen */ #include "tsLogger.h" namespace base { pthread_mutex_t tsLogger::lock_ = PTHREAD_MUTEX_INITIALIZER; tsLogger instance_; tsLogger::tsLogger() { tsLogger::stream_.open("linuxvw.log", std::ofstream::out | std::ofstream::app); pthread_mutex_init(&lock_, NULL); } tsLogger::~tsLogger() { if (tsLogger::stream_.is_open()) { tsLogger::stream_.close(); } pthread_mutex_destroy(&lock_); } tsLogger& tsLogger::Run() { return instance_; } inline void tsLogger::lock() { pthread_mutex_lock(&lock_); } inline void tsLogger::unlock() { pthread_mutex_unlock(&lock_); } void tsLogger::Format(Level level, const char* format, ...) { va_list argList; char cbuffer[kMaxFmtMessage] = { 0 }; tsLogger::lock(); va_start(argList, format); vsnprintf(cbuffer, kMaxFmtMessage, format, argList); va_end(argList); tsLogger::FmtLevel(level); tsLogger::stream_ << cbuffer << std::endl; tsLogger::unlock(); } void tsLogger::FmtLevel(Level level) { switch(level) { case DEBUG: tsLogger::stream_ << "[DEBUG]:"; break; case INFO: tsLogger::stream_ << "[INFO]:"; break; case ERROR: tsLogger::stream_ << "[ERROR]:"; break; default: break; } } }
赞赏记录
参与人
雪币
留言
时间
心游尘世外
为你点赞~
2024-5-31 06:37
QinBeast
为你点赞~
2024-5-31 06:28
飘零丶
为你点赞~
2024-4-26 01:00
shinratensei
为你点赞~
2024-3-14 00:35
一笑人间万事
为你点赞~
2023-3-5 04:17
赞赏
他的文章
- [原创]再见,看雪论坛,这一次是永远! 17487
- [求助]更改文章标题怎么还要扣KX? 11644
- [分享]根据国外的开源B树写了一份C++版本。 8462
- [原创]去掉标题免得被喷...... 7183
- [讨论]以前搞360,现在搞百度? 4686
看原图
赞赏
雪币:
留言: