#include "stdafx.h"
#include "Log.h"
// ログ　中友さんのログに、エディットボックスへもログが残せるように変更
void Log(char *file , char *functionname , int line , const char *Format ...)
{
	CString strTmp,strLog;
	//m_edit_Log.GetWindowTextA(strLog);
	//if ( strLog.GetLength() > 1000000 ) strLog = "";

	FILE *fp;
	fpos_t fsize;
	char filename[256];
	va_list list;
	struct tm *date;
	time_t now;
	int year, month, day;
	int hour, minute, second;

	if( !strstr( file , ".txt" ) )
	{
		sprintf( filename , "%s.txt" , file );
	}
	else
	{
		strcpy( filename , file );
	}

	fp = fopen( filename , "a" );

	//ファイルサイズ取得
	fseek( fp , 0 , SEEK_END );
	fgetpos( fp , &fsize );

	rewind(fp);

	if( fsize > 10000000 )
	{
		fclose(fp);
		fp = fopen( filename , "w" );
	}

	/* 現在の日時を取得 */
	time(&now);
	date = localtime(&now);

	year = date->tm_year - 100;
	month = date->tm_mon + 1;
	day = date->tm_mday;

	hour = date->tm_hour;
	minute = date->tm_min;
	second = date->tm_sec;

	fprintf( fp , "%02d%02d%02d - %02d%02d%02d - %s %d	" , year , month , day , hour , minute , second , functionname , line);

	va_start( list , Format );
	vfprintf( fp , Format , list );
	strTmp.FormatV(Format,list);
	va_end( list );

	fprintf( fp , "\n" );
	strLog += strTmp;
	strLog += "\r\n";

	//m_edit_Log.SetWindowTextA(strLog);

	fclose(fp);
}
