写日志文件

#include<iostream>

#include<windows.h>
#include <atlstr.h>
#include<iostream>
#include<atltime.h>
#include<io.h>
#include<stdio.h>

using namespace std;

#define BUFSIZE 255

std::wstring AnsiToUnicode(const std::string& strA)
{
int unicodeLen = ::MultiByteToWideChar(CP_ACP, 0, strA.c_str(), -1, NULL, 0);
if (unicodeLen <= 0)
{
return (L"");
}
wchar_t* pUnicode = new wchar_t[unicodeLen + 1];
memset(pUnicode, 0, (unicodeLen + 1)*sizeof(wchar_t));
::MultiByteToWideChar(CP_ACP, 0, strA.c_str(), -1, (LPWSTR)pUnicode, unicodeLen);
std::wstring rt = pUnicode;
delete[] pUnicode;
pUnicode = NULL;
return rt;
}

std::string string_To_UTF8(const std::string & str)
{
int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
wchar_t * pwBuf = new wchar_t[nwLen + 1];
ZeroMemory(pwBuf, nwLen * 2 + 2);
::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);
int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
char * pBuf = new char[nLen + 1];
ZeroMemory(pBuf, nLen + 1);
::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
std::string retStr(pBuf);
delete []pwBuf;
delete []pBuf;
pwBuf = NULL;
pBuf = NULL;
return retStr;
}

std::string UnicodeToUTF8(const std::wstring& str)
{
int iTextLen = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, NULL, 0, NULL, NULL);
if (iTextLen <= 0)
{
return "";
}

char* pElementText = new char[iTextLen + 1];
memset(pElementText, 0, sizeof(char) * (iTextLen + 1));
::WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL);
std::string strText = pElementText;
delete[] pElementText;
pElementText = NULL;
return strText;
}


std::wstring Utf8ToUnicode(const std::string &strUTF8)
{
int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0);
if (len == 0)
{
return L"";
}
wchar_t *pRes = new wchar_t[len];
if (pRes == NULL)
{
return L"";
}
MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, pRes, len);
pRes[len - 1] = L'';
std::wstring result = pRes;
delete [] pRes;
return result;
}


//C:UsersAdministratorAppDataLocalTempUpLog2021052520.slog

BOOL WriteLogInfo(string contant)
{
HANDLE g_hLog = INVALID_HANDLE_VALUE;
CTime t = CTime::GetCurrentTime();

CString datestr = t.Format(_T("[%Y-%m-%d %H:%M:%S] "));
std::string datestr_ = CT2A(datestr.GetBuffer()); //转化为非unicode


//日志样例 [2021-05-25 20:31:52] 试一试
string strJson;
strJson += datestr_ ;
strJson += contant;
strJson += " ";

std::wstring wstr = AnsiToUnicode(strJson);
std::string strUtf8 = UnicodeToUTF8(wstr);

DWORD dwBufSize=BUFSIZE;
TCHAR lpPathBuffer[BUFSIZE];
DWORD dwRetVal = GetTempPath(dwBufSize, // length of the buffer
lpPathBuffer); // buffer for path

if (dwRetVal > dwBufSize || (dwRetVal == 0))
{
return FALSE;
}

CString strLogPath ;
strLogPath.Format(_T("%s%s"),lpPathBuffer,_T("\UpLog"));
if(!PathIsDirectory(strLogPath))//判断路径是否存在
{
CreateDirectory(strLogPath,NULL);
}

CString curdata = t.Format(_T("%Y%m%d%H"));
CString strLog = strLogPath +_T("\")+curdata+_T(".slog");

g_hLog = CreateFile(strLog,GENERIC_WRITE, // open for writing
FILE_SHARE_READ|FILE_SHARE_WRITE, // share
NULL, // default security
OPEN_ALWAYS, // open existing or create a new one
FILE_ATTRIBUTE_NORMAL, // normal file
NULL);

DWORD dwWriten;

if(g_hLog != INVALID_HANDLE_VALUE)
{
DWORD dwHigh = 0;
GetFileSize(g_hLog,&dwHigh);
if(dwHigh == 0)
{
SetFilePointer(g_hLog,0,NULL,FILE_END);
}
else
{
SetFilePointer(g_hLog,0,NULL,FILE_BEGIN);
}
}

if(!WriteFile(g_hLog,strUtf8.c_str(),strUtf8.length(),&dwWriten,NULL))
{
::CloseHandle(g_hLog);
return FALSE;
}
::CloseHandle(g_hLog);
return TRUE;
}

int main()
{
WriteLogInfo("试一试");
return 0;
}

原文地址:https://www.cnblogs.com/roea1/p/14810517.html