#include "LogManager.h"
#include <stdarg.h>

LogManager::~LogManager(){}
LogManager::LogManager(){
	
}

/// Get the one and only instance of the LogManager.
LogManager &LogManager::getInstance(){
	if(pLog == NULL){
		pLog = new LogManager();
	}
	static LogManager LogM;
	return LogM;
}

LogManager* LogManager::pLog=NULL;

/// Startup the LogManager (open logfile "dragonfly.h").
/// append = true to add to logfile, flush = true if flush() afer each write.
//only care about writing to the logfile
int LogManager::startUp(bool append, bool flush){
	string fname=LOGFILE_NAME;
	//this->fp=fopen(fname.append(getTimeString()).c_str(),"w+");
	//this->fp=fopen_s(LOGFILE_NAME,"w+");
	return fopen_s(&fp,LOGFILE_NAME,"w+");
	//return 0;
}

/// Shutdown the LogManager (close logfile).
void LogManager::shutDown(){
	fclose(this->fp);
	//this->~LogManager();
}

/// Write to logfile.
/// Supports printf() formatting of strings.
/// Return number of bytes written, -1 if error.
int LogManager::writeLog(const char *fmt, ...){
	char buff[1024];
	//sprintf(buff,fmt);
	va_list args;
	va_start (args, fmt);
	vsprintf (buff,fmt, args);
	va_end (args);
	return fputs (buff,this->fp);
}