#include "StdAfx.h"
#include "Global.h"
#include "CritSection.h"
#include <locale.h>
// PathFileExists头文件;
#include <shlwapi.h>
#pragma comment(lib,"shlwapi.lib")

namespace Global
{

	TCHAR g_szModulePath[MAX_PATH] = _T("");			// 软件目录;
	TCHAR g_szModuleFileName[MAX_PATH] = _T("");		// 软件名称;
	TCHAR g_szConnectString[MAX_PATH] = _T("");			// DB连接串;
	// 服务器信息;
	TCHAR g_szDBSource[MAX_PATH] = _T("");				// 数据库源(服务所在IP或计算机名);
	TCHAR g_szDBSourcePort[MAX_PATH] = _T("");			// 数据库源端口;
	DWORD g_dwDBServerPort = 0;							// 数据库源端口;
	TCHAR g_szDBAccount[MAX_PATH] = _T("");				// 数据库登录用户;
	TCHAR g_szDBPassWord[MAX_PATH] = _T("");			// 数据库登录密码;
	TCHAR g_szDBName[MAX_PATH] = _T("");				// 数据库名称;	
	DWORD g_dwDBPoolMaxCount = 1;						// 数据库连接池最大对象数;
	DWORD g_dwDBPoolDef = 1;							// 数据库连接池默认对象数;

	/************************************************************************/
	/*  函数：[5/20/2016 IT];
	/*  描述：;
	/*  参数：;
	/*  	[IN] ：;
	/*  	[OUT] ：;
	/*  	[IN/OUT] ：;
	/*  返回：void;
	/*  注意：;
	/*  示例：;
	/*
	/*  修改：;
	/*  日期：;
	/*  内容：;
	/************************************************************************/
	int GetIniInfo(const TCHAR *szPath, const TCHAR *szIniName)
	{
		TCHAR szDrive[_MAX_DRIVE] = {0};
		TCHAR szDir[_MAX_DIR] = {0};
		TCHAR szFName[_MAX_FNAME] = {0};
		TCHAR szExt[_MAX_EXT] = {0};
		::GetModuleFileName(NULL, g_szModulePath, sizeof(g_szModulePath) / sizeof(TCHAR));
		_stprintf_s(g_szModulePath, MAX_PATH, _T("%s"), g_szModulePath);

		_tsplitpath_s(g_szModulePath, szDrive, szDir, szFName, szExt);
		_tcscpy_s(g_szModulePath, szDrive);
		_tcscat_s(g_szModulePath, szDir);

		// -----------------------------------------------------//
		TCHAR szIniPath[MAX_PATH] = {0};
		if (szPath != NULL && szIniName != NULL)
			_stprintf_s(szIniPath, _T("%s%s"), szPath, szIniName);
		else
			_stprintf_s(szIniPath, _T("%sServiceInfo.ini"), g_szModulePath);

		HANDLE hFile = CreateFile(szIniPath, 0/*GENERIC_READ*/, 0, NULL, OPEN_EXISTING, 0, NULL);
		if (ERROR_FILE_NOT_FOUND == GetLastError())
		{
			return -1;
		}
		CloseHandle(hFile);

		// 获取服务器端信息;
		GetPrivateProfileString(_T("DatabaseInfo"), _T("dbSource"), _T(""), g_szDBSource, MAX_PATH, szIniPath);
		g_dwDBServerPort = GetPrivateProfileInt(_T("DatabaseInfo"), _T("dbServerPort"), 0, szIniPath);
		GetPrivateProfileString(_T("DatabaseInfo"), _T("dbAccount"), _T(""), g_szDBAccount, MAX_PATH, szIniPath);
		GetPrivateProfileString(_T("DatabaseInfo"), _T("dbPassWord"), _T(""), g_szDBPassWord, MAX_PATH, szIniPath);
		GetPrivateProfileString(_T("DatabaseInfo"), _T("dbName"), _T(""), g_szDBName, MAX_PATH, szIniPath);

		g_dwDBPoolMaxCount = GetPrivateProfileInt(_T("DatabaseInfo"), _T("dbpoolMaxObj"), 1, szIniPath);
		g_dwDBPoolDef = GetPrivateProfileInt(_T("DatabaseInfo"), _T("dbpoolDef"), 1, szIniPath);

		if (g_dwDBServerPort != 0)
		{
			if ( _tcscmp(g_szDBAccount, _T("")) == 0 )
				_stprintf_s(g_szConnectString, DB_SW_CONN_WITH_PORT, g_szDBSource, g_dwDBServerPort,g_szDBName);
			else
				_stprintf_s(g_szConnectString, DB_SS_CONN_WITH_PORT,g_szDBSource, g_dwDBServerPort, g_szDBName, g_szDBAccount, g_szDBPassWord);
		}
		else
		{
			if ( _tcscmp(g_szDBAccount, _T("")) == 0 )
				_stprintf_s(g_szConnectString, DB_SW_CONN_WITHOUT_PORT, g_szDBSource, g_szDBName);
			else
				_stprintf_s(g_szConnectString, DB_SS_CONN_WITHOUT_PORT,g_szDBSource, g_szDBName, g_szDBAccount, g_szDBPassWord);
		}

		return 0;
	}

	/************************************************************************/
	/*  函数：WriteTextLog[7/28/2016 IT];
	/*  描述：写文本日志;
	/*  参数：;
	/*  	[IN] ：;
	/*  返回：void;
	/*  注意：;
	/*  示例：;
	/*
	/*  修改：;
	/*  日期：;
	/*  内容：;
	/************************************************************************/
	void WriteTextLog(const TCHAR *format, ...)
	{
		try
		{
			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);
			}

			_stprintf_s(szlogpath, _T("%s%s.txt"), szModulePath, szFna);

			// 打开或创建文件;
			CStdioFile fp;
			if (PathFileExists(szlogpath))
			{
				if (fp.Open(szlogpath, CFile::modeWrite) == FALSE)
				{
					return;
				}
				fp.SeekToEnd();
			}
			else
			{
				if ( !fp.Open(szlogpath, CFile::modeCreate | CFile::modeWrite) )
					return;
			}

			// 格式化前设置语言区域;
			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_p( format, args );
			if ( len == -1 )
			{
				goto clear;
			}

			len++;
			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 );
clear:
			_tsetlocale(LC_CTYPE, old_locale);
			free(old_locale);//还原区域设定;
			fp.Close();
		}
		catch (CException *e)
		{
			e->ReportError();
			e->Delete();
		}
	}

};