c++实现写日志功能
来源:互联网 发布:阿里云备案订单二维码 编辑:程序博客网 时间:2024/06/03 23:42
编程实例:
#include "Mutex.h"#include "CTime.h"#include <fcntl.h>#include <iostream>#include <stdarg.h>#include <fstream>#define LOGPATH "./common.log"#define BUFSIZE 2048class CLog{public:static CLog* getInstance();void Init(const char* path, int level);void Error(char *pFmt, ...); void Info(char *pFmt, ...); void Debug(char *pFmt, ...); void Warn(char *pFmt, ...); void Log(int level, const char * loginfo);private:CLog(){}static CLog *m_log;CLog(const CLog& clog);CLog& operator=(const CLog& clog);Mutex m_mutex;int m_logLevel;const char* m_logPath;enum { LOG_ERROR = 0, LOG_INFO, LOG_WARN, LOG_DEBUG};};CLog* CLog::m_log = NULL;CLog* CLog::getInstance(){if(m_log == NULL){m_log = new CLog();}return m_log;}void CLog::Init(const char* path, int level){m_logPath = path;m_logLevel = level;}void CLog::Log(int level, const char * loginfo){const char *infos[4] = {"ERROR: ", "INFO: ", "WARN : ", "DEBUG: "};m_mutex.Lock();CTime time;std::ofstream fout(m_logPath, std::ios::app|std::ios::ate);if(fout){fout<<"["<<time<<"]"<<infos[level]<<loginfo<<std::endl;if(fout.tellp() > 2*1024*1024){fout.close();std::string logname = m_logPath + time.getTime(); //back the log filerename(m_logPath, logname.c_str());}}else{std::cout << time << infos[0] <<"open file failed \n"<<strerror(errno) << std::endl;}if(fout) fout.close();m_mutex.UnLock();}void CLog::Error(char *pFmt, ...){va_list valist;char Buf[BUFSIZE]={'\0'};va_start(valist, pFmt);vsnprintf(Buf, sizeof(Buf)-1, pFmt, valist); va_end(valist); Log(LOG_ERROR, Buf);}void CLog::Info(char *pFmt, ...){va_list valist;char Buf[BUFSIZE]={'\0'};va_start(valist, pFmt);vsnprintf(Buf, sizeof(Buf)-1, pFmt, valist); va_end(valist); Log(LOG_INFO, Buf);}void CLog::Debug(char *pFmt, ...){va_list valist;char Buf[BUFSIZE]={'\0'};va_start(valist, pFmt);vsnprintf(Buf, sizeof(Buf)-1, pFmt, valist); va_end(valist); Log(LOG_DEBUG, Buf);}void CLog::Warn(char *pFmt, ...){va_list valist;char Buf[BUFSIZE]={'\0'};va_start(valist, pFmt);vsnprintf(Buf, sizeof(Buf)-1, pFmt, valist); va_end(valist); Log(LOG_WARN, Buf);}
Mutext.h
#include <cstdlib> #include <pthread.h> #include <errno.h>#include <unistd.h>#include <cstdio>#include <string.h>class Mutex { public: Mutex(); virtual ~Mutex(); void Lock(); void UnLock(); private: friend class CondVar; pthread_mutex_t m_mutex; Mutex(const Mutex&); void operator=(const Mutex&); }; class CondVar { public: explicit CondVar(Mutex* mu); virtual ~CondVar(); void Wait(); void Signal(); void SignalAll(); private: pthread_cond_t m_cond; Mutex *m_mu; }; class CASLock{public: CASLock(void* lock); virtual ~CASLock(); void Lock();void UnLock();bool TryLock();private:void* m_lock;};static void MutexErr(const char* err, int res) { if(res != 0) { fprintf(stderr, "pthread %s: %s\n", err, strerror(res)); return; } } Mutex::Mutex() { MutexErr("init mutex", pthread_mutex_init(&m_mutex, NULL)); } Mutex::~Mutex() { MutexErr("destroy mutex", pthread_mutex_destroy(&m_mutex)); } void Mutex::Lock() { MutexErr("lock mutex", pthread_mutex_lock(&m_mutex)); } void Mutex::UnLock() { MutexErr("unlock mutex", pthread_mutex_unlock(&m_mutex)); } CondVar::CondVar(Mutex* mu):m_mu(mu) { MutexErr("init cond", pthread_cond_init(&m_cond, NULL)); } CondVar::~CondVar() { MutexErr("destroy cond", pthread_cond_destroy(&m_cond)); } void CondVar::Wait() { MutexErr("wait cond", pthread_cond_wait(&m_cond, &m_mu->m_mutex)); } void CondVar::Signal() { MutexErr("signal cond", pthread_cond_signal(&m_cond)); } void CondVar::SignalAll() { MutexErr("broadcast cond", pthread_cond_broadcast(&m_cond)); }CASLock::CASLock(void* lock):m_lock(lock){}CASLock::~CASLock(){}void CASLock::Lock(){while(!__sync_bool_compare_and_swap((int*)m_lock, 0, 1)){sched_yield();}}void CASLock::UnLock(){*((int*)m_lock) = 0;}bool CASLock::TryLock(){if(__sync_bool_compare_and_swap((int*)m_lock, 0, 1))return 0;elsereturn -1;}
CTime.h
#include <iostream>#include <cstring>#include <ctime>#include <sys/time.h>#include <cstdio>class CTime{public: CTime();CTime(std::string tim); virtual ~CTime();std::string getTime();std::string getShortTime();friend std::ostream& operator<<(std::ostream& os, CTime& tim);void Start();void Stop();void Reset();void costTime();private:std::string m_time; //reserve the time void timeToStr(char* buf);struct timeval t1, t2;bool b;};void CTime::timeToStr(char *buf){time_t ti = time(NULL);strftime(buf, 30, "%Y-%m-%d %H:%M:%S", localtime(&ti));}CTime::CTime(){char buf[30]={'\0'};timeToStr(buf);m_time = std::string(buf);}CTime::CTime(std::string tim){if(tim.size() == 0){char buf[30]; timeToStr(buf);m_time = buf;}else{m_time = tim;}}CTime::~CTime(){}std::string CTime::getTime(){return m_time;}std::string CTime::getShortTime(){if(m_time.size()){return m_time.substr(0, m_time.find_first_of(' '));}else{char buf[30]; timeToStr(buf);return std::string(buf);}}void CTime::Start(){b = false;gettimeofday(&t1, NULL);}void CTime::Stop(){gettimeofday(&t2, NULL);}void CTime::Reset(){}void CTime::costTime(){int usec = 0, sec =0;if(t2.tv_usec > t1.tv_usec){usec = t2.tv_usec - t1.tv_usec;}else{b = true;usec = t2.tv_usec + 1000000 - t1.tv_usec;}if(b){sec = t2.tv_sec -1 -t1.tv_sec;}else{sec = t2.tv_sec - t1.tv_sec;}printf("cost the time:%.05fms\n", (double)(sec*1000000+usec)/1000);}std::ostream& operator<<(std::ostream& os, CTime& tim){os <<tim.m_time;return os;}
TestLog.cpp
#include "CLog.h"CLog *cLog = CLog::getInstance();int main(int argc, char *argv[]){cLog->Init("./common.log", 0);cLog->Info("test the program");cLog->Error("err\n"); return 0;}
编译:
g++ -o testLog TestLog.cpp CLog.h Mutex.h CTime.h
0 0
- C语言写日志功能
- c++实现写日志功能
- C语言实现日志功能
- linux c日志功能实现
- Spring AOP 实现写事件日志功能
- linux 下 C 语言实现写日志
- 写日志的功能
- C写日志文件
- 【C#】写日志方法
- 测试功能Writer写日志
- C语言实现简单的分级别写日志程序
- C语言简单实现日志功能的的题目
- Linux下简单的日志功能实现(C语言)
- 自己写的日志框架--linkinLog4j--实现基本的框架功能
- C/C++写日志代码
- C语言写 系统日志
- 【C++】添加写日志类
- Linux C写日志文件
- Mina传输大数组,多路解码,粘包问题的处理
- マクロ切り替える
- 第十二周项目四 利用遍历思想求解图问题
- Oracle数据库之group by:按自定规则分组
- sublime text 3左侧边栏背景色更改
- c++实现写日志功能
- Android 休眠导致的问题Socket断开
- 第十二周 - 项目3 - 图遍历算法实现
- Window.Open()详解
- 项目5-- 迷宫问题之图深度优先遍历解法
- 第十三周——【项目 - 验证算法】
- linux设备驱动归纳总结(六):3.中断下半部之tasklet
- 第十三周--最小生成树的克鲁斯卡尔算法
- 信息隐藏(2)---直接4bit替换