// stdafx.cpp : ֻ������׼�����ļ���Դ�ļ�
// test.pch ����ΪԤ����ͷ
// stdafx.obj ������Ԥ����������Ϣ

#include "stdafx.h"

#include <time.h> //���� #include <ctime>
#include <locale.h>

// TODO: �� STDAFX.H ��
// �����κ�����ĸ���ͷ�ļ����������ڴ��ļ�������

/************************************************************************/
/*  ������[7/3/2018 Wang];
/*  ������;
/*  ������;
/*  	[IN] ��;
/*  	[OUT] ��;
/*  	[IN/OUT] ��;
/*  ���أ�void;
/*  ע�⣺���̰߳�ȫ;
/*  ʾ����;
/*
/*  �޸ģ�;
/*  ���ڣ�;
/*  ���ݣ�;
/************************************************************************/
void ShowSystemErrorInfo( LPCTSTR lpszTitle,const DWORD &dwError)
{
	LPVOID lpMsgBuf;
	static TCHAR szResult[MAX_PATH] = {0};
	memset(szResult, 0, sizeof(TCHAR)*MAX_PATH);
	BOOL fOk = FormatMessage(
		FORMAT_MESSAGE_ALLOCATE_BUFFER |
		FORMAT_MESSAGE_FROM_SYSTEM |
		FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL,
		dwError,
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		(LPTSTR)&lpMsgBuf,
		0, NULL);

	if (!fOk)
	{
		// Is it a network-related error?
		HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES);

		if (hDll != NULL)
		{
			FormatMessage(
				FORMAT_MESSAGE_FROM_HMODULE |
				FORMAT_MESSAGE_FROM_SYSTEM |
				FORMAT_MESSAGE_IGNORE_INSERTS,
				hDll,
				dwError,
				MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
				(LPTSTR)&lpMsgBuf,
				0,
				NULL);

			FreeLibrary(hDll);
		}
	}

	if (lpMsgBuf != NULL)
	{
		_stprintf_s(szResult, _T("%s:%ld,%s"), lpszTitle == NULL ? _T("����") : lpszTitle, dwError, (PCTSTR)LocalLock(lpMsgBuf));
		WriteTextLog(szResult);
		LocalFree(lpMsgBuf);
	}
	else
	{
		_stprintf_s(szResult, _T("%s:δ֪����"),lpszTitle == NULL ? _T("����") : lpszTitle);
		WriteTextLog(szResult);
	}
}

/************************************************************************/
/*  ������WriteTextLog[7/28/2016 IT];
/*  ������д�ı���־;
/*  ������;
/*  	[IN] ��;
/*  ���أ�void;
/*  ע�⣺;
/*  ʾ����;
/*
/*  �޸ģ�;
/*  ���ڣ�;
/*  ���ݣ�;
/************************************************************************/
void WriteTextLog(const TCHAR *format, ...)
{
	//static ThreadSection _critSection;
	//AutoThreadSection aSection(&_critSection);
	// ��������־·��;
	TCHAR szlogpath[MAX_PATH] = {0};
	static TCHAR szModulePath[MAX_PATH] = {0};
	static TCHAR szFna[_MAX_DIR] = { 0 };
	if ( szModulePath[0] == _T('\0') )
	{
		TCHAR szDrive[_MAX_DRIVE] = { 0 };
		TCHAR szDir[_MAX_DIR] = { 0 };
		TCHAR szExt[_MAX_DIR] = { 0 };
		::GetModuleFileName(NULL, szModulePath, sizeof(szModulePath) / sizeof(TCHAR));
		_tsplitpath_s(szModulePath, szDrive, szDir, szFna, szExt);
		_tcscpy_s(szModulePath, szDrive);
		_tcscat_s(szModulePath, szDir);
	}

	// ��ȡ�������;
	__time64_t gmt = time(NULL);// ��ȡ��ǰ����ʱ��(1900-01-01��ʼ��Unixʱ���);
	struct tm gmtm = {0};
	//gmtime_s(&gmtm, &gmt); // ʱ���ת��UTCʱ��(Ҳ��GMTʱ��);
	localtime_s(&gmtm, &gmt); // ʱ���ת�ɱ���ʱ��;
	_stprintf_s(szlogpath, _T("%s%s%04d-%02d-%02d.txt"), szModulePath, szFna, gmtm.tm_year + 1900, gmtm.tm_mon+1, gmtm.tm_mday);

	// �򿪻򴴽��ļ�;
#if 0 // MFC
	CStdioFile fp;
	if (PathFileExists(szlogpath))
	{
		if (fp.Open(szlogpath, CFile::modeWrite) == FALSE)
		{
			return;
		}
		fp.SeekToEnd();
	}
	else
	{
		fp.Open(szlogpath, CFile::modeCreate | CFile::modeWrite);
	}

	// ��ʽ��ǰ������������;
	TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
	_tsetlocale(LC_CTYPE, _T("chs"));//�趨����;

	// ��ʽ����־����;
	va_list		args = NULL;
	int			len = 0;
	TCHAR		*buffer = NULL;
	va_start( args, format );
	// _vscprintf doesn't count. terminating '\0'
	len = _vsctprintf( format, args )  + 1;
	buffer = (TCHAR*)malloc( len * sizeof(TCHAR) );
	_vstprintf_s( buffer, len, format, args ); // C4996
	// Note: vsprintf is deprecated; consider using vsprintf_s instead

	// ����־�������뵽�ļ���;
	fp.WriteString( CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S ")) );
	fp.WriteString(buffer);
	fp.WriteString(_T("\n"));

	// �ر��ļ����ͷ���Դ�����û�ԭ��������;
	free( buffer );
	_tsetlocale(LC_CTYPE, old_locale);
	free(old_locale);//��ԭ�����趨;
	fp.Close();
#else // ������MFC;
	FILE *fp = NULL;
	fp = _tfopen(szlogpath, _T("a+"));
	if ( fp == NULL )
		return;

	fseek(fp, 0, SEEK_END);

	// ��ʽ��ǰ������������;
	TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
	_tsetlocale(LC_CTYPE, _T("chs"));//�趨����;

	// ��ʽ����־����;
	va_list		args = NULL;
	int			len = 0;
	TCHAR		*buffer = NULL;
	va_start( args, format );
	// _vscprintf doesn't count. terminating '\0'
	len = _vsctprintf( format, args )  + 1;
	buffer = (TCHAR*)malloc( len * sizeof(TCHAR) );
	_vstprintf_s( buffer, len, format, args ); // C4996
	// Note: vsprintf is deprecated; consider using vsprintf_s instead

	// ����־�������뵽�ļ���;
	TCHAR szTime[36] = {0};
	_stprintf_s(szTime, _T("%04d-%02d-%02d %02d:%02d:%02d  "), gmtm.tm_year + 1900, gmtm.tm_mon+1, gmtm.tm_mday, gmtm.tm_hour, gmtm.tm_min, gmtm.tm_sec);
	fwrite(szTime, _tcslen(szTime)*sizeof(TCHAR), 1, fp);
	fwrite(buffer, _tcslen(buffer)*sizeof(TCHAR), 1, fp);
	fwrite(_T("\n"), sizeof(TCHAR), 1, fp);

	// �ر��ļ����ͷ���Դ�����û�ԭ��������;
	free( buffer );
	_tsetlocale(LC_CTYPE, old_locale);
	free(old_locale);//��ԭ�����趨;

	fclose(fp);

#endif
}