zoukankan      html  css  js  c++  java
  • 一个方便调试所使用的方法

    由于调试及需要可能会经常要记录相关的信息至文件中以便查看,所以写了这个方法,希望对大家有用。
    #pragma once

    #i nclude <fcntl.h>
    #i nclude <sys/types.h>
    #i nclude <sys/stat.h>
    #i nclude <io.h>
    #i nclude <stdio.h>

    int LogWrite(const char *Format, ...);
    CFile g_logFile = CFile::hFileNull;

    BOOL InitLogFile()
    {
     try
     {
      g_logFile.Open("RunMsgLog.txt", CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite);
     }
     catch (CFileException *e)
     {
      e->Delete();
      return FALSE;
     }
     return TRUE;
    }

    /*************************************************************
    *   函数名:LogWrite
    *   说明:  写入程序相关的信息至文件
    **************************************************************/
    int LogWrite(const char *Format, ...)
    {
     CHAR Buffer0[1024];
     CHAR Buffer1[512];
     DWORD ItemsWritten;
     va_list ArgumentList;

     if (g_logFile.m_hFile == CFile::hFileNull)
     {
      if (!InitLogFile())
       return -1;
     }

     if (g_logFile == -1)
      return -1;

     SYSTEMTIME sysTime;
     GetLocalTime (&sysTime);

     sprintf (Buffer0, "%02d年%02d月%02d日: %02d:%02d:%02d-> ", sysTime.wYear, sysTime.wMonth, sysTime.wDay, sysTime.wHour,
      sysTime.wMinute, sysTime.wSecond);

     va_start ( ArgumentList, Format);
     ItemsWritten = vsprintf ((char *)Buffer1, Format, ArgumentList );
     va_end ( ArgumentList );
     strcat (Buffer0, Buffer1);
     strcat (Buffer0, "/r");
     strcat (Buffer0, "/n");
     
     g_logFile.Seek(0, CFile::end);

     g_logFile.Write(Buffer0, strlen((const char *)Buffer0 ));

     return 1;
    }
    在需要记录信息的地方加入此头文件,如下使用即可:
    long lbegin = ::GetCurrentTime();
    // 记录进创建的日志文件。
    LogWrite("共用时 %d 毫秒", (lbegin - GetCurrentTime()) / 1000000);
    LogWrite("完成操作");
     
  • 相关阅读:
    chartjs 初步
    QT “error: error writing to -: Invalid argument”
    OTL mySQL
    qtcteater pro 文件配置
    Python os.readlink() 方法
    Python os.read() 方法
    Python os.popen() 方法
    Python os.pipe() 方法
    Python os.pathconf() 方法
    ThinkPHP框架前后台的分页调用
  • 原文地址:https://www.cnblogs.com/tyjsjl/p/2156085.html
Copyright © 2011-2022 走看看