Windows下一个LogFile类
来源:互联网 发布:mac口红畅销的颜色色号 编辑:程序博客网 时间:2024/05/07 16:55
头文件:
#ifndef __LOGFILE__H__#define __LOGFILE__H__// Include fstream header#ifndef _FSTREAM_#include <fstream>using namespace std;#endifclass CLogFile{public: void PrintCurTime(); CLogFile(); CLogFile(TCHAR* tszFileName); ~CLogFile(); CLogFile& operator <<(long lVal); CLogFile& operator <<(const TCHAR* str); CLogFile& operator <<(TCHAR tch); CLogFile& operator <<(int nVal); CLogFile& operator <<(unsigned long ulVal); CLogFile& operator <<(double dVal); CLogFile& operator <<(unsigned int unVal); void LOGERROR(TCHAR* formatString, ...);private: ofstream *m_cOutFile; voidOutput(const TCHAR* data);};#endif// __LOGFILE__H__
cpp文件:
#include "stdafx.h"#include <afxwin.h>#include <strstream>#include "logfile.h"using namespace std;CLogFile::CLogFile(){ //Open file m_cOutFile = new ofstream(_T("c:\\smartform.log"), ios_base::out); if (!m_cOutFile->is_open()) { AfxMessageBox(_T("Unable to create log file")); delete m_cOutFile; m_cOutFile = NULL; }}CLogFile::CLogFile(TCHAR* tszFileName){ m_cOutFile = new ofstream(tszFileName, ios_base::out); if (!m_cOutFile->is_open()) { AfxMessageBox(_T("Unable to create log file")); delete m_cOutFile; m_cOutFile = NULL; }}CLogFile::~CLogFile(){ //Close the file and do clean up m_cOutFile->close(); delete m_cOutFile; m_cOutFile = NULL;}void CLogFile::Output(const TCHAR *data){ m_cOutFile->write(data,_tcslen(data));}void CLogFile::PrintCurTime(){ TCHAR dateString[52]; SYSTEMTIME cur; GetLocalTime(&cur); sprintf(dateString,"%d/%d/%d, %d:%d:%d ", cur.wYear, cur.wMonth, cur.wDay, cur.wHour, cur.wMinute, cur.wSecond); Output(dateString);}CLogFile& CLogFile::operator <<(unsigned int unVal){ strstream tmp; tmp << unVal; tmp << '\0'; TCHAR* output = tmp.str(); Output(output); tmp.freeze(false); return *this;}CLogFile& CLogFile::operator <<(long lVal){ strstream tmp; tmp << lVal; tmp << '\0'; TCHAR* output = tmp.str(); Output(output); tmp.freeze(false); return *this;}CLogFile& CLogFile::operator <<(const TCHAR* str){ Output(str); return *this;}CLogFile& CLogFile::operator <<(TCHAR tch){ TCHAR szCh[2]; szCh[0] = tch; szCh[1] = '\0'; Output(szCh); return *this;}CLogFile& CLogFile::operator <<(int nVal){ strstream tmp; tmp << nVal; tmp << '\0'; TCHAR* output = tmp.str(); Output(output); tmp.freeze(false); return *this;}CLogFile& CLogFile::operator <<(unsigned long ulVal){ strstream tmp; tmp << ulVal; tmp << '\0'; TCHAR* output = tmp.str(); Output(output); tmp.freeze(false); return *this;}CLogFile& CLogFile::operator <<(double dVal){ strstream tmp; tmp << dVal; tmp << '\0'; TCHAR* output = tmp.str(); Output(output); tmp.freeze(false); return *this;}void CLogFile::LOGERROR(TCHAR* formatString, ...){ // Insert the current time.. PrintCurTime(); // Parse the format string and write to the file if(formatString == NULL) { // No point in continuiing return; } va_list argList; // Set va_list to the beginning of optional arguments va_start(argList, formatString); TCHAR* ptr = formatString; while(*ptr != '\0') { TCHAR* str = NULL; int nInteger = 0; unsigned int unInt= 0; long lLong = 0; unsigned long ulLong = 0; double dDoub = 0; if(*ptr == '%') { switch(*(ptr+1)) { case 's': str = va_arg(argList, TCHAR*); if( NULL == str) break; *this << str; ptr++; break; case 'd': nInteger = va_arg(argList, int); *this << nInteger; ptr++; break; case 'u': unInt = va_arg(argList, unsigned int); *this << unInt; ptr++; break; case 'l': ptr++; if(*(ptr+1) == 'd') { lLong = va_arg(argList, long); *this << lLong; ptr++; } else if(*(ptr+1) == 'u') { ulLong = va_arg(argList, unsigned long); *this << ulLong; ptr++; } break; case 'f': dDoub = va_arg(argList, double); *this << dDoub; ptr++; break; default:*this << *ptr; } } // if(*ptr == '%') else { *this << *ptr; } // Increment pointer.. ptr++; } *this << '\n';}
编译的时候:设置VS字符集为多字节字符集
测试代码:
#include "StdAfx.h"#include "logfile.h"int main(){ CLogFile log("lof_file.txt"); log<<1; log<<"\n"; log<<2.3; log<<"\n"; log<<"ok"; log<<"\n"; log.LOGERROR("%d, %s",1,"error1"); log.LOGERROR("%d, %s",2,"error2"); return 0;}
- Windows下一个LogFile类
- logfile
- windows 下一个小搜索
- Windows Phone 7下一个上传图片的类
- muduo库的LogFile日志文件类剖析
- oracle logfile
- -logfile -l
- redo logfile
- 下一个
- 如何直接删除windows下一个文件夹?(转)
- Windows下一个汇编小程序列表文件
- windows下一个简单的输入法模块[Python]
- Windows下一个并发阻塞队列(BlockingQueue)
- Windows下一个并发阻塞队列(BlockingQueue)
- windows 下一个线程池的实现
- 当涉及到安全性,Android是下一个Windows
- windows下一个目录最大文件数目限制(摘自)
- Windows 8是微软下一个重炮还是Vista的翻版?
- 草稿
- 【Linux】Tomcat6.0.35
- ArrayList和linkLIst的区别
- POJ-3040-Asteroids
- 拼搏进行时
- Windows下一个LogFile类
- Linux内核分析 - 网络[十六]:TCP三次握手
- A folder failed to be renamed or moved--安装Android SDK的问题
- Android调用系统发送短信界面
- 矩阵键盘以及测试
- MyEclipse10添加jadclipse反编译插件
- 怎样通过DOS来提取一个文件夹下所有文件的名字
- 简单总结一下debug
- web学习—css样式表—css属性(一)