zoukankan      html  css  js  c++  java
  • 转: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; 

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

  • 相关阅读:
    java中Switch的实现原理浅谈
    漫谈计算机编码:从ASCII码到UTF8
    从斐波那契数列看java方法的调用过程
    Java中的位运算及简单的算法应用介绍
    在powserdesigner中,如何将Name(中文注释)导入到sqlserver的字段说明中?
    关于ArcMap的符号库和字体
    ORACLE CHAR,VARCHAR,VARCHAR2,NVARCHAR类型的区别与使用
    在window 2008 上安装arcgisserver93,报arcgissom 密码不符合安全策略
    Power Designer反向数据库时遇到sqlstate=37000错误,解决方案!
    解决PowerDesigner 反向工程没有注释且如何将注释转换成PDM的name
  • 原文地址:https://www.cnblogs.com/skyofbitbit/p/3672876.html
Copyright © 2011-2022 走看看