#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <termio.h>
#include <sys/stat.h>
#include <sys/io.h>
#include <sys/utsname.h>
#include <sys/ipc.h>
#include <sys/shm.h>
 #include <stdarg.h>
 
static int new_file_flag = 1;
static char system_name[128]="dba";
static char log_file_name[256] = {0};
static char log_path[256]="/var/log";
static int log_max_files=100;    
static int max_file_size=10240; //10M

FILE		*logFile_fp = NULL;


void logWriter_read_param()
{
	char cnf_file[]="./conf/logWriter.conf";
	char info_str[512], *buf_ptr;;
	FILE	*fp=NULL;
	
	fp = fopen(cnf_file,"r");


	if(fp == NULL)
		return;

	while (!feof(fp))
	{
		info_str[0] = '\n';
		fgets(info_str, 500, fp);
		if (info_str[0] == '#' || info_str[0] == '\n')
			continue;
		buf_ptr = info_str;
		
		if(!strncasecmp(info_str,"sysName=",8))
		{
			strsep(&(buf_ptr), "=\n");
			snprintf(system_name, sizeof(system_name), "%s", strsep(&(buf_ptr), "=\n") );
		}
		if(!strncasecmp(info_str,"logPath=",8))
		{
			strsep(&(buf_ptr), "=\n");
			snprintf(log_path, sizeof(log_path), "%s", strsep(&(buf_ptr), "=\n") );
		}
		if(!strncasecmp(info_str,"logMaxFiles=",12))
		{
			log_max_files = atoi(&info_str[12]);
			if ( log_max_files > 500 )
			{
				log_max_files = 500;
			}
			continue;
		}
		if(!strncasecmp(info_str,"logMaxFileSize(KB)=",19))
		{
			max_file_size = atoi(&info_str[19]);
			// Set the min file size to 100KB to control the CPU loading
			if (max_file_size < 100)
			{
				max_file_size = 100;
			}
			continue;
		}
	}
	
	printf(" sysName = %s\n logPath = %s\n logMaxFiles = %d\n logMaxFileSize(KB) = %d\n", \
		system_name, log_path, log_max_files, max_file_size);
	}


int logWriter_init()
{

	logWriter_read_param();

  if(access(log_path,0) == -1 )
  {
    if (mkdir(log_path,0777))
    {
       printf("creat log_path %s failed!!!", log_path );
    }
  } 

	snprintf(log_file_name, sizeof(log_file_name), "%s/%s.log", log_path, system_name);
	logFile_fp = fopen ( log_file_name, "a" );

	printf("logWriter Init Complete!\n");
	
	return 1;
}

int  logWriter_write_log_to_disk(char *log_record)
{
	short	len;
	if(log_record == NULL)
		return 0;
		
	len = strlen(log_record);
	fwrite ( log_record, len, 1, logFile_fp );
	fflush ( logFile_fp ); // force to write to harddisk
	
	return 1;
}

int  myLOG_D( const char *fmt, ...)
{ 
	va_list        apa;
	char        buf[10240];
//return 0;
	va_start(apa, fmt);
	vsprintf(buf, fmt, apa);
	va_end(apa);
	
	logWriter_write_log_to_disk(buf);
	
	return 1;
}


void logWriter_rename_log_file()
{
	int i;
	char old_file_name[256], new_file_name[256];

	if (log_max_files == 0)
	{
		return;
	}

	for ( i=(log_max_files-1); i>0; i-- )
	{
		snprintf(old_file_name, sizeof(old_file_name), "%s/%s.log.%d", log_path, system_name, i);
		if (1 == i)
		{
			snprintf(new_file_name, sizeof(new_file_name), "%s/%s.log", log_path, system_name);
		}
		else
		{
			snprintf(new_file_name, sizeof(new_file_name), "%s/%s.log.%d", log_path, system_name, (i-1) );
		}
		if ( access(new_file_name, R_OK) == 0 )
		{
			rename(new_file_name, old_file_name);
		}
	}
}

int logWriter_rt()
{
	struct stat buf;
	char	  real_path[128];
	
	if (log_file_name[0] != 0)
	{
		realpath(log_file_name, real_path);
		stat(real_path, &buf);
		if ( buf.st_size >= (max_file_size*1024) )
		{
			new_file_flag		= 1;
			logWriter_rename_log_file();
		}
	}

	if ( 1 == new_file_flag )
	{
		if (logFile_fp != NULL)
		{
			fclose ( logFile_fp );
			logFile_fp = NULL;
		}
		logFile_fp = fopen ( log_file_name, "a" );
		new_file_flag		= 0;
	}
			
	return 1;
}

//int main(int argc, char **argv)
//{
//	logWriter_init();
//	
//	while(1)
//	{
//		logWriter_rt();
//		usleep(10);
//	}
//	
//	return 1;
//}
