📄 vlog.cpp
字号:
/*************************************************************************** vlog.cpp - description ------------------- begin : 11.17 10:18:00 CST 2003 copyright : (C) 2003 by |LiuZhong| email : |zliu@foundermn.com| ***************************************************************************//*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/#include "linuxh.h"#include "vlog.h"//-------------------------------------------------------------------------------------------------------------------------------------------------string VLog::m_FileName = "/var/log/vlog.log";long VLog::m_LogBufSize = 1024;VCritiSec VLog::m_CriLog;VLog::VLog(){ }VLog::~VLog(){ }void VLog::SaveLogToFile(const char* Logger,const char* Event){ char tmpbuf[128]; memset(tmpbuf,0,128); VAutoLock al(&m_CriLog); FILE *fp = fopen((char*)m_FileName.c_str(),"a+"); if(fp){ time_t curm = time(NULL); struct tm t; localtime_r(&curm,&t); strftime(tmpbuf,127,"[%Y-%m-%d %H:%M:%S]",&t); fprintf(fp,"%s %s %s\n",tmpbuf,Logger,Event); fclose(fp); }}void VLog::SetFileName(const char* FileName){ m_FileName = FileName;}void VLog::SetLogBufSize(long LogBufSize){ if(LogBufSize>1024) m_LogBufSize = LogBufSize;}void VLog::WriteLog(const char* Logger,const char* Event){ SaveLogToFile(Logger,Event);}void VLog::AppendLog(const char* Logger,const char* fmt,...){ char EventBuf[m_LogBufSize]; va_list argptr; va_start(argptr, fmt); vsprintf(EventBuf, fmt, argptr); va_end(argptr); SaveLogToFile(Logger,EventBuf); }//-------------------------------------------------------------------------------------------------------------------------------------------------long VLog2::m_Count = 100;deque<string> VLog2::m_StrList;bool VLog2::m_bRun = false;pthread_t VLog2::m_Pid = 0;VEvent VLog2::m_LogEvent;VCritiSec VLog2::m_CriLog2;VLog2::VLog2(){}VLog2::~VLog2(){}bool VLog2::Open(){ if(m_Pid==0){ if(pthread_create(&m_Pid,NULL,LogServiceThread,NULL)<0){ return false; } } return true;}void VLog2::Close(){ if(m_Pid!=0){ m_bRun = false; pthread_join(m_Pid,NULL); m_Pid = 0; } }bool VLog2::IsOpened(){ return (m_Pid!=0 && m_bRun);}void* VLog2::LogServiceThread(void* pArg){ LogService(); return NULL;}void VLog2::LogService(){ m_bRun = true; FILE *fp = fopen((char*)m_FileName.c_str(),"a+"); while(m_bRun){ if(m_StrList.size()==0) m_LogEvent.Wait(500); string Str=""; { VAutoLock al(&m_CriLog2); if(m_StrList.size()>0){ Str = m_StrList.front(); m_StrList.pop_front(); } } if(Str!=""){ try{ if(fp){ fprintf(fp,"%s\n",Str.c_str()); fflush(fp); } else{ while(!(fp=fopen((char*)m_FileName.c_str(),"a+"))&&m_bRun){ m_LogEvent.Wait(1000); } } } catch(...){ fclose(fp); while(!(fp=fopen((char*)m_FileName.c_str(),"a+"))&&m_bRun){ m_LogEvent.Wait(1000); } } } } fclose(fp);}void VLog2::SetCount(long Count){ if(Count>0) m_Count = Count;}void VLog2::AppendLog2(const char* Logger,const char* fmt,...){ char EventBuf[m_LogBufSize]; va_list argptr; va_start(argptr, fmt); vsprintf(EventBuf, fmt, argptr); va_end(argptr); if(m_bRun) SaveLogToFile2(Logger,EventBuf); else SaveLogToFile(Logger,EventBuf);}void VLog2::WriteLog2(const char* Logger,const char* Event){ if(m_bRun) SaveLogToFile2(Logger,Event); else SaveLogToFile(Logger,Event);}void VLog2::SaveLogToFile2(const char* Logger,const char* Event){ char tmpbuf[128]; memset(tmpbuf,0,128); time_t curm = time(NULL); struct tm t; localtime_r(&curm,&t); strftime(tmpbuf,127,"[%Y-%m-%d %H:%M:%S]",&t); string Str = tmpbuf; Str = Str+" "+Logger+" "+Event; VAutoLock al(&m_CriLog2); if(m_StrList.size()<m_Count){ m_StrList.push_back(Str); m_LogEvent.Set(); } }//-------------------------------------------------------------------------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -