C++ log printing (a little dynamic application memory)

// CLog.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <stdarg.h>
#include <varargs.h>
#include<string.h>

#define LOG_LEVEL 0
#define OPEN_LOG

enum
{
	LOG_DEBUG,
	LOG_INFO,
	LOG_WARN,
	LOG_ERROR
};

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


void EM_LOG(const int level,const char* fun,const int line,char* fmt, ...)
{
#ifdef OPEN_LOG
	va_list arg;
	va_start(arg,fmt);

	int size = 1 + _vsnprintf(NULL,0,fmt,arg);
	char* buf = new char[size];
	memset(buf,0,size);
	_vsnprintf(buf,size,fmt,arg);
	va_end(arg);

	if (level >= LOG_LEVEL)
	{
		printf("[%s] [%s %d] %s\n",EM_LOGLevelGet(level),fun,line,buf);
	}
	delete buf;buf = NULL;
#endif
}



int _tmain(int argc, _TCHAR* argv[])
{
	EM_LOG(LOG_DEBUG,__FUNCTION__,__LINE__,"啊是多久啊老师的[%s][%d]","asdasda",111);
	getchar();
	return 0;
}



Guess you like

Origin blog.csdn.net/u012842273/article/details/119913777
Recommended