#include "Logini.h"

bool LogIni::m_IsWRLog = false;

void LogIni::GetLogToIni(string contentType, wstring cmdContent)
{
	if (m_IsWRLog == false)
		return;
	//文件:存放在文档里
	wstring wPath = FolderCore::SetSHGetFolderPath(CSIDL_PERSONAL) + L"\\" + L"Installer_Log.ini";
	wchar_t* ptrPath = _wcsdup(wPath.c_str());
	std::string sPath = CharConvert::GetMultiByteW(ptrPath);

	time_t t;
	tm tmp;
	time(&t);
	localtime_s(&tmp, &t);
	string  strTime = to_string(tmp.tm_year + 1900) + "/" + to_string(tmp.tm_mon + 1) + "/" + to_string(tmp.tm_mday) + "--" + to_string(tmp.tm_hour) + ":" + to_string(tmp.tm_min) + ":" + to_string(tmp.tm_sec);
	char* chTime = (LPSTR)(LPCTSTR)strTime.c_str();
	CharConvert::GetWideCharW(chTime);

	wstring cmdStr = cmdContent;
	wchar_t* cmdptr = _wcsdup(cmdStr.c_str());
	char* cmdCh = CharConvert::GetMultiByteW(cmdptr);

	char* chcontentType = (LPSTR)(LPCTSTR)contentType.c_str();

	DWORD pidIni = WritePrivateProfileStringA(chTime, chcontentType, cmdCh, sPath.c_str());
}

void LogIni::GetLogToIniMaps(std::map<wstring, wstring> maps)
{
	if (m_IsWRLog == false)
		return;
	//路径
	wstring wPath = FolderCore::SetSHGetFolderPath(CSIDL_PERSONAL) + L"\\" + L"Installer_Log.ini";
	//当前系统时间
	time_t t;
	tm tmp;
	time(&t);
	localtime_s(&tmp, &t);
	wstring  strTime = to_wstring(tmp.tm_year + 1900) + L"/" + to_wstring(tmp.tm_mon + 1) + L"/" + to_wstring(tmp.tm_mday) + L"--" + to_wstring(tmp.tm_hour) + L":" + to_wstring(tmp.tm_min) + L":" + to_wstring(tmp.tm_sec);
	maps.insert(pair<wstring,wstring>(L"-----", L"----"));

	std::map<wstring, wstring>::iterator iterRead;
	for (iterRead = maps.begin(); iterRead != maps.end(); iterRead++)
	{
		wstring cmdStr = iterRead->second;
		wchar_t* cmdptr = _wcsdup(cmdStr.c_str());

		DWORD pidIni = WritePrivateProfileString(strTime.c_str(), iterRead->first.data(), cmdptr, wPath.c_str());
	}

}

#pragma region ini读写操作

DWORD LogIni::WriteIntToini(LPCSTR lpAppNam, LPCSTR lpKeyName, INT nDefault, LPCSTR lpFileName)
{
	char _Buf[64];
	_CSTD sprintf_s(_Buf, sizeof(_Buf), "%u", nDefault);
	return WritePrivateProfileStringA(lpAppNam, lpKeyName, _Buf, lpFileName);
}

DWORD LogIni::ReadIntFromini(LPCSTR lpAppNam, LPCSTR lpKeyName, INT nDefault, LPCSTR lpFileName)
{
	return  GetPrivateProfileIntA(lpAppNam, lpKeyName, nDefault, lpFileName);
}

#pragma endregion

