
//  copyleft by jinzhongjie(xjinzhongjie@gmail.com).anyone who want change the code please tell writer.
//  Everyone is permitted to copy and distribute verbatim copies
//  of this license document, but changing it is not allowed.
#ifndef LOG_H_
#define LOG_H_

#ifdef __cplusplus

// #include <time.h>
#include <sys/timeb.h>
#include <iostream>
#include <fstream>
#include <queue>
#include <stdarg.h>
// #include <unordered_map>
// #include <thread>
// #include <pthread.h> 
#include "Locker.h"
//#include <mutex>
//#include <condition_variable>
#include <ctime>
#include <memory>
#include <string.h>

extern "C" {
	void *_run_thread_log(void *_pObject);
} 

class Log{
private:
	class LogMsg{
	private:
		struct tm ts;
		int milli;
		std::string message;
	public:
		LogMsg(struct tm&, int &, std::string&);
		friend std::ostream& operator<<(std::ostream&, const LogMsg&);
	};
	
	bool open;
	std::ofstream fout;
	//std::queue<std::shared_ptr<LogMsg>> msg_queue;
	std::queue<LogMsg*> msg_queue;
	// std::thread exec_thread;
	PthreadManager  exec_thread;

	Log(Log const&){};
	Log& operator=(Log const&);
	friend std::ostream& operator<<(std::ostream&, const LogMsg&);

protected:
	//std::mutex trans_mutex;
	// std::condition_variable run_cv;
	Mutex  trans_mutex;
	virtual void thread_handle();
	friend void* _run_thread_log(void *_pObject);
/*
	int nPolicy;
	int nScope;
	struct sched_param oParam;
	bool bRunning;	
	pthread_t nThreadID;
*/
	__declspec(dllexport) Log();

public:
	static Log& getInstance();
	void openLog(const char*);
	// void write( std::string& );
	__declspec(dllexport) void write(struct tm &, int &, std::string& );
	void time_write(std::string& );
	void closeLog(); 
};
#endif /* __cplusplus */

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

int log_init(const char *pFile);
void log_write(const char *pBuf);
void log_close(void);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif
