C++程序调试输出并写入文件(Linux/Windows)
来源:互联网 发布:建筑软件班推荐 编辑:程序博客网 时间:2024/06/05 16:26
#include "DebugOut.h"DebugOut::DebugOut(){m_sFilePath = NULL;}DebugOut::DebugOut(char* sFilePath){m_sFilePath = sFilePath;}DebugOut::~DebugOut(){}/************************************************************************//* 功能描述:把调试信息写入到文件中 *//* 参数说明: *//* sFilePath:文件保存路径 *//* sInfo :待写入文件中的调试信息 *//* 返回值 : *//* 0:失败 *//* 1:成功 *//************************************************************************/int DebugOut::WriteToFile(const char* sFilePath, const char* sInfo){if (sFilePath == NULL){return 0;}if (strcmp(sFilePath, "") == 0){return 0;}FileRename(sFilePath);FILE* fp = fopen(sFilePath, "a+");if (fp == NULL){return -1;}fwrite(sInfo, strlen(sInfo+1), 1, fp);fclose(fp);return 0;}/************************************************************************//* 功能描述:从文件路径中解析出文件保存目录、文件名、文件后缀、文件长度 *//* 参数说明: *//* sFilePath:文件绝对路径 *//* sDir :文件所存目录 *//* sFileName:文件名 *//* sSufix :文件后缀 *//* iFileLen :文件长度(字节) *//* 返回值 : *//* 0:失败 *//* 1:成功 *//************************************************************************/int DebugOut::FilePathParse(const char* sFilePath, char* sDir, char* sFileName, char* sSufix, int* iFileLen){//获取文件大小(字节)FILE* fp = fopen(sFilePath, "a+");if (fp == NULL){return 0;}fseek(fp, 0L, SEEK_END);*iFileLen = ftell(fp);fclose(fp);//获取文件int iLen = strlen(sFilePath);int iPos = -1;for (int i=0; i<iLen; i++){if ((sFilePath[i] == '\\') || (sFilePath[i] == '/')){iPos = i;}}char strFile[256] = {0};int j = 0;for (int i=iPos+1; i<iLen; i++){strFile[j++] = sFilePath[i];}strFile[j] = '\0';//获取文件所在目录if (iPos >= 0){int k = 0;for (int i=0; i<=iPos; i++){sDir[k++] = sFilePath[i];}sDir[k] = '\0';}//获取文件名int k = 0;for (unsigned int i=0; i<strlen(strFile); i++){if (strFile[i] == '.'){k = i;sFileName[i] = '\0';break;} else {sFileName[i] = strFile[i];}}//获取文件后缀if(k > 0){int j = 0;for (unsigned int i=k+1; i< strlen(strFile) ; i++){sSufix[j++] = strFile[i];}sSufix[j] = '\0';}return 1;}/************************************************************************//* 功能描述:当文件大小超出指定大小时重命名文件 *//* 参数说明: *//* sFilePath:文件保存路径 *//* 返回值 : *//* 0:失败 *//* 1:成功 *//************************************************************************/int DebugOut::FileRename(const char* sFilePath){int iFileLen = 0;char sFileName[256] = {0};char sDir[256] = {0};char sSufix[256] = {0};if(0 == FilePathParse(sFilePath, sDir, sFileName, sSufix, &iFileLen)){return 0;}if (iFileLen > MAXLEN){char tmpPath[256] = {0};#if (OS_TYPE == Windows_OS)int i = 0;struct _finddata_t FileInfo;while (1){sprintf(tmpPath, "%s%s_%d.%s", sDir, sFileName, i++, sSufix);if(_findfirst(tmpPath, &FileInfo) == -1L){break;}}#endif#if (OS_TYPE == Linux_OS)struct dirent *dp = NULL;DIR *pDir = opendir(sDir);char allFileName[4096] = {0};while((dp = readdir(pDir)) != NULL){strcat(allFileName, "$");strcat(allFileName, dp->d_name);}char tmpName[32] = {0};int i = 0;while(1){sprintf(tmpName, "%s_%d.%s", sFileName, i++, sSufix);if(NULL == strstr(allFileName, tmpName)){break;}}strcpy(tmpPath, sDir);strcat(tmpPath, tmpName);#endifif(-1 == rename(sFilePath, tmpPath)){printf("rename failed. error code:%d, error message:%s\n", errno, strerror(errno));return 0;}}return 1;}/************************************************************************//* 功能描述:获取当前时间 *//* 参数说明:无 *//* 返回值 :返回当前时间 *//************************************************************************/char* DebugOut::GetCurrentTime() {char* strTime = (char*)malloc(32);memset(strTime, 0, 32);time_t timeTmp;time(&timeTmp);struct tm *ptm = localtime(&timeTmp);struct tm *ptm2 = (struct tm *)malloc(sizeof(struct tm));memset(ptm2, 0, sizeof(struct tm));memcpy(ptm2, ptm, sizeof(struct tm));sprintf(strTime, "[%04d-%02d-%02d %02d:%02d:%02d] ", (1900+ptm2->tm_year), (1+ptm2->tm_mon), ptm2->tm_mday, ptm2->tm_hour, ptm2->tm_min, ptm2->tm_sec);free(ptm2);ptm2 = NULL;return strTime;}/************************************************************************//* 功能描述:格式化打印输出,并写入文件 *//* 参数说明:变参 *//* 返回值 :无 *//************************************************************************/void DebugOut::Printf(const char *fmt, ...){char* buf = (char*)malloc(40960);memset(buf, 0, 40960);va_list args;va_start(args, fmt);vsnprintf(buf, 4096, fmt, args);va_end(args);char* sTime = GetCurrentTime();char* sTmp = (char*)malloc(41000);memset(sTmp, 0, 41000);strcpy(sTmp, sTime);strcat(sTmp, buf);#ifdef _DEBUGputs(sTmp);#endifstrcat(sTmp, "\n\r");WriteToFile(m_sFilePath, sTmp);free(sTime);sTime = NULL;free(buf);buf = NULL;free(sTmp);sTmp = NULL;}
#ifndef __DEBUGOUTTOFILE__#define __DEBUGOUTTOFILE__#include <stdlib.h>#include <string.h>#include <time.h>#include <stdio.h>#include <stdarg.h>#define Windows_OS 1#define Linux_OS 2#define OS_TYPE 2#if (OS_TYPE == Windows_OS)#include <io.h>#endif#if (OS_TYPE == Linux_OS)#include <dirent.h>#include <errno.h>#define _DEBUG#endif//#define MAXLEN 4194304#define MAXLEN 128class DebugOut{public:DebugOut();DebugOut(char* sFilePath);~DebugOut();void Printf(const char *fmt, ...);private:char* GetCurrentTime();int WriteToFile(const char* sFilePath, const char* sInfo);int FilePathParse(const char* sFilePath, char* sDir, char* sFileName, char* sSufix, int* iFileLen);int FileRename(const char* sFilePath);char* m_sFilePath;};#endif
#include "DebugOut.h"int main(){DebugOut* debugOut = new DebugOut((char *)"/root/test.txt");debugOut->Printf("%s %s %d", __FILE__, __FUNCTION__, __LINE__);debugOut->Printf("%s %s %d", __FILE__, __FUNCTION__, __LINE__);return 0;}
1 0
- C++程序调试输出并写入文件(Linux/Windows)
- C# 调用控制台程序,并获取输出写入文件
- cs0016:未能写入输出文件 "c:"WINDOWS"Microsoft.NET"Fram...
- 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64
- 程序中输出的各种日志信息类并写入日志文件
- CS0016: 未能写入输出文件“c:/WINDOWS/xxxxxsktsuj.dll”--“拒绝访问。”的处理
- 主题:CS0016: 未能写入输出文件“c:/WINDOWS/Microsoft.NET/***.dll”错误处理[转载]
- 关于未能写入输出文件C:/Windows/Temporary ASP.NET Files/WebApp/错误的解决
- CS0016: 未能写入输出文件“c:/WINDOWS/Microsoft.NET/***.dll”错误处理
- CS0016: 未能写入输出文件“c:/WINDOWS/Microsoft.NET/***.dll”错误处理
- 编译器错误信息: CS0016: 未能写入输出文件“c:/WINDOWS/Microsoft.NET/Framework/
- CS0016: 未能写入输出文件“c:/WINDOWS/Microsoft.NET/***.dll”错误处理
- 未能写入输出文件“c:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files
- 未能写入输出文件“c:/WINDOWS/Microsoft.NET/***.dll”错误处理
- Win7下:编译器错误信息: CS0016: 未能写入输出文件“c:/Windows/Microsoft.NET/Framework……”
- cs0016:未能写入输出文件 "c:"WINDOWS"Microsoft.NET"Framework"v2.0.50727"Temp
- 编译器错误信息: CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework……”--“
- CS0016: 未能写入输出文件“c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temp 解决方法
- 关于页面日期不能绑定到对象的解决办法
- HDU 2255 奔小康赚大钱 (二分图:KM算法)
- MVC中form提交和在控制器中怎样接受
- Sql日期时间格式转换
- html页面 meta设置页面编码问题
- C++程序调试输出并写入文件(Linux/Windows)
- Android 属性动画 详解
- Android基础--登陆界面,密码的隐藏和显示
- java图形-趣味编程之奥运五环;
- 删除重复数据
- Java练习——带界面的通讯录系统
- JavaScript计算字段在数据库中的长度
- JS移动客户端--触屏滑动事件
- install composer on ubuntu