Linux可变参数打印日志(二)

#include<stdio.h>
#include<stdlib.h>
#include<stdarg.h>
#include<string.h>

#define my_printf(fmt,...) debug_printf(__FILE__,__FUNCTION__,__LINE__,fmt,##__VA_ARGS__)
const char *logPath = "./log.txt";

void debug_printf(const char *file,const char *fun,const int line,const char *fmt, ...)
{
    va_list arg_ptr;
    va_list arg_tmp;
    va_start(arg_ptr,fmt);
    va_copy(arg_tmp,arg_ptr);

    printf("[%s %s %d] ",file,fun,line);
    vprintf(fmt,arg_ptr);

    FILE *pf = NULL;
    pf = fopen(logPath,"a");
    if(NULL != pf)
    {
        fprintf(pf,"[%s %s %d] ",file,fun,line);
        vfprintf(pf,fmt,arg_tmp);
        fclose(pf);
        pf = NULL;
    }    
    va_end(arg_ptr);    
}

int main()
{    
    int i = 1;
    my_printf("this is my %d test",i);
    return 0;
}

运行结果:

 

转载于:https://www.cnblogs.com/wanghao-boke/p/11165264.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值