/**
* @file common_itk_util.cpp
* @brief itk warpper utility function 
* @author James
* @history
* ===================================================================================
* Date             Name                   Description of Change
* 18-July-2008     Ray                 
*/
//#include <tc/tc.h>
#include <tc/tc_util.h>
#include <tc/tc_arguments.h>
#include <base_utils/mem.h>
#include <fclasses/tc_string.h>

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string>
using namespace std;
//
#ifdef WIN32
#include <io.h>
#include <direct.h>
#else
#include <unistd.h>
#endif
//
#include "tc_log.h"

#define ARGS_LENGTH 200
#define MAX_PRINTLINE_LENGTH 2000
#define MAX_PATH_LENGTH 2000
#define MAX_ARGUMENT_LENGTH 400
#define MAX_PARAMNAME_LENGTH 50
#define MAX_FILE_EXT_LENGTH 10
#define TRUE_FLAG 1
#define FALSE_FLAG 0
#define DETAILLOG 1

#define DOFREE(obj)								\
{												\
	if(obj)										\
	{											\
		MEM_free(obj);							\
		obj = NULL;								\
	}											\
}


FILE* log_file = NULL;
char *log_path = NULL;

void CreateTcLogFile(const char* logFileName)
{
	int i=0;

	log_file = NULL;
	if (log_path) {
		MEM_free(log_path);
		log_path = NULL;
	}

	log_path = (char*)MEM_alloc((strlen(logFileName) + 1) * sizeof(char*));
	strcpy(log_path, logFileName);

	//get lidy_logFileName
	//sprintf(lidy_logFileName, "%s", lidy_logFileName);
	printf("log file name: %s\n", logFileName);


	//create log file
	if((log_file = fopen(logFileName, "a"))==NULL)
	{
		printf("log file create failed\n");
	}

}

void WriteTcLog(const char* format, ...)
{
	va_list arg;
	char tmp[8192];

	if(log_file)
	{
		//get the message
		memset(tmp, 0, sizeof(tmp));
		va_start(arg, format);
		vsprintf(tmp, format, arg);
		va_end(arg);

		//----------print to command window for trace--------//
		printf("%s\n", tmp);
			
		//print message to log file
		fprintf(log_file, "%s\n", tmp);
		fflush(log_file);
	}
	else
	{
		printf("*!Error!*: Log File Not Exist\n");
	}
}

void WriteTcLog2(string msg) {
	if (log_file) {
		printf("%s\n", msg.c_str());
		fprintf(log_file, "%s\n", msg.c_str());
		fflush(log_file);
	}
	else {
		printf("*!Error!*: Log File Not Exist\n");
	}
}

void CloseTcLog(void)
{
	if(log_file)
	{
		fclose(log_file);
		log_file = NULL;
	}

	if (log_path) {
		MEM_free(log_path);
		log_path = NULL;
	}
}

void log_open(void) {
	if (log_path) {
		if ((log_file = fopen(log_path, "a")) == NULL) {
			printf("log file create failed\n");
		}
	}
}

void log_close(void) {
	if (log_file) {
		fclose(log_file);
		log_file = NULL;
	}
}

string getNum(int num) {
	char str[3] = "";
	sprintf(str, "%02d", num);
	return str;
}

string getNow(char* format) {
	time_t now = time(NULL);
	tm* tm_t = localtime(&now);
	string str = "";
	if (tc_strcmp(format, "yyyyMMdd") == 0) {
		str.append(to_string(tm_t->tm_year + 1900)).append(getNum(tm_t->tm_mon + 1)).append(getNum(tm_t->tm_mday));
	}
	else if (tc_strcmp(format, "LOG") == 0) {//yyyy-MM-dd HH:mi:ss
		str.append(to_string(tm_t->tm_year + 1900)).append("-")
			.append(getNum(tm_t->tm_mon + 1)).append("-")
			.append(getNum(tm_t->tm_mday)).append(" ")
			.append(getNum(tm_t->tm_hour)).append(":")
			.append(getNum(tm_t->tm_min)).append(":")
			.append(getNum(tm_t->tm_sec));
	}
	else if (tc_strcmp(format, "yyyyMMddHHmmss") == 0) {//yyyy-MM-dd HH:mi:ss
		str.append(to_string(tm_t->tm_year + 1900))
			.append(getNum(tm_t->tm_mon + 1))
			.append(getNum(tm_t->tm_mday))
			.append(getNum(tm_t->tm_hour))
			.append(getNum(tm_t->tm_min))
			.append(getNum(tm_t->tm_sec));
	}
	return str;
}

string getDateString(date_t date) {
	string str = "";
	str.append(to_string(date.year)).append("-").
		append(to_string(date.month + 1)).append("-").
		append(to_string(date.day)).append(" ").
		append(to_string(date.hour)).append(":").
		append(to_string(date.minute)).append(":").
		append(to_string(date.second));
	return str;
}
