/********************************************************************************************
*** Copyright (c) 2007, UTStarcom,Inc.
*** File name  : mlog.cpp
*** Module     : IPVS client 
*** Author     : Alex Han
*** Version    : 1.0
*** Created On : 2007.03.17
*** Description:
*** Modification histroy:
*********************************************************************************************/
#include "stdio.h"
#include "../JThread/src/jmutex.h"
#include "mlog.h"
#include <time.h>
#include "ModulePath.h"

bool mlogInited =false;
FILE *mlogLogFp = NULL;
int mlogmask = 0xff;		//print all log
JMutex mlogmutex;
long lMlogSize = 0;

#define NETWORK_MONITOR_PORT	3000
SOCKET logsock=INVALID_SOCKET;

int mlogInit(char *localFile)
{
	if (mlogInited)
    {
        return LOG_ALREADY_INITED;
    }	

	if (NULL == (mlogLogFp = fopen(localFile, "a")))
    {
        return LOG_ERROR;
    }

	fseek( mlogLogFp, 0L, SEEK_END );
	if ( ( lMlogSize = ftell(mlogLogFp) ) < 0)
		return LOG_GETSIZE_ERROR;
	
	mlogmutex.Init();

	mlogInited = true;

    return 0;
}

int mlogClose()
{
	if (!mlogInited || mlogLogFp==NULL)
    {
        return LOG_NOT_INITED;
    }
	
	fclose(mlogLogFp);

	mlogInited = false;

	return 0;
}

void mlog(unsigned int level,const char *format_str,...)
{
	if (!mlogInited || mlogLogFp==NULL)
    {
        return ;
    }	

	if(!(level&mlogmask))
		return ;

	mlogmutex.Lock();

	if (lMlogSize >= MAX_CLIENT_LOGFILE_SIZE)	
	{
		fclose(mlogLogFp);
		mlogLogFp = NULL;
		mlogRotate();
		lMlogSize = 0;
	}

	va_list ap; 
	va_start(ap,format_str);
	int ret = 0;
	char Message[5120];
	char OutMess[5120];
	time_t now;
	time(&now);
	struct tm* stm;
	stm = localtime(&now);
	
	ret = vsprintf(Message,format_str,ap);
	sprintf(OutMess,"%.19s  %s\n",asctime( stm ),Message); 
	if(mlogLogFp==NULL)
		return;
	fwrite(OutMess,strlen(OutMess),1,mlogLogFp);	

	if(logsock == INVALID_SOCKET)
	{
		logsock=socket(AF_INET,SOCK_DGRAM,0);
		if(logsock==INVALID_SOCKET)
		{
		}
	}

	if(logsock != INVALID_SOCKET)
	{
		struct sockaddr_in minitor;

		minitor.sin_family      = AF_INET;
		minitor.sin_addr.s_addr = inet_addr("127.0.0.1");
		minitor.sin_port        = htons(NETWORK_MONITOR_PORT);
		memset(&(minitor.sin_zero),0,8);			
					
		int ret=sendto(logsock, OutMess, strlen(OutMess),0, (const sockaddr *)&minitor, sizeof(minitor));
		if(ret!=strlen(OutMess))
		{
			closesocket(logsock);
			logsock=INVALID_SOCKET;
		}
	}

	lMlogSize += strlen(OutMess);
	va_end(ap);

	fflush(mlogLogFp);
	mlogmutex.Unlock();
}

void mlogRotate()
{
	char RotatedName[256];
	char CurrentTimeStr[19];

	struct tm *t;
    time_t tsec;	
    time(&tsec);
    t = localtime(&tsec);
	sprintf(CurrentTimeStr, "%02d-%02d-%02d-%02d-%02d-%02d", t->tm_year-100, t->tm_mon+1,
		t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
	sprintf(RotatedName, "%s-%s%s", PRE_LOGFILENAME, CurrentTimeStr, POST_LOGFILENAME);	
	
	char logfilename[1024];
	strcpy(logfilename,::GetModuleFileDirectory().c_str());
	sprintf(logfilename,"%s\\%s",logfilename,RotatedName);

	mlogLogFp = fopen(logfilename, "a");    
}