写代码要有调试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; 
}

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

转:sock_ev——linux平台socket事件框架(logTrace) .的更多相关文章

  1. 转:sock_ev——linux平台socket事件框架(event loop) .

    上一篇我们封装了三种事件监听方式,如果分别提供给客户端使用,有点不方便,也不利于统一管理:我们再封装一层EventLoop. /************************************ ...

  2. 转:sock_ev——linux平台socket事件框架(socket API的封装) .

    把linux平台提供的有关socket操作的API进行封装是有必要的:基于stream操作的流程与基于dgram操作的流程略有不同,分别放在两个类中,但两者又有很多相似的操作,因此写一个基类,让其继承 ...

  3. 转:sock_ev——linux平台socket事件框架(event dispatcher) .

    最近比较忙,好久没更新了:今天我们看一下事件的监听方式,在linux下面事件的监听方式有三种select.poll.epoll,性能上面epoll最高,如果仅是最多监听十多个描述符,用啥无所谓,如果是 ...

  4. 转:sock_ev——linux平台socket事件框架(基于字节流的测试程序) .

    原文:http://blog.csdn.net/gdutliuyun827/article/details/8257186 由于工作与学习的需要,写了一个socket的事件处理框架,在公司写的已经使用 ...

  5. 转:sock_ev——linux平台socket事件框架(socket代理类) .

    前面分析了对socket基本操作的封装,并按照数据的传送方式写了两个类,本篇将写一个代理类提供给库的使用者使用的类. /**************************************** ...

  6. 转:sock_ev——linux平台socket事件框架(uri地址的解析) .

    在第一篇中,已经说明,传递的socket地址采取以下形式: [cpp] view plaincopyprint?stream://192.168.2.10:8080   dgram://192.168 ...

  7. 转:sock_ev——linux平台socket事件框架(基于数据报的测试程序) .

    上一篇已经做过注释,这一篇直接上代码 /******************************************************************************** ...

  8. AgileEAS.NET SOA 中间件平台.Net Socket通信框架-介绍

    一.前言 AgileEAS.NET SOA 中间件平台是一款基于基于敏捷并行开发思想和Microsoft .Net构件(组件)开发技术而构建的一个快速开发应用平台.用于帮助中小型软件企业建立一条适合市 ...

  9. AgileEAS.NET SOA 中间件平台.Net Socket通信框架-简单例子-实现简单的服务端客户端消息应答

    一.AgileEAS.NET SOA中间件Socket/Tcp框架介绍 在文章AgileEAS.NET SOA 中间件平台Socket/Tcp通信框架介绍一文之中我们对AgileEAS.NET SOA ...

随机推荐

  1. 01-项目简介Springboot简介入门配置项目准备

    总体课程主要分为4个阶段课程: ------------------------课程介绍------------------------ 01-项目简介Springboot简介入门配置项目准备02-M ...

  2. zoj 3621 Factorial Problem in Base K 数论 s!后的0个数

    Factorial Problem in Base K Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onli ...

  3. Codeforces Round #304 (Div. 2) B. Soldier and Badges 水题

    B. Soldier and Badges Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/54 ...

  4. iptables数据包、连接标记模块MARK/CONNMARK的使用(打标签)

    MARK标记用于将特定的数据包打上标签,供iptables配合TC做QOS流量限制或应用策略路由. 看看和MARK相关的有哪些模块: ls /usr/lib/iptables/|grep -i mar ...

  5. 让Firefox支持offsetX、offsetY

    //计算光标相对于第一个定位的父元素的坐标 function coordinate(e){ var o = window.event || e, coord, coord_X, coord_Y; co ...

  6. 修改Linux默认启动级别或模式

    在Linux中有7种启动级别,默认是X-Window,像是Windows的窗口模式,而Linux的操作和配置一般我们都采用输入命令的方式来完成,像DOS操作系统一样,如何让Linux一启动就进入这种模 ...

  7. Delegates and Events

    People often find it difficult to see the difference between events and delegates. C# doesn't help m ...

  8. text段,data段,bss段,堆和栈

    转载:http://blog.chinaunix.net/uid-29025972-id-3874376.html   程序编译后生成的目标文件至少含有三个段,分别为:.text..data和.bss ...

  9. linux下关于压缩、解压相关的操作

    本文转自: http://alex09.iteye.com/blog/647128 很不错的linux下关于压缩.解压相关的操作,适合于linux初学者.   .tar  解包:tar xvf Fil ...

  10. python使用游标访问数据

    游标是一种数据访问对象,可用于在表中迭代一组行或者向表中插入新行.游标有三种形式:搜索.插入或更新.游标通常用于读取现有几何和写入新几何. 每种类型的游标均由对应的 ArcPy 函数(SearchCu ...