#include "log.h"
#include "common.h"


struct log_s g_log_env;

int log_print(int index)
{
	char file_name[200];
	char now_time[12];
	
	if(index == 0)
	{
		g_log_env.log_flag = 1;
		GetCurrentTime(now_time);
		
		sprintf(file_name, "%s_%02d_%02d_%02d_%02d_%02d_%02d.log", 
						g_log_env.log_file,
						now_time[0],
						now_time[1],
						now_time[2],
						now_time[3],
						now_time[4],
						now_time[5]);						
		g_log_env.log_fp = fopen(file_name, "a+");
		if(g_log_env.log_fp != NULL)
		{
			fprintf(g_log_env.log_fp, "Rest Proxy: Start @%s\r\n", GetAsciiTime());
		}
	}
	else
	{
		if(g_log_env.log_fp != NULL)
			fclose(g_log_env.log_fp);
		
		GetCurrentTime(now_time);
		
		sprintf(file_name, "%s_%02d_%02d_%02d_%02d_%02d_%02d.log", g_log_env.log_file,
						now_time[0],
						now_time[1],
						now_time[2],
						now_time[3],
						now_time[4],
						now_time[5]);						
		g_log_env.log_fp = fopen(file_name, "a+");
		if(g_log_env.log_fp != NULL)
		{
			fprintf(g_log_env.log_fp, "< Log file opened @%s >\r\n", GetAsciiTime());
		}
	}

	return 1;
}

int rest_proxy_open_log(int index)
{
	char file_name[200];
	char now_time[12];
	
	if(index == 0)
	{
		g_log_env.log_flag = 1;
		GetCurrentTime(now_time);
		
		sprintf(file_name, "%s_%02d_%02d_%02d_%02d_%02d_%02d.log", 
						g_log_env.log_file,
						now_time[0],
						now_time[1],
						now_time[2],
						now_time[3],
						now_time[4],
						now_time[5]);						
		g_log_env.log_fp = fopen(file_name, "a+");
		if(g_log_env.log_fp != NULL)
		{
			fprintf(g_log_env.log_fp, "Rest Proxy: Start @%s\r\n", GetAsciiTime());
		}
	}
	else
	{
		if(g_log_env.log_fp != NULL)
			fclose(g_log_env.log_fp);
		
		GetCurrentTime(now_time);
		
		sprintf(file_name, "%s_%02d_%02d_%02d_%02d_%02d_%02d.log", g_log_env.log_file,
						now_time[0],
						now_time[1],
						now_time[2],
						now_time[3],
						now_time[4],
						now_time[5]);						
		g_log_env.log_fp = fopen(file_name, "a+");
		if(g_log_env.log_fp != NULL)
		{
			fprintf(g_log_env.log_fp, "< Log file opened @%s >\r\n", GetAsciiTime());
		}
	}

	return SUCCESS;
}

