#include "pch.h"
#include "DbgLog.h"
#include <ctime>
#include "shlwapi.h"
#pragma comment(lib, "shlwapi")
//char DbgLog::LogPath[MAX_PATH] = "Printer_log.txt";
ofstream logfile;
//streambuf *outstream;
DbgLog::DbgLog(){}
DbgLog::~DbgLog(){}
void DbgLog::StartLog(TCHAR *LogName)
{
/*
if(OFFONPRINTF)
{
TCHAR LogPath[MAX_PATH] = { 0 };
GetModuleFileName(NULL, LogPath, sizeof(LogPath));
PathRemoveFileSpec(LogPath);
PathAppend(LogPath, LogName);
logfile.open(LogPath, ios::app | ios::out);
//outstream = std::cout.rdbuf(logfile.rdbuf());
time_t t = time(0);
char tmp[64] = "";
tm newtime;
localtime_s(&newtime, &t);
strftime(tmp, sizeof(tmp), "%Y/%m/%d %H:%M:%S", &newtime);
logfile << "-------------------------------------" << tmp << "-------------------------------------" << endl;
}
*/
if(OFFONPRINTF)
{
TCHAR LogPath[MAX_PATH] = { 0 };
TCHAR LogFileName[MAX_PATH] = { 0 };
GetModuleFileName(NULL, LogPath, sizeof(LogPath));
PathRemoveFileSpec(LogPath);
PathAppend(LogPath, L"project");
CreateDirectory(LogPath, NULL);
PathAppend(LogPath, L"log");
CreateDirectory(LogPath, NULL);
PathAppend(LogPath, LogName);
CreateDirectory(LogPath, NULL);
SYSTEMTIME sys;
GetLocalTime(&sys);
wsprintf(LogFileName, L"%04d%02d%02d%02d%02d.txt", sys.wYear, sys.wMonth, sys.wDay, sys.wHour, sys.wMinute);
PathAppend(LogPath, LogFileName);
logfile.open(LogPath, ios::app | ios::out);
}
}
void DbgLog::EndLog()
{
if(OFFONPRINTF)
{
//std::cout.rdbuf(outstream);
logfile.close();
}
}
void DbgLog::DbgPrint(CHAR* CppName, CHAR *FunName, INT CodeLine,
CHAR* format, ...)
{
if(OFFONPRINTF)
{
char Buffer[1024] = {0};
va_list argp;
va_start(argp, format);
vsprintf_s(Buffer, format, argp);
va_end(argp);
logfile << TimeStrLog() << "[" << GetCurrentThreadId() << "][" << CppName << "][" << FunName << "][" << CodeLine << "] ";
logfile << Buffer;
logfile << flush;
}
}
void DbgLog::DbgPrint(CHAR* CppName, CHAR *FunName, INT CodeLine,
TCHAR* format, ...)
{
if (OFFONPRINTF)
{
TCHAR Buffer[1024] = { 0 };
CHAR cBuffer[2048] = { 0 };
va_list argp;
va_start(argp, format);
vswprintf_s(Buffer, format, argp);
va_end(argp);
wchar_to_char(cBuffer, Buffer, 2048);
logfile << TimeStrLog() << "[" << GetCurrentThreadId() << "][" << CppName << "][" << FunName << "][" << CodeLine << "] ";
logfile << cBuffer;// Buffer;
logfile << flush;
}
}
string DbgLog::TimeStrLog()
{
SYSTEMTIME sys;
GetLocalTime(&sys);
char TS[255] = "";
sprintf_s(TS, 255, "%04d-%02d-%02d %02d:%02d:%02d.%04d ", sys.wYear, sys.wMonth, sys.wDay, sys.wHour, sys.wMinute, sys.wSecond, sys.wMilliseconds);
return &TS[0];
}
int DbgLog::wchar_to_char(char *strGBK, LPCWSTR FileName, int strlen)
{
int len = WideCharToMultiByte(CP_ACP, 0, FileName, -1, NULL, 0, NULL, NULL);
if(len > strlen){
return -1;
}
WideCharToMultiByte(CP_ACP, 0, FileName, -1, strGBK, len, NULL, NULL);
return len;
}cpp文件也要改吧
最新发布