linux下可变参数打印

【自取】最近整理的,有需要可以领取学习:

参考c++文档的可变参数打印进行修改

#include
#include
#include

FILE * pFile = NULL;

void WriteFormatted (const char* pfileName , const char* pfun , const long lline, const char * format, ...)
{
        if(!pFile) return;

        time_t timep;
        time (&timep);
        char tmp[64];
        strftime(tmp, sizeof(tmp),/* "%Y-%m-%d %H:%M:%S"*/"%F %T",localtime(&timep) );

        fprintf(pFile,"%s %s:%s:%ld  ",tmp,pfileName,pfun,lline);

        va_list args;
        va_start (args, format);
        vfprintf (pFile, format, args);
        va_end (args);

}

#define debuglog(format, ...)    WriteFormatted(__FILE__, __FUNCTION__, __LINE__, format, ##__VA_ARGS__)

int main ()
{
        pFile = fopen ("myfile.txt","a+");

        debuglog("Call with %d variable %s.\n",2,"arguments");
        debuglog("Call with %d variable %s.\n",2,"arguments");
        debuglog("Call with %d variable %s.\n",2,"arguments");
        debuglog("Call with %d variable %s.\n",2,"arguments");
        debuglog("Call with %d variable %s.\n",2,"arguments");
        debuglog("Call with %d variable %s.\n",2,"arguments");

        fclose (pFile);
        return 0;
}

Original: https://www.cnblogs.com/nanqiang/p/13071420.html
Author: cicero
Title: linux下可变参数打印

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/10336/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

发表回复

登录后才能评论
免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部