sock_ev——linux平台socket事件框架(logTrace)

本文介绍了一种在C语言中实现日志记录的方法,通过宏定义简化调用过程,并利用变长参数支持灵活的日志输出格式。此外,还介绍了如何使用系统调用将日志输出到标准位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写代码要有调试log,采用syslog的输出;一般会输出到"/var/log/messages"

 

/***************************************************************************************
****************************************************************************************
* FILE		: log_trace.h
* Description	: 
*			  
* Copyright (c) 2012 by Liu Yanyun(E-mail:liuyun827@foxmail.com). All Rights Reserved.
*            Without permission, shall not be used for any commercial purpose
* 
* History:
* Version		Name       		Date			Description
   0.1		Liu Yanyun		2012/12/04		Initial Version
   
****************************************************************************************
****************************************************************************************/

#ifndef _LOG_TRACE_H_
#define _LOG_TRACE_H_

#ifdef __cplusplus
extern "C"{
#endif /* __cplusplus */

#include <stdio.h>

void log_printf(int line,
	const char *file,
	const char *func,
	const char *format,
	...);
	
#define logTrace(format, args...) \
	do{log_printf(__LINE__, __FILE__, __FUNCTION__, format, ##args);}while(0)


#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /*_LOG_TRACE_H_*/

上面头文件注意以下几点:

1、防止头文件重复包含的宏定义;

2、extern "C"的用法,C与C++的混合使用;

3、变长参数的使用;

4、编译器定义的的宏变量的使用;

5、do ...while(0)在宏定义中的用法;

 

实现文件:

/***************************************************************************************
****************************************************************************************
* FILE		: log_trace.cc
* Description	: 
*			  
* Copyright (c) 2012 by Liu Yanyun(E-mail:liuyun827@foxmail.com). All Rights Reserved.
*            Without permission, shall not be used for any commercial purpose
* 
* History:
* Version		Name       		Date			Description
   0.1		Liu Yanyun		2012/12/04		Initial Version
   
****************************************************************************************
****************************************************************************************/


#include "log_trace.h"
#include <stdio.h>
#include <syslog.h>
#include <time.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/time.h>

void log_printf(int line,
	const char *file,
	const char *func,
	const char *format,
	...)
{
	char *tmpStr;
	int rc;
	
	struct timeval tv ;
	
	va_list ap;
	va_start(ap, format);
	rc = vasprintf(&tmpStr, format, ap);
	va_end(ap);
	
	if(rc < 0)
	{
		syslog(LOG_ERR, "PID:%d;file:%s[%d],FUN:%s; vasprintf failed!!!", 
						getpid(), file, line, func);
		return;
	}
	
	gettimeofday(&tv, NULL);
	
	syslog(LOG_ERR, "%ld.%ld;PID:%d;file:%s[%d],FUN:%s; %s", 
					tv.tv_sec, tv.tv_usec, getpid(), file, line, func, tmpStr);
         //printf仅仅是我调试使用,直接打到屏幕,可以去掉
	printf("%ld.%ld;PID:%d;file:%s[%d],FUN:%s; %s\n", 
					tv.tv_sec, tv.tv_usec, getpid(), file, line, func, tmpStr);
	
	free(tmpStr);
	
	return;
}



开头几个函数与结构是处理变长参数使用的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值