Debug chương trình C/C++ bằng hàm trace nhiều tham số

Đặt vấn đề 

Theo các bạn, chương trình C/C++ chạy trên Windows/Linux OS có bao nhiêu cách debug?.

  • Local Debug
  • Remote Debug
  • Breakpoint
  • Trace log

Trong đó, breakpoint được sử dụng debug chương trình EXE (ví dụ: console application, Win32/MFC).

Nhưng nếu chương trình EXE của bạn call dynamic library (DLL) hoặc là printer driver (GDI gọi function trong DLL file), bạn không thể sử dụng breakpoint để debug logic trong file DLL.

Solution: viết hàm để trace biến và giá trị ra file. Phần tiếp theo sẽ hướng dẫn các bạn viết hàm trace log với nhiều đối số truyền vào.

Hàm Trace Log

void TraceLog( const char *pStr, ... )
{
    char LogFilePath[1024];
    char buf[9];
    char szBuff[1024];
    va_list arg;
    FILE *fpLogFile = NULL;

    memset(LogFilePath, 0, 1024);
    memset(buf, 0, 9);
    memset(szBuff, 0, 1024);

    sprintf(LogFilePath, "%s\\TraceLog.txt", "C:\\trace");
    fpLogFile = fopen(LogFilePath, "a+");
    if (fpLogFile == NULL)
        return;

    //[mm/dd/yyyy hh:mm:ss]
    _strdate(buf);
    sprintf(szBuff,"[%s",buf);
    _strtime(buf);
    sprintf(szBuff,"%s %s] ",szBuff,buf);
    fprintf(fpLogFile, "%s",szBuff);
    if ( pStr )
    {
        va_start(arg, pStr);
        vfprintf(fpLogFile, pStr, arg);
        va_end(arg);
    }
    fprintf(fpLogFile, "\n" );

    fclose(fpLogFile);
    return;
}

Giải thích:

sprintf(LogFilePath, "%s\\TraceLog.txt", "C:\\trace");
fpLogFile = fopen(LogFilePath, "a+");
if (fpLogFile == NULL)
    return;

– File trace được lưu đường dẫn “C:\trace\TraceLog.txt”. Các bạn có thể thay bằng đường dẫn khác.
– File được open với mode append.

_strdate(buf);
sprintf(szBuff,"[%s",buf);
_strtime(buf);
sprintf(szBuff,"%s %s] ",szBuff,buf);
fprintf(fpLogFile, "%s",szBuff);

– Đoạn code này ghi timestamp [mm/dd/yyyy hh:mm:ss] vào đầu mỗi line trong trace file.
– Để sử dụng hàm _strdate(), _strtime(), cần include

if ( pStr )
{
    va_start(arg, pStr);
    vfprintf(fpLogFile, pStr, arg);
    va_end(arg);
}

Đoạn code này lưu các đối số trong biến pStr và ghi nội dung vào trace file.
Phần tiếp theo hướng dẫn các bạn sử dụng hàm trong ví dụ cụ thể.

Test hàm Trace Log

////////////////////////////////////////////////////////////////////
// Debug by Trace Log function
// 01/11/2017   vncoding    Create
//
////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <time.h>
#include <stdarg.h>
#include <string.h>

void TraceLog( const char *pStr, ... );

int main()
{
    char name[] = "Viet";
    char blog[] = "vncoding";
    unsigned int age = 28;


    TraceLog("My name's %s, %d year old", name, age);
    TraceLog("I belong a %s", blog);

    return 0;
}

void TraceLog( const char *pStr, ... )
{
    char LogFilePath[1024];
    char buf[9];
    char szBuff[1024];
    va_list arg;
    FILE *fpLogFile = NULL;

    memset(LogFilePath, 0, 1024);
    memset(buf, 0, 9);
    memset(szBuff, 0, 1024);

    sprintf(LogFilePath, "%s\\TraceLog.txt", "C:\\trace");
    fpLogFile = fopen(LogFilePath, "a+");
    if (fpLogFile == NULL)
        return;

    //[mm/dd/yyyy hh:mm:ss]
    _strdate(buf);
    sprintf(szBuff,"[%s",buf);
    _strtime(buf);
    sprintf(szBuff,"%s %s] ",szBuff,buf);
    fprintf(fpLogFile, "%s",szBuff);
    if ( pStr )
    {
        va_start(arg, pStr);
        vfprintf(fpLogFile, pStr, arg);
        va_end(arg);
    }
    fprintf(fpLogFile, "\n" );

    fclose(fpLogFile);
    return;
}

Kết quả:

Hàm Trace Log
Hàm Trace Log

Be the first to comment

Leave a Reply