/*************************************************
File name:	cdrlog.c
Author: 	
Version:	9:00:00 
Date:  		2002-7-2
Description:该文件定义了cdrCollector模块日志操作函数
			

			
			
												
History:               
No. 		
Author:		  
Date:		 
Version:	   
Description:
*************************************************/

#include "cdr.h"

 static FILE *log_fp;

extern BYTE iCdrNum;

extern cdr_src CdrSet[CDR_SRC_NUM];

/*************************************************
Function:        // cdrlog_init
Description:     // 打开日志文件，以便写入运行日志
Calls:           // 
Called By:       // cdrmain.c; cdrdb.c;
Table Accessed:  // 
Table Updated:   // 
Input:           // 
Output:          // 
Return:          // 
Others:          // 
*************************************************/


void cdrlog_init() 
{
	
char log_file[64], cmdstr[64];
	
long l_time;
	
struct tm *t;
	
l_time = time(NULL);
	
t = localtime(&l_time);
	
if (access(CDR_LOG_PATH, F_OK) != 0)
		
 {
		
sprintf(cmdstr, "mkdir -p -m777 %s", CDR_LOG_PATH);
		
printf("%s\n", cmdstr);
		
system(cmdstr);
		
}
	

sprintf(log_file, "%s/%s_%d_%d", CDR_LOG_PATH, CDR_LOG_FILE,
			  t->tm_mon + 1, t->tm_mday);
	
log_fp = fopen(log_file, "a");
	
cdr_log("CDR Module start up.%s", ctime(&l_time));

}

/*************************************************
Function:        // cdrlog_timer
Description:     // 定时地往日志文件里写一些日志
Calls:           // cdr_log;
Called By:       // cdr.c:cdr_timer;
Table Accessed:  // 
Table Updated:   // 
Input:           // 
Output:          // 
Return:          // 
Others:          // 
*************************************************/

void cdrlog_timer() 
{
	
char log_file[64];
	
long l_time;
	
static BYTE ch = 0, ch2 = 0;
	
static short m_state;
	
struct tm *t;

	if (m_state++ < 6)
		
return;
	
m_state = 0;
	

l_time = time(NULL);
	
t = localtime(&l_time);
	

if (ch2 == 0 && t->tm_min % 15 <= 3)	//report per 15 minute
	{
		
cdr_report();
		
ch2 = 1;
	
}
	
	else if (t->tm_min % 15 > 3)
		
ch2 = 0;
	

if (t->tm_hour == 0 && ch == 0)
		
 {
		
if (log_fp != NULL)
			
fclose(log_fp);
		

sprintf(log_file, "%s/%s_%d_%d", CDR_LOG_PATH, CDR_LOG_FILE,
				  t->tm_mon + 1, t->tm_mday);
		
if ((log_fp = fopen(log_file, "a")) != NULL)
			
ch = 1;
		
}
	
	else if (t->tm_hour > 0)
		
ch = 0;


}

/*************************************************
Function:        // cdr_report
Description:     // 定时地往日志文件里写入CDR数据源的一些信息
Calls:           // cdr_log;
Called By:       // cdrlog_timer;
Table Accessed:  // 
Table Updated:   // 
Input:           // 
Output:          // 
Return:          // 
Others:          // 
*************************************************/


void cdr_report() 
{
	
BYTE i;
	
long l_time;
	
struct tm *t;
	
cdr_src * pcdr;
	

l_time = time(NULL);
	
t = localtime(&l_time);
	

cdr_log("=================%02d:%02d:%02d================\n",
			  t->tm_hour, t->tm_min, t->tm_sec);
	
for (i = 0; i < iCdrNum; i++)
		
 {
		
pcdr = &CdrSet[i];
		
cdr_log("%-08lX(%s-%d)\n", htonl(pcdr->addr.remote_ip),
				 pcdr->record_table, pcdr->sysid);
		
cdr_log("\tphead=%ld\tptail=%d\tmaxbind=%d\n", pcdr->head,
				 pcdr->instance, pcdr->maxbind);
		
cdr_log("\treceved:%d\t\trecord:%d\tloss=%d\n", pcdr->recv,
				 pcdr->recd, pcdr->loss);
		
cdr_log
			("\tget state=%d\ttimeout=%d\tretry=%d\tset state=%d\ttimeout=%d\n\n",
			 pcdr->get_state, pcdr->get_timeout, pcdr->retry,
			 pcdr->set_state, pcdr->set_timeout);
		

}
	
cdr_log("===========================================\n\n");
	
return;


}

/*************************************************
Function:        // cdr_log
Description:     // 往日志文件里写入信息函数
Calls:           // 
Called By:       // 
Table Accessed:  // 
Table Updated:   // 
Input:           // 消息格式以及参数
Output:          // 
Return:          // 
Others:          // 
*************************************************/


void cdr_log(const char *fmt, ...) 
{
	
char buf[2048];
	
va_list ap;
	
if (log_fp == NULL)
		
return;
	
va_start(ap, fmt);
	
vsprintf(buf, fmt, ap);
	
fputs(buf, log_fp);
	
fflush(log_fp);
	
va_end(ap);

}


/*************************************************
Function:        // cdr_log
Description:     // 以十六进制的格式往日志文件里写入信息
Calls:           // 
Called By:       // 
Table Accessed:  // 
Table Updated:   // 
Input:           // msgbuf:字节指针; len:数组长度;
Output:          // 
Return:          // 
Others:          // 
*************************************************/


void cdr_logmsg(BYTE * msgbuf, int len) 
{
	
int i;
	
char buf[2048];
	
if (log_fp == NULL)
		
return;
	
for (i = 0; i < len; i++)
		
sprintf(buf + 3 * i, "%02X ", msgbuf[i]);
	
strcat(buf, "\n");
	
fputs(buf, log_fp);
	
fflush(log_fp);

}


