控制台显示效果如图:
文本存储效果如图:
使用方法:
#include "log.h"
int main (int argc,char* argv[])
{
int a=1;
LOGV("verbose");
LOGD("debug");
LOGI("infomation");
LOGW("warning");
LOGE("error");
LOGF("fatal,a=%d",a);
system("pause");
return 1;
}
头文件:
#pragma once
#ifndef __LOG_PRINT_H_
#define __LOG_PRINT_H_
#include
#include
#include
#ifdef __cplusplus
extern "C"
{
#endif
#define LOG_LEVEL LOG_LEVEL_VERBOSE
#define LOG_LEVEL_VERBOSE 1 /*所有*/
#define LOG_LEVEL_DEBUG 2 /*调试*/
#define LOG_LEVEL_INFO 3 /*信息*/
#define LOG_LEVEL_WARNING 4 /*警告*/
#define LOG_LEVEL_ERROR 5 /*错误*/
#define LOG_LEVEL_FATAL 6 /*致命错误*/
#define LOG_LEVEL_SILENT 7 /*静默*/
#define TITLE_VERBOSE " V:"
#define TITLE_DEBUG " D:"
#define TITLE_INFO " I:"
#define TITLE_WARNING " W:"
#define TITLE_ERROR " E:"
#define TITLE_FATAL " F:"
#ifndef LOG_LEVEL
# define LOG_LEVEL LOG_LEVEL_VERBOSE
#endif
#define LOG_NOOP (void) 0
#ifndef _FILE_POINTER_
#define _FILE_POINTER_
static FILE *fp_log;
#endif
#ifndef _TIME_POINTER_
#define _TIME_POINTER_
static time_t timep;
static struct tm *p;
#endif
#ifdef DEBUG_FILE
# define LOG_PRINT(level,fmt,...) \
fp_log=fopen(DEBUG_FILE,"a"); \
time(&timep); \
p =localtime(&timep); \
fprintf(fp_log,\
"%d-%d %d:%d:%d In %s Line:%u " level fmt "\n",\
1+p->tm_mon,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec,\
__FILE__,__LINE__,##__VA_ARGS__); \
fclose(fp_log); \
fp_log=NULL
#else
# define LOG_PRINT(level,fmt,...) \
time(&timep); \
p =localtime(&timep); \
fprintf(stdout,\
"%d-%d %d:%d:%d In %s Line:%u " level fmt "\n",\
1+p->tm_mon,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec,\
__FILE__,__LINE__,##__VA_ARGS__)
#endif
#if LOG_LEVEL_VERBOSE >= LOG_LEVEL
# define LOG_VERBOSE(fmt,...) LOG_PRINT(TITLE_VERBOSE,fmt,##__VA_ARGS__)
#else
# define LOG_VERBOSE(...) LOG_NOOP
#endif
#define LOGV(format,...) LOG_VERBOSE(format,##__VA_ARGS__)
#if LOG_LEVEL_DEBUG >= LOG_LEVEL
# define LOG_DEBUG(fmt,...) LOG_PRINT(TITLE_DEBUG,fmt,##__VA_ARGS__)
#else
# define LOG_DEBUG(...) LOG_NOOP
#endif
#define LOGD(format,...) LOG_DEBUG(format,##__VA_ARGS__)
#if LOG_LEVEL_INFO>= LOG_LEVEL
# define LOG_INFO(fmt,...) LOG_PRINT(TITLE_INFO,fmt,##__VA_ARGS__)
#else
# define LOG_INFO(...) LOG_NOOP
#endif
#define LOGI(format,...) LOG_INFO(format,##__VA_ARGS__)
#if LOG_LEVEL_WARNING >= LOG_LEVEL
# define LOG_WARN(fmt,...) LOG_PRINT(TITLE_WARNING,fmt,##__VA_ARGS__)
#else
# define LOG_WARN(...) LOG_NOOP
#endif
#define LOGW(format,...) LOG_WARN(format,##__VA_ARGS__)
#if LOG_LEVEL_ERROR >= LOG_LEVEL
# define LOG_ERROR(fmt,...) LOG_PRINT(TITLE_ERROR,fmt,##__VA_ARGS__)
#else
# define LOG_ERROR(...) LOG_NOOP
#endif
#define LOGE(format,...) LOG_ERROR(format,##__VA_ARGS__)
#if LOG_LEVEL_FATAL >= LOG_LEVEL
# define LOG_FATAL(fmt,...) LOG_PRINT(TITLE_FATAL,fmt,##__VA_ARGS__)
#else
# define LOG_FATAL(...) LOG_NOOP
#endif
#define LOGF(format,...) LOG_FATAL(format,##__VA_ARGS__)
#ifdef __cplusplus
}
#endif
#endif