❤️作者主页:嵌入式_源
❤️作者简介:嵌入式软件工程师,记录学习
❤️点赞 👍 收藏 ⭐再看,养成习惯
log.c
#include "log.h"
#include <stdio.h>
char* EM_LOGLevelGet(const int level)
{
if (level == LOG_DEBUG) {
return "DEBUG";
}
else if (level == LOG_INFO) {
return "INFO";
}
else if (level == LOG_WARN) {
return "WARN";
}
else if (level == LOG_ERROR) {
return "ERROR";
}
return "UNLNOW";
}
void EM_LOG(const int level,const char* fun, const int line ,const char* fmt, ...)
{
#ifdef OPEN_LOG
va_list arg;
va_start(arg, fmt);
char buf[1+vsnprintf(NUll,0,fmt,arg)]
vsnprintf(buf, sizeof(buf), fmt, arg);
va_end(arg);
if (level >= LOG_LEVEL)
printf("[%-5s] [%-20s%4d] %s \r\n", EM_LOGLevelGet(level), fun, line, buf);
#endif
}
log.h
#ifndef __LOG_H_
#define __LOG_H_
#include <stdarg.h>
#define OPEN_LOG 1 //是否打开log调试
#define LOG_LEVEL LOG_DEBUG //log调试等级
typedef enum
{
LOG_DEBUG = 0,
LOG_INFO,
LOG_WARN,
LOG_ERROR,
}E_LOGLEVEL;
void EM_LOG(const int level, const char* fun, const int line, const char* fmt, ...);
#define EMLOG(level,fmt,...) EM_LOG(level,__FUNCTION__,__LINE__,fmt, ##__VA_ARGS__)
#endif
##__VA_ARGS__的作用看这篇文章http://t.csdnimg.cn/pJ7y7