#pragma once
//日志还需要改进的地方包括：1.线程输出；2.日志内容从缓冲区申请内存放入消息，并还给缓冲区
#include <Windows.h>
#include <string>
#include <memory>
#include <time.h>
#include <vector>
#include <mutex>
#include <log4cpp\Category.hh>
#include <log4cpp\Appender.hh>
#include <log4cpp\FileAppender.hh>
#include <log4cpp\OstreamAppender.hh>
#include <log4cpp\Layout.hh>
#include <log4cpp\SimpleLayout.hh>
#include <log4cpp\BasicLayout.hh>
#include <log4cpp\PatternLayout.hh>
#include <log4cpp\Priority.hh>
#include <log4cpp\RollingFileAppender.hh>
#include <log4cpp\DailyRollingFileAppender.hh>

#ifdef _WIN32
#include <direct.h>  
#include <io.h>  
#elif _LINUX
#include <stdarg.h>  
#include <sys/stat.h>  
#endif  
#ifdef _WIN32
#define ACCESS _access  
#define MKDIR(a) _mkdir((a))  
#elif _LINUX  
#define ACCESS access  
#define MKDIR(a) mkdir((a),0755)  
#endif  

#define MAX_FILE_SIZE 200*1024*1024
#define MAX_BACKUP_COUNTS 20

enum FILE_TYPE {
	RAW_S = 0,
	SYNC_S,
	PARSE_S,
	PARSE_CARD_S,
	PARSE_LIGHT_S,
	DIST_S,
	DISTEX_S,
	DISTXX_S,
	DIST_TOF_S,
	LOCATE_TOF_S,
	JSON_S,
	JSON_R,
	SQL_S,
	SYS_S,
	KALMAN_S,
	ALGO_S,
	VIBRATE_S,
	STARTUP_RATIO,
    RAW_LOCATION_DATA,
    POSITION_ORIGIN,
    POSITION_OPTIMIZED,
	MOVING_READER_PARSE_DATA,
	FILE_TYPE_TOTAL
};

const std::string my_log_file_name[] = {
	"raw_s",
	"sync_s",
	"parse_s",
	"parse_card_s",
	"parse_light_s",
	"dist_s",
	"distex_s",
	"distxx_s",
	"dist_tof_s",
	"locate_tof_s",
	"json_s",
	"json_r",
	"sql_s",
	"sys_s",
	"kalman_s",
	"algo_s",
	"vibrate_s",
	"startup_ratio",
    "position_raw",
    "position_origin",
    "position_ajusted",
	"moving_reader_parse_s"
};

const std::string log_file_pattern[] = {
	"%m%n",		//消息，换行符
	"[%d] : %m%n"
};

class MyLog
{
public:
	MyLog(FILE_TYPE type);
	MyLog(FILE_TYPE type,bool status);
	~MyLog();
public:
	int WriteLog(const std::string msg);
	int WriteLog(const char* pMsg,...);

	int test();

public:
	bool flag;
private:
	bool status;		//是否输出时间

private:
	//以下函数考虑移出本类
	std::string GetExeDir();		//获得exe程序所在目录
	std::string TCHAR2string(TCHAR* tch);
	std::string int2string(int value);
	std::string getCurTime();
public:
	int SetAppender(const char* filename);
	int SetAppender(std::string filename);
	int SetLayout(bool status);
	static bool IsDirExist(std::string path);		//检查目录是否存在
	static bool CreateDir(const char* path);		//创建目录
	FILE_TYPE GetFileType(){return fileType;}

	bool GetStatus(){return status;}
private:
	int InitLogConfig();

	long GetFileSize();

	std::string GetFileName();

private:
	std::string strExeDir;

private:
	FILE_TYPE fileType;

	std::string curFileName;
	int curFileCount;
	std::string m_Lasttime;
	std::string m_Curtime;
	log4cpp::Appender* pAppender;
	log4cpp::PatternLayout* pLayout;
	log4cpp::Category* pCategory;
};