程序运行调试日志的简单方法

程序运行调试日志的简单方法

1. 添加下面的宏

 

// 在调试状态下, 才会产生日志

#ifdef _DEBUG

#define  WRITE_LOG(strLog) WriteLog(strLog, __FILE__, __LINE__, __FUNCTION__)

#define  WRITE_DATA(strData, nDataLen) WriteData(strData, nDataLen)

#else

#define  WRITE_LOG(strLog)   

#define  WRITE_DATA(strData, nDataLen) 

#endif

 

 

#include <stdio.h>

void WriteLog(const char* strLog, char *pFile, int nLine, char *strFunction)

       {

       try

              {

              if(0 == strLog)

                     {

                     return;

                     }

              if(0 == strlen(strLog))

                     {

                     return;

                     }

              // 日志时间

              SYSTEMTIME systime;

              memset(&systime,0,sizeof(systime));

              GetLocalTime(&systime);

 

              FILE       *pFile;

              pFile=fopen("Log.Log","a+");

              if(pFile!=NULL)

                     {

 

                     fprintf(pFile,"/n/n%04d-%02d-%02d %02d:%02d:%02d.%03d】【%s(%d)-%s/nLOG%s/n",systime.wYear,systime.wMonth,

                            systime.wDay,systime.wHour,systime.wMinute,systime.wSecond,systime.wMilliseconds,

                            sFile,nLine,sFunction,strLog);

                     fclose(pFile);

                     }

              }catch(...){}

       }

 

void WriteData(const unsigned char* strLog,int nLength)

       {

       try

              {

              if (0 == strLog )

                     {

                     return ;

                     }

 

              if (0 == nLength )

                     {

                     return ;

                     }

 

              int  nLineByte = 0;           //行字节数

 

              FILE       *pFile;

              pFile=fopen("Data.Log","a+");

              if(pFile!=NULL)

                     {

                     fprintf(pFile,"/n/nDATA/n");

                     for (int i=0; i<nLength; i++)

                            {

                            if (nLineByte == 32)

                                   {

                                   fprintf(pFile,"/n");

                                   nLineByte = 0;

                                   }

 

                            fprintf(pFile,"%02x ",strLog[i]);

                            nLineByte++;

                            }

                     fprintf(pFile,"/n");

                     fclose(pFile);

                     }

              }catch(...){}

#endif

       }

 

 

// 使用

WRITE_LOG("received the invite message from server");

WRITE_DATA((unsigned char*)pdata, nDataLen);

你可能感兴趣的:(server,function,File,null)