logger.c
#include "stdafx.h"
#include <cstdio>
#include "logger.h"
using namespace std;
//extern CServerInterface *ExtServer;
Logger ExtLogger;
extern string workingDir_;
LPCWSTR String2Wstring(const string s)
{
//wstring wszStr;//LPCWSTR wszStr;//wstring wszStr;
int len = s.length();
int lenbf = MultiByteToWideChar(CP_ACP, 0, s.c_str(), len, NULL, NULL);
LPWSTR lpwszStr = new wchar_t[lenbf+1];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), -1, lpwszStr, lenbf * sizeof(wchar_t));
lpwszStr[len] = 0;
return lpwszStr;
}
Logger::Logger(void)
/*:logName_("plugin_log.log")*/
{
}
Logger::~Logger(void)
{
sync_.Lock();
if (file_.is_open())
{
file_.flush();
file_.close();
}
sync_.Unlock();
}
void Logger::Initialize()
{
SYSTEMTIME st = {0};
GetLocalTime(&st);
char filename[32] = {0};
string tmp;
sync_.Lock();
logDir_ = workingDir_ + "logs";
CreateLogDir();
logFileDate_ = st.wDay;
sprintf_s(filename, 32, "%04d%02d%02d.log", st.wYear, st.wMonth, st.wDay);
tmp = filename;
logFileName_ = logDir_ + "\\" + tmp;
//--- check file
if (file_.is_open())
{
file_.close();
}
file_.open(logFileName_, fstream::out | fstream::app | fstream::ate);
sync_.Unlock();
}
void Logger::CreateLogDir()
{
LPCWSTR logdirs = String2Wstring(logDir_);
CreateDirectory(logdirs, NULL