写代码要有调试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. Codeforces Beta Round #10 A. Power Consumption Calculation 水题

    A. Power Consumption Calculation 题目连接: http://www.codeforces.com/contest/10/problem/A Description To ...

  2. PAT甲级1103. Integer Factorization

    PAT甲级1103. Integer Factorization 题意: 正整数N的K-P分解是将N写入K个正整数的P次幂的和.你应该写一个程序来找到任何正整数N,K和P的N的K-P分解. 输入规格: ...

  3. mysql server has gone away的原因

    之前遇到开发询问“mysql server has gone away”的问题,想当然的就认为是由于太长时间没有操作,导致超过MySQL服务端上的wait_timeout的设置,最终连接被MySQL服 ...

  4. asp.net 链接数据库ADO.NET

    web.config <configuration> <connectionStrings> <add name="constr" connectio ...

  5. IDA Pro plug-in defines

    /* This file contains definitions used by the Hex-Rays decompiler output. It has type definitions an ...

  6. JSP页面乱码全解析

    乱码指的是中文乱码. 一.POST乱码 由于两个页面都是用UTF-8编码,但是参数的传输默认是ISO-8859-1,这时候可以在接受参数之前 request.setCharacterEncoding( ...

  7. jdbcTemplate异常:like模糊查询报错(Parameter index out of range (1 > number of parameters)

    http://cuisuqiang.iteye.com/blog/1480525   模糊查询like要这样写 注意Object参数和like语法   public static void main( ...

  8. 发布一个C++版本的ORM库SmartDB

    先简单说说ORM的优点: 提高开发效率,减少重复劳动,只和业务实体打交道,由业务实体自动生成sql语句,不用手写sql语句. 简单易用, 可维护性好. 隔离数据源,使得我们更换数据源时不用修改代码. ...

  9. Silverlight:《Pro Silverlight5》读书笔记 之 Dependency Properties And Routed Event

    Dependency Properties And Routed Event Dependency Properties Dynamic Value Resolution As you’ve alre ...

  10. 关于TagHelper的那些事情——自定义TagHelper(内嵌TagHelper)

    内嵌TagHelper 上一篇文章中提到有时候需要设计一种内嵌的TagHelper,如下: <my name="yy" age="35"> < ...