1. 程式人生 > >C語言 日誌輸出 測試執行時間(Windows、Linux平臺)

C語言 日誌輸出 測試執行時間(Windows、Linux平臺)

做嵌入式開發,Debug移植是比較頭疼的問題。當我們需要測試程式執行時間,輸出變數的值以用來除錯時非常不方便,雖然linux中有gdb,Windows中有vs可進行除錯。但在部分場景下,還是需要進行日誌輸出較為方便。為此,本人將本人在嵌入式開發過程中使用較多的日誌資訊,封裝成巨集定義。可較為方便的去使用。
下面貼出相關程式碼,供需要的朋友使用除錯。
log.h

#ifndef _LOG_H
#define _LOG_H

#ifndef G_LOG_H
#define G_LOG_H extern
#endif
#define LOG_WRITE(FILEPATH,FORMAT,...) log_write(FILEPATH,__FILE__,__LINE__,__FUNCTION__,FORMAT,##__VA_ARGS__)
#define LOG_PRINT(FORMAT,...) log_print(__FILE__,__LINE__,__FUNCTION__,FORMAT,##__VA_ARGS__) #define LOG_RECORD_BEGIN log_record_begin() #define LOG_WRITE_RECOED_END(FILEPATH) log_record_end(0,FILEPATH,__FILE__,__LINE__,__FUNCTION__) #define LOG_PRINT_RECOED_END log_record_end(1,"",__FILE__,__LINE__,__FUNCTION__)
void log_write(const char* filepath, const char* filename, int lines, const char* functions, const char *format, ...); void log_print(const char* filename, int lines, const char* functions,const char*format, ...); void log_record_begin(); void log_record_end(int flag, const char* filepath, const
char* filename, int lines, const char* functions); #endif

log.cpp

#include "log.h"
#ifdef _WIN32
    #include <stdio.h>  
    #include <stdarg.h> 
    #include <time.h>
    #include <windows.h>
    clock_t c_start, c_end;
#elif __linux__
    #include <stdio.h>
    #include <stdarg.h> 
    #include <sys/time.h>
    #include <time.h>
    struct timeval t_start,t_end;
#endif


void log_write(const char* filepath,const char* filename,int lines,const char* functions, const char *format, ...)
{
    FILE*pFile = fopen(filepath, "a+");
    va_list arg;
    int done;
    va_start(arg, format);
    time_t time_log = time(NULL);
    struct tm* tm_log = localtime(&time_log);
    fprintf(pFile, "Data:%04d-%02d-%02d %02d:%02d:%02d File:%s Line:%d Function:%s \t", tm_log->tm_year + 1900, tm_log->tm_mon + 1, tm_log->tm_mday, tm_log->tm_hour, tm_log->tm_min, tm_log->tm_sec, filename, lines,functions);
    done = vfprintf(pFile, format, arg);
    fprintf(pFile,"\n");
    va_end(arg);
    fflush(pFile);
    fclose(pFile);
}

void log_print(const char* filename, int lines, const char* functions,const char*format, ...)
{
    va_list arg;
    int done;
    va_start(arg, format);
    time_t time_log = time(NULL);
    struct tm* tm_log = localtime(&time_log);
    printf("Data:%04d-%02d-%02d %02d:%02d:%02d File:%s Line:%d Function:%s \t", tm_log->tm_year + 1900, tm_log->tm_mon + 1, tm_log->tm_mday, tm_log->tm_hour, tm_log->tm_min, tm_log->tm_sec, filename, lines, functions);
    done = vprintf(format, arg);
    printf("\n");
    va_end(arg);
}

void log_record_begin()
{
#ifdef _WIN32
    c_start = clock();
#elif __linux__
    gettimeofday(&t_start, NULL);
#endif

}

void log_record_end(int flag, const char* filepath, const char* filename, int lines, const char* functions)
{
#ifdef _WIN32
    c_end = clock();
    double costtime = (double)(c_end - c_start);
#elif __linux__
    gettimeofday(&t_end, NULL);
    long start = ((long)t_start.tv_sec)*1000+(long)t_start.tv_usec/1000;
    long end = ((long)t_end.tv_sec) * 1000 + (long)t_end.tv_usec / 1000;
    long costtime = end - start;
#endif
    switch (flag)
    {
        case 0:
        {
#ifdef _WIN32
            log_write(filepath,filename,lines,functions,"UsedTime:%.2fms",costtime);
#elif __linux__
            log_write(filepath,filename,lines,functions,"UsedTime:%ldms",costtime);
#endif // _WIN32
            break;
        }
        case 1:
        {
#ifdef _WIN32
            log_print(filename,lines,functions,"UsedTime:%.2fms",costtime);
#elif __linux__
            log_print(filename,lines,functions,"UsedTime:%ldms",costtime);
#endif // _WIN32
            break;
        }
        default:
        {
            break;
        }
    }
}