C++简单的写日志记录

 1 /*
 2 
 3   简单的写日志记录. (日志而已,何必那么复杂!!!) 
 4    C++6.0 编写  2016.8.31 
 5 
 6 */
 7 #include "stdafx.h"
 8 #include <windows.h>
 9 #include <ctime>
10 #include <fstream>  
11 #include <string>
12 //写入日志文件
13 void WriteLog(char *lpBuffer)
14 {
15     char _szFileName[100]; 
16     GetCurrentDirectory(256,_szFileName);//获取当前运行目录
17     strcat(_szFileName,"\\wLog.txt");//连接后缀名
18     std::string path=_szFileName; //使用string保存当前目录
19     time_t t = time(0);  
20     char tmp[64];  
21     strftime(tmp, sizeof(tmp), "[%Y.%m.%d %X] ", localtime(&t));  //获取当前日期时间
22     std::string loginfo=tmp;//添加时间
23     loginfo.append(lpBuffer);//插入日志内容
24     std::ofstream ofs;  
25     ofs.open(path.c_str(), std::ofstream::app);  //ofstream::app 添加输出 
26     ofs.write(loginfo.c_str(), loginfo.size());  //写入数据
27     ofs <<'\n'; //换行
28     ofs.close();  //关闭文件
29 }
30 
31 
32 
33 int APIENTRY WinMain(HINSTANCE hInstance,
34                      HINSTANCE hPrevInstance,
35                      LPSTR     lpCmdLine,
36                      int       nCmdShow)
37 {
38      // TODO: Place code here.
39 
40 
41   WriteLog("测试软件");
42 
43     return 0;
44 }

 

posted @ 2016-08-31 21:52  By老夫子  阅读(1086)  评论(0)    收藏  举报