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

写代码要有调试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 ",  
                    tv.tv_sec, tv.tv_usec, getpid(), file, line, func, tmpStr); 
     
    free(tmpStr); 
     
    return; 

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

原文地址:https://www.cnblogs.com/skyofbitbit/p/3672876.html