如何在程序中写log日志(定义log级别:error,warn, info, debug; 宏定义打印不同级别的日志; 程序中引用宏定义即可)

  • Post author:
  • Post category:其他


#include <stdio.h>
#include <stdlib.h>
#define  global_debug 2
#define global_progname "MYLOG"

enum
{
	ERROR,
	WARN,
	INFO,
	DEBUG,
};


#define debug(OUT, STR, ARGS...)  \
	if (global_debug >= DEBUG) \
		fprintf(stdout, "%s - DEBUG: [%lli] ", global_progname, (long long)getpid()),\
		fprintf(OUT, STR, ##ARGS), fflush(OUT)

#define info(OUT, STR, ARGS...)  \
	if (global_debug >= INFO) \
		fprintf(stdout, "%s - INFO: [%lli] ", global_progname, (long long)getpid()),\
		fprintf(OUT, STR, ##ARGS), fflush(OUT)

#define warn(OUT, STR, ARGS...)  \
	if (global_debug >= WARN) \
		fprintf(stdout, "%s - WARN: [%lli] (line: %d) ", global_progname, (long long)getpid(),__LINE__ ),\
		fprintf(OUT, STR, ##ARGS), fflush(OUT)

#define error(OUT, STR, ARGS...)  \
	if (global_debug >= ERROR) \
		fprintf(stdout, "%s - ERROR: [%lli](line: %d) ", global_progname, (long long)getpid(), __LINE__),\
	



版权声明:本文为j123kaishichufa原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。