c++实现 日志记录
2014-06-25 15:33
183 查看
// logtest.cpp : 定义控制台应用程序的入口点。 ///************************************************************************/ /* author:郑金玮 time:2014/07/03 desc:implement log instance in svc pro */ /************************************************************************/ #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h> #define DISALLOW_COPY_ASSIGN(typename) \ private: \ typename(const typename&); \ typename operator = (const typename&); class CLog { DISALLOW_COPY_ASSIGN(CLog); public: CLog(); ~CLog(); static CLog* getinstance(); bool open(const char szFile[]); void log(const char* fmt,...); private: FILE* m_pfile; }; #define LOGINSTANCE() CLog::getinstance() CLog::CLog() { } CLog::~CLog() { if (fclose(m_pfile) !=0) { return; } } CLog* CLog::getinstance() { static CLog _logInst; return &_logInst; } bool CLog::open(const char szFile[]) { if ((m_pfile = fopen(szFile,"a"))== NULL) { return false; } return true; } void CLog::log(const char* fmt,...) { va_list ap; va_start(ap, fmt);//将ap指向fmt后的第一个参数 char _strtemp[1024]; memset(_strtemp,0,1024); while(*fmt) { if(*fmt == '%') { switch(*(++fmt)) { case 'd':{ char _str[256]; sprintf(_str,"%d",va_arg(ap, int)); strcat(_strtemp,_str); }break; case 'f':{ char _str[256]; double _dval=va_arg(ap, double); _gcvt(_dval,8,_str); strcat(_strtemp,_str); }break; case 's':{ strcat(_strtemp,va_arg(ap, char*)); }break; default:break; } } else { char _str[256]; sprintf(_str,"%c",*fmt); strcat(_strtemp,_str); } fmt ++; } printf("\n"); printf(_strtemp); printf("\n"); fputs("\n",m_pfile); fputs(_strtemp,m_pfile); fputs("\n",m_pfile); va_end(ap); } #define PRINTLOG LOGINSTANCE()->log int _tmain(int argc, _TCHAR* argv[]) { LOGINSTANCE()->open("..//test.log"); //LOGINSTANCE()->log("%s-welcome to beijing---%d------%f","zhengjinwei ",23,23.11); PRINTLOG("%s: welcome to beijing---%d------%f","zhengjinwei ",23,23.11); return 0; }
相关文章推荐
- C++实现一个简单的异常日志记录类
- WinCE程序C/C++/C#实现带时间标记的日志记录
- C++实现一个简洁而又强大的日志记录类
- C++简单日志记录方法
- MySQL用触发器实现日志记录
- 如何覆盖PB的系统函数 比如Messagebox 以记录Messagebox错误日志 或者实现MESSAGEBOX信息的翻译
- 记录程序日志(日志实现和统一接口)
- 利用MSMQ实现高并发情况下快速日志和异常记录
- 用c++实现记录程序运行时间的类
- LOG4CPP &amp;amp; C++实现日志(-)
- 在C++中使用Apache的Log4cxx记录日志
- 全Sql语句实现SBO事务日志记录与查询
- log4j配置相对路径实现日志记录
- C# 实现记录每个线程log日志
- spring2.x使用aop实现声明式日志记录
- 在C++中使用Apache的Log4cxx记录日志
- JavaScript中AOP的实现和日志记录
- 利用触发器实现日志记录
- [c++]记录Windows程序“应用程序错误”到异常日志
- 日志模块(一头文件就实现了日志记录)