/*
*
*file description
*
*/
#include "stdafx.h"
#include "publicInclude.h"
#include "baseStruct.h"
/*****************************************************
* Function description
*
*Author:Qlong
*Date:2005-04-18
*Right All Reserve QlongSoftWare
*Input:
*Output:
*Return:
*
******************************************************/
void sleepMilliSecond(int ms)
{
timeval tv;
tv.tv_sec = ms/1000;
tv.tv_usec = (ms%1000)*1000;
select(0, (fd_set*)NULL, (fd_set*)NULL , (fd_set*)NULL, &tv);
}
/*****************************************************
* Function description
*
*Author:Qlong
*Date:2005-04-18
*Right All Reserve QlongSoftWare
*Input:
*Output:
*Return:
*
******************************************************/
TBool checkFile(FILE *fp, TCChar *fileName)
{
struct stat buf;
TInt result;
TInt lenFileName = strlen(fileName);
if( 1 > lenFileName)
{
printf("File name invalid");
AfxMessageBox("lenFileName failed");
return False;
}
TChar newFileName[MAX_PATH];
memset(newFileName, NULL_CHAR, MAX_PATH);
SYSTEMTIME lpSystemTime;
char nowDay[20];
memset(nowDay, NULL_CHAR, 20);
GetSystemTime(&lpSystemTime);
sprintf(nowDay,"%04d%02d%02d%02d%02d",
lpSystemTime.wYear,
lpSystemTime.wMonth,
lpSystemTime.wDay,
lpSystemTime.wHour,
lpSystemTime.wMinute);
sprintf(newFileName, "%s-%s", fileName, nowDay);
result = stat( fileName, &buf );
if( result != NULL_INT )
{
printf("Stat %s failed", fileName);
AfxMessageBox("stat failed");
return False;
}
//验证日志文件大不,如果大于20M则改名重新另建一文件
if(buf.st_size >= MAX_DEBUG_FILE)
{
if(fclose(fp))
{
printf("Close debug log file failed");
return False;
}
fp = NULL;
result = rename(fileName, newFileName);
if( result != NULL_INT )
{
printf("Rename failed");
AfxMessageBox("rename failed");
return False;
}
fp = fopen(fileName, "a+");
if( NULL == fp)
{
printf("Open %s failed", fileName);
AfxMessageBox("fopen failed");
return False;
}
CString bakFile = _T("");
bakFile.Format(_T("%s"), newFileName);
bakFile.Replace(LOGCFG_DIR, BAKCFG_DIR);
if(!MoveFile(newFileName, bakFile))
{
printf("Move %s file failed");
AfxMessageBox("MoveFile failed");
return False;
}
}
return True;
}
/*****************************************************
* Function description
*
*Author:Qlong
*Date:2005-04-18
*Right All Reserve QlongSoftWare
*Input:
*Output:
*Return:
*
******************************************************/
void PrintFormat(int logPri, const char* format, ...)
{
char fileFullPath[100];
memset(fileFullPath, 0, 100);
sprintf(fileFullPath, "%s%s[%d]%s", g_WorkPath, LOGCFG_DIR,
g_ServerPort, g_debugFileName);
if(NULL == g_debugFile || INVALID_INT == access(fileFullPath, 0))
{
g_debugFile = fopen(fileFullPath, "a+");
if( NULL == g_debugFile )
{
AfxMessageBox("打开日志文件失败");
//printf("[Error] Log file open failed\n");
exit(0);
}
fprintf(g_debugFile,"===========================CS service Start========="\
"=====================\n");
}
if(False == g_bLog)
{
return;
}
if(g_LogLevel != 10)
{
if( g_LogLevel != 8)
{
if(g_LogLevel != logPri)
{
return;
}
}
}
if( False == checkFile(g_debugFile, fileFullPath))
{
exit(0);
}
TChar buf[MAX_LEN_PIC_FILE];
TChar strbuf[MAX_LEN_BUFFER];
TChar timebuf[MAX_LEN_LOGTIME];
TChar logPriBuf[MAX_LEN_LOGTIME];
memset(buf, NULL_CHAR, MAX_LEN_PIC_FILE);
memset(strbuf, NULL_CHAR, MAX_LEN_BUFFER);
memset(timebuf, NULL_CHAR, MAX_LEN_LOGTIME);
memset(logPriBuf, NULL_CHAR, MAX_LEN_LOGTIME);
//格式化变参到字符串
va_list args;
va_start(args, format);
vsprintf(buf, format, args);
va_end(args);
TInt bufLen = strlen(buf);
//get current time and format string save strbuf
//time_t longtime;
//struct tm* curtime=NULL;
//time(&longtime);
//curtime=localtime(&longtime);
//取得系统的当前时间,精确到毫秒
SYSTEMTIME lpSystemTime;
GetSystemTime(&lpSystemTime);
sprintf(timebuf,"%02d-%02d %02d:%02d:%02d-%03dms]",
lpSystemTime.wMonth,
lpSystemTime.wDay,
lpSystemTime.wHour,
lpSystemTime.wMinute,
lpSystemTime.wSecond,
lpSystemTime.wMilliseconds);
if(bufLen > 2*1024)
{
if(10==g_LogLevel)
{
printf("Warning:print content=[%d] too long=>2K\n", bufLen);
return;
}
#ifdef _NOLOG
printf("Warning:print content=[%d] too long=>2K\n", bufLen);
return;
#endif
fprintf(g_debugFile, "[WARNING:Print content=[%d] too big =>2K\n",
timebuf, bufLen);
fflush(g_debugFile);
return;
}
switch(logPri)
{
case PROMPT:
strcat(logPriBuf, "[PROMPTS:");
break;
case WARNING:
strcat(logPriBuf, "[WARNING:");
break;
case SPERROR:
strcat(logPriBuf, "[SPERROR:");
break;
case SUCCESS:
strcat(logPriBuf, "[SUCCESS:");
break;
default:
break;
}
strncat(strbuf, logPriBuf, strlen(logPriBuf));
strncat(strbuf, timebuf, strlen(timebuf));
strncat(strbuf, buf, strlen(buf));
if (strbuf[strlen(strbuf)-1] != '\n')
{
strncat(strbuf, "\n", strlen("\n"));
}
if(10==g_LogLevel)
{
printf(strbuf);
return;
}
//增加不输出日志文件,直接输出到屏幕的调试开关
#ifdef _NOLOG
printf(strbuf);
return;
#endif
fprintf(g_debugFile, "%s", strbuf);
fflush(g_debugFile);
return;
}
/*****************************************************
* Function description
*
*Author:Qlong
*Date:2005-04-18
*Right All Reserve QlongSoftWare
*Input:
*Output:
*Return:
*
******************************************************/
void PrintBCDFormat(int printOpt, const int len, char * buf)
{
}
/*****************************************************
* Function description
*
*Author:Qlong
*Date:2005-04-18
*Right All Reserve QlongSoftWare
*Input:
*Output:
*Return:
*
******************************************************/
void PrintBCDCode(char * buf)
{
}
/*****************************************************
* Function description
*
*Author:Qlong
*Date:2005-04-18
*Right All Reserve QlongSoftWare
*Input:
*Output:
*Return:
*
******************************************************/
TBool InitGlobalVarResource()
{
printf("Enter InitGlobalVarResource\n");
//获取配置文件中的端口号,方便确定日志文件
if(g_ServerPort == 0)
{
g_ServerPort=g_IniFile.GetInt("SERVER_INFO", "SERVER_PORT", 1850);
if(g_ServerPort<1850 || g_ServerPort >9999)
{
AfxMessageBox("服务器端口号配置项非法,请纠正", MB_ICONERROR);
return False;
}
}
g_pServer = new TSocket;
if( NULL == g_pServer )
{
ALLOCAL_MEMORY_FAILED;
return False;
}
int maxServerNo=g_ServerPort+MAX_LEN_OF_SERVER;
while(False==g_pServer->OpenServer(g_ServerPort))
{
g_ServerPort++;
if(maxServerNo<g_ServerPort)
{
AfxMessageBox("服务器的启动的个数已超过最大值10个!", MB_ICONERROR);
return False;
}
}
PrintFormat(SUCCESS, "Create socket server success!");
g_pMainControl = new TMainControl;
评论6
最新资源