⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 comm_log.c

📁 unix下多进程编程实例
💻 C
字号:
/********************************************************************

 * NAME         : TCcom_Log.c

 * FUNCTION     : 

 * AUTHOR		: 

 * OS           : solaris

 ********************************************************************/



/********************************************************************

 *  INCLUDE FILES

 ********************************************************************/

#include "Comm_log.h"



/********************************************************************

 *  全局变量

 ********************************************************************/

int giOutputLog = 1 ;



char *__file__;      /* current source file name */

int __line__;       /* current line number */



/********************************************************************

 * NAME     : DebugLogOut()

 * FUNCTION : 

 * PROCESS  : 

 * INPUT    : 

 * OUTPUT   : 

 * UPDATE   : 

 * RETURN   : 

 * PROGRAMMED   : TELSTAR/lczheng

 * DATE(ORG): 2000.04.08 

 * CALL     : 

 * DEBUG FILE NAME		:	Log[process_name].[process_id]

 * DEBUG FILE FORMAT	:

	<YYMMDDHHMISS> "source_file_name"(line_number): debug_message

 ********************************************************************/

int DebugLogOut(char *fmt,...)

{

	va_list	ap;

	int  DbgFd,pid;

	char *pDbgFile;

	struct stat stStat;

	char *pFiBuf,*pVaBuf;

	time_t	iCurTim;

	struct tm stLocTim;

	

	if(giOutputLog==0){		/* not need output debuglog */

		return IS_OK;

	}



	pDbgFile = (char *)malloc(256);

	if(pDbgFile==NULL){

		return IS_NG;

	}



	/* 取得文件名 */

	pid = getpid();

	sprintf(pDbgFile,"%s/log/debuglog/Log%s.%06d",

					WORK_DIR,

					szSubSystemID,

					pid);



	if(stat(pDbgFile,&stStat) == -1){		/* 日志文件可能不存在,创建方式打开 */

		if((DbgFd = open(pDbgFile,O_CREAT|O_WRONLY,0666)) == -1){

			free(pDbgFile);

			return IS_NG;

		}

	}else if(stStat.st_size>DEBUGFILE_MAXLEN){	/* 文件已经大于2M,截取方式打开 */

		if((DbgFd = open(pDbgFile,O_WRONLY|O_TRUNC,0666)) == -1){

			free(pDbgFile);

			return IS_NG;

		}

	}else{									/* 追加方式打开 */

		if((DbgFd = open(pDbgFile,O_WRONLY | O_APPEND,0666)) == -1){

			free(pDbgFile);

			return IS_NG;

		}

	}

	

	pFiBuf = (char *)malloc(256);

	pVaBuf = (char *)malloc(4096);

	if(pFiBuf==NULL || pVaBuf==NULL){

		free(pDbgFile);

		return IS_NG;

	}



	iCurTim = time(NULL);

	stLocTim = *localtime(&iCurTim);

	sprintf(pFiBuf,"<%02d%02d%02d%02d%02d%02d> \"%s\"(%d): ",

			stLocTim.tm_year % 100, stLocTim.tm_mon + 1,stLocTim.tm_mday,

			stLocTim.tm_hour,stLocTim.tm_min,stLocTim.tm_sec,

			__file__,__line__);



	write(DbgFd,pFiBuf,strlen(pFiBuf));



	va_start(ap,fmt);

	(void)vsprintf(pVaBuf,fmt,ap);

	va_end(ap);

	

	write(DbgFd,pVaBuf,strlen(pVaBuf));

	write(DbgFd,"\n",1);

	close(DbgFd);



	free(pDbgFile);

	free(pFiBuf);

	free(pVaBuf);



	return IS_OK;

}



/********************************************************************

 * NAME     : ErrorLog()

 * FUNCTION : 

 * PROCESS  : 

 * INPUT    : iErrType: Error Type;

 *			: pObj	:	USERID(for PC) or JFIP(for JF)

 * OUTPUT   : 

 * UPDATE   : 

 * RETURN   : 

 * PROGRAMMED   : TELSTAR/lczheng

 * DATE(ORG): 2000.04.08 

 * CALL     : 

 * ERROR FILE NAME		:	ErrorLogYYYYMMDD

 * ERROR FILE FORMAT	:

	process_name error_type error_output_time : error_message

 ********************************************************************/

int ErrorLog(int iErrType,char *pObj,char *fmt,...)

{

	va_list	ap;

	int  ErrFd,pid;

	char sErrFile[256];

	struct stat stStat;

	char sFiBuf[256],sVaBuf[4096];

	time_t	iCurTim;

	struct tm stLocTim;

	

	/* 取得文件名 */

	iCurTim = time(NULL);

	stLocTim = *localtime(&iCurTim);

	sprintf(sErrFile,"%s/log/errorlog/ErrorLog%04d%02d%02d",

					WORK_DIR,

					stLocTim.tm_year + 1900, stLocTim.tm_mon + 1,stLocTim.tm_mday);



	if(stat(sErrFile,&stStat) == -1){		/* 日志文件可能不存在,创建方式打开 */

		if((ErrFd = open(sErrFile,O_CREAT|O_WRONLY,0666)) == -1){

			return IS_NG;

		}

	}else if(stStat.st_size>ERRORFILE_MAXLEN){	/* 文件已经大于20M,截取方式打开 */

		if((ErrFd = open(sErrFile,O_WRONLY|O_TRUNC,0666)) == -1){

			return IS_NG;

		}

	}else{									/* 追加方式打开 */

		if((ErrFd = open(sErrFile,O_WRONLY | O_APPEND,0666)) == -1){

			return IS_NG;

		}

	}

	

	pid = getpid();

	sprintf(sFiBuf,"%s %d %d %s %02d:%02d:%02d ",

			szSubSystemID,pid,iErrType,pObj,

			stLocTim.tm_hour,stLocTim.tm_min,stLocTim.tm_sec);



	write(ErrFd,sFiBuf,strlen(sFiBuf));



	va_start(ap,fmt);

	(void)vsprintf(sVaBuf,fmt,ap);

	va_end(ap);

	

	write(ErrFd,sVaBuf,strlen(sVaBuf));

	write(ErrFd,"\n",1);

	close(ErrFd);



	return IS_OK;

}



/********************************************************************

 * NAME     : DebugLogOutputCtrl()

 * FUNCTION : 

 * PROCESS  : Decide output debuglog or not

 * INPUT    : process name

 * OUTPUT   : 

 * UPDATE   : 

 * RETURN   : OUTPUTLOG_ON(1):		need output debuglog,

 *			: OUTPUTLOG_OFF(0):		do't need output debuglog.

 * AUTHOR	: ZZ-NODE/2000.12.20/songqufei 

 * CALL     : 

 ********************************************************************/

int DebugLogOutputCtrl(char *sProcName)

{

	char	sCtrlVal[16];

	int		iLoop;



	strcpy(sCtrlVal,MNG_IS_DEBUG);

	for(iLoop=0;iLoop<strlen(sCtrlVal);iLoop++){

		if(sCtrlVal[iLoop]>='a'&&sCtrlVal[iLoop]<='z'){

			sCtrlVal[iLoop] -= ('a'-'A');

		}

	}



	if(strcmp(sCtrlVal,"YES")==0){

		return 1;

	}else{

		return 0;

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -