#include "RecordLog.h"
#include <io.h>
#include <direct.h>
#include<QFile>
#include<QTextStream>
#include<QDir>

//CRecordLog gRecordLog;
//
//bool g_bEnableLog=true;

//创建多级目录
bool my_create_dir(std::string path)
{
	int m = 0, n;
	std::string str1, str2;

	str1 = path;
	str2 = str1.substr(0, 2);
	str1 = str1.substr(3, str1.size());

	while (m >= 0)
	{
		m = str1.find('\\');

		str2 += '\\' + str1.substr(0, m);
		n = _access(str2.c_str(), 0); //判断该目录是否存在
		if (n == -1)
		{
			if (_mkdir(str2.c_str()) != 0)     //创建目录
			{
				return false;
			}
		}

		str1 = str1.substr(m + 1, str1.size());
	}

	return true;
}

DWORD WINAPI ThreadLogRun(LPVOID lpVoid)
{
	CRecordLog* logEntity = (CRecordLog*)lpVoid;
	while(logEntity->m_bRunning){
		logEntity->AutoWriteLog();
		Sleep(200);
		if(!logEntity->m_bRunning)break;
		Sleep(150);
		if(!logEntity->m_bRunning)break;
		Sleep(150);
		if(!logEntity->m_bRunning)break;
	}
	return 0;
}
HANDLE CRecordLog::CreateThread()
{
	DWORD threadId;
	m_ThreadHandle=::CreateThread(NULL, 0,	ThreadLogRun, (LPVOID)this, 0, &threadId);
	Q_ASSERT(m_ThreadHandle);
	return m_ThreadHandle;
}

CRecordLog::CRecordLog()
{
	m_bRunning = true;
	CreateThread();
}


CRecordLog::~CRecordLog()
{
	Exit();
}

void CRecordLog::AddLog(Log_Type type, const QString &msg)
{
	if(0 == m_ThreadHandle)return;
	QMutexLocker l(&m_AWLogMutex);
	//QByteArray utf8Array = msg.toUtf8();
	m_LogVec.push_back(_Tag_LOG( msg.toUtf8().data(), type));
}

void CRecordLog::LogInfo(const QString& msg)
{
	AddLog(Log_Type_Info, msg);
}

void CRecordLog::ProductLog(const QString& msg)
{
	AddLog(Log_Type_Product, msg);
}

void CRecordLog::LogError(const QString& msg)
{
	//LOG(ERROR)<<msg.toStdString().c_str();
	AddLog(Log_Type_Error, msg);
}

void CRecordLog::AutoWriteLog()
{
	QMutexLocker l(&m_AWLogMutex);
	if(m_LogVec.size()<=0)return;
    std::list<_Tag_LOG>   temp;
    temp.swap(m_LogVec);
	l.unlock();
	if (!temp.size())
		return;

	QString sLogFilePath = CreateFilePathCurDate("RecordLog");
	QFile file(sLogFilePath);
	if(!file.open(QIODevice::Append))
	{
		//LOG(INFO)<<ERR_TITTLE()<<"Record Log open file failed"<<sLogFilePath.toStdString();
		return;
	}

	QTextStream outStream(&file);
	for (auto iter=temp.begin();iter!=temp.end();iter++)
	{
		QString s = (*iter).m_msg.c_str();
		outStream<<(*iter).m_TimeStr.toString("yyyy-MM-dd hh:mm:ss zzz")<<(*iter).m_sType.c_str()<<s<<"\n";
	}

	file.close();
}

void CRecordLog::Exit()
{
	m_bRunning = false;
	AutoWriteLog();
	if(0 == m_ThreadHandle)
		return;
	
	
	WaitForSingleObject(m_ThreadHandle, 2000);
	CloseHandle(m_ThreadHandle);
	m_ThreadHandle = 0;
}

QString CRecordLog::GetFileNameByType(Log_Type type)
{
	return QString();
}

QString CRecordLog::CreateFilePathCurDate(const QString& parentFolder)
{
	QDateTime curDateTime = QDateTime::currentDateTime();

	QString folder = curDateTime.toString("yyyyMM");
	QString fileName = curDateTime.toString("yyyy-MM-dd")+".txt";
	QDir curPath = QDir(QDir::currentPath());
	curPath.cdUp();

	QString path = curPath.absoluteFilePath(parentFolder + "/" + folder);
	curPath.setPath(path);
	if(!curPath.exists()){
		if(false == curPath.mkpath(path)){
			//LOG(INFO)<<ERR_TITTLE()<<"Record Log open file failed"<<path.toStdString();
		}
	}
	
	QString filePath = curPath.absoluteFilePath(fileName);

	return filePath;
}

QString CRecordLog::GetPicNameByCurTime()
{
	QString fullName;
	return fullName;
}
