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;
        }
    }
}

最后完整工程github地址为:https://github.com/alonegiveup/log_test

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ant5985

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值