做嵌入式开发,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