Write .log file
来源:互联网 发布:centos yum安装svn 编辑:程序博客网 时间:2024/06/05 07:33
No. T01
写在前面:
放下LabVIEW选择C++半年有余,从零开始到能够参与项目模块开发,走不少弯路费不少精力,然在工作中发现基础不扎实导致效率低下也是时常发生。新的一年开始了,就想着夯实基础,整理头脑。
新开通博客也不知从何写起,就整理下工具吧,往后再使用时也就信手拈来了。
T01) 写日志文件:要求保留一个月数据。代码片段如下
#include <direct.h>#include <time.h> #include <sys/stat.h>//————————————————————————————————————————void WriteToFile(char* path,char* Msg){ struct stat temp; char su1[MAX_PATH]; char su2[MAX_PATH]; memcpy(su1,path,MAX_PATH); memcpy(su2,path,MAX_PATH); strcat(su1,".1"); strcat(su2,".2"); stat(path,&temp); int ss=temp.st_size; if (ss/1024/1024/1024>=1) { DeleteFileA(su2); rename(su1,su2); rename(path,su1); } FILE *file=fopen(path,"a+"); fwrite(Msg,strlen(Msg),1,file); fclose(file); return;}void LogInfo(){ HINSTANCE dllModule = GetModuleHandleA("BH_PIN.dll"); //日志路径. memset(mPath, 0, MAX_PATH); GetModuleFileNameA(dllModule, mPath, sizeof(mPath)); *(strrchr(mPath, '\\') + 1) = 0; memset(mdllItem, 0, 1024); memcpy(mdllItem, mPath, strlen(mPath)); strcat_s(mdllItem, "\\KMY350X.dll"); strcat_s(mPath, "LOG\\"); _mkdir(mPath); //产生日志名. time_t t = time(0); char LogName[64]; strftime(LogName, sizeof(LogName), "PIN%Y%m%d.log", localtime(&t)); strcat_s(PINpath, LogName); //删除日志 int year, month, day; tm* tt = localtime(&t); year = tt->tm_year+1900; month = tt->tm_mon; day = tt->tm_mday; memset(LogName, 0, 64); if ( 0 == month ) //1月时删除上一年12月的日志 { sprintf_s(LogName, "PIN%d%02d%02d.log", year-1, 12, day); strcat_s(mPath, LogName); DeleteFileA(mPath); return; } for (int i = 1; i < 13; i++) { if (i == month+1) continue; CStringA szstr; szstr.Empty(); szstr = mPath; int k = szstr.ReverseFind('\\'); szstr = szstr.Left(k+1); sprintf_s(LogName, "PIN%d%02d%02d.log", year, i, day); szstr += LogName; bool bb = DeleteFileA(szstr.GetBuffer()); if (bb) { time_t t = time(0); char tmp[128]; strftime(tmp, sizeof(tmp), "<<%H:%M:%S#信息>>删除日志成功!", localtime(&t)); strcat(tmp, szstr.GetBuffer()); strcat(tmp, "\n"); WriteToFile(mPath, tmp); } } return;}
PS: 第一篇技术博客,难免有漏洞或错误,请多多指正。
本工具也是比较粗糙的,比如2月份时因为最多29天,那么1月份最后两天的数据就没法被删除完,只有等到3月份时才会被删除。然也能够实现需求,就这样吧。
1 0
- log file parallel write
- log file parallel write
- Write .log file
- log file sync, log file parallell write
- log file sync, log file parallell write
- log file sync, log file parallell write
- Write log file with .net
- log file parallel write&&log buffer space
- log file sync和log file parallel write的关系
- log file sync 与 log file parallel write 等待事件
- log buffer —— log file parallel write
- log buffer —— log file sequential read、log file single write
- Oracle db file parallel write 和 log file parallel write 等待事件 说明
- Oracle db file parallel write 和 log file parallel write 等待事件 说明
- log file parallel write wait event(摘自sharklove)
- Latex Error: ! I can't write on file `xxx.log'.
- WAITEVENT: "log file parallel write" Reference Note (文档 ID 34583.1)
- log file sync(日志文件同步) 与 Log file parallel write 等待事件
- 九度OJ 1386(最值) 1387(递归) 1388(递归) 1389(递归) 1390(递归)
- java18.IO操作------使用带缓冲的字节流读写数据
- Python【map、reduce、filter】内置函数使用说明(转载)
- umeng1.统计SDK技术测评
- Handler发送消息小结
- Write .log file
- 挥手2015,笑迎2016
- 文章标题
- rpm程序包管理篇
- 【基础建模】复杂建模-布尔运算和放样学习笔记
- request对象
- java的TTS(Text to Speech)的实现
- umeng2应用更新SDK技术评测
- 优化var_dump()