c++ note--unix程序日志实现

int My_Log(int level, const char* className, const char* fileName, int lineNo, const char* fmt, ...)
{
	cout<<"into log!"<<endl;
	int ret = 0;
	char m_buf[20480];
	char szBuffer[20480]; 
	char szLogFile[1000];
	char tmp[64];
	char szSec[32] = {0};
	int nCurrDayNo = 0;
	//日志格式
	int m_pos = 0 ; 	     
	/*
	函数名: vsprintf 
	功 能: 送格式化输出到串中 
	用 法: int vsprintf(char *string, char *format, va_list param); 
	*/		
	va_list argptr;
	va_start(argptr, fmt); 
	m_pos += vsprintf(m_buf+m_pos, fmt, argptr); 
	va_end(argptr); 
	strcat(m_buf+m_pos++, "\n");

	struct timeval tv; 	 
	gettimeofday(&tv,NULL);
	sprintf(szSec,"%06u",tv.tv_usec);
	
	//取系统当前日期
	time_t t = time(0);     
	char cur_time[64];      
	strftime(cur_time,sizeof(cur_time),"%Y%m%d %H%M%S",localtime(&t));
	strftime(tmp,sizeof(tmp),"%Y%m%d",localtime(&t));   
	nCurrDayNo = atoi(tmp);

	int pthread_id =  pthread_self() ;    
	//int pthread_id = 110;
	sprintf(szBuffer,"[%d][%s:%s][%d][%s][%s][%d]%s",level,cur_time,szSec,pthread_id,className,fileName,lineNo, m_buf);  

	//printf("%s",szBuffer); 

	//************ recode to logfile
	sprintf(szLogFile, "%s%s_%d.log", "/home/hb_ocs/zhouxin/log/", "log", nCurrDayNo);		
	//create or open the log file    
	FILE* pLogFile = NULL;	
	pLogFile = fopen(szLogFile, "a+");
	if(pLogFile == NULL)
	{
		printf("OPEN FILE ERROR!\n");
		exit(1);
	}

	fprintf(pLogFile, "%s", szBuffer);     
	fflush(pLogFile);
	fclose(pLogFile);

	return ret;
}
<pre name="code" class="cpp">//调用日志函数实例
My_Log(1, "::DCdeal", __FILE__, __LINE__,  " 本次数据处理结束! ... ");


 

                
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值