読者です 読者をやめる 読者になる 読者になる

ログ出力部品

#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#define curFileName (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
#define debug(...) logging("[DEBUG]", curFileName, __LINE__, __VA_ARGS__);
#define info(...) logging("[INFO]", curFileName, __LINE__, __VA_ARGS__);
#define warning(...) logging("[WARNING]", curFileName, __LINE__, __VA_ARGS__);
#define error(...) logging("[ERROR]", curFileName, __LINE__, __VA_ARGS__);
#define critical(...) logging("[CRITICAL]", curFileName, __LINE__, __VA_ARGS__);


FILE *logFile;
errno_t error;

int LogFileOpen()
{
	if((error = fopen_s(&logFile,"C:\\test\\testLog.log","a")) != 0)
	{
		return 10;
	}
	return 0;
}

int LogFileClose()
{
	if(logFile != 0x00)
	{
		if(fclose(logFile) != 0)
		{
			return 10;
		}
	}
	return 0;
}


void logging(const char *level, const char *file, const int line, const char *format, ...)
{
	va_list argp;
#ifdef _DEBUG
	if(level == "[DEBUG]")
	{
		return;
	}
#endif
	fprintf(logFile, "%s%s(%d): ", level, file, line);
	va_start(argp, format);
	vfprintf(logFile, format, argp);
	va_end(argp);
	fprintf(logFile, "\n");
}

int main(int argc, char *argv[])
{
	if( LogFileOpen() != 0)
	{
		return 10;
	}

	debug("START");
	printf("Hello, World!\n");
	debug("EXIT");

	if( LogFileClose() != 0)
	{
		return 10;
	}

  return 0;
}