Linux自定义日志打印函数

不管是服务器还是客户端,作为程序员都应该在自己的日志文件打印自己的程序输出信息。

如下函数,自测通过,将程序打印输出到日志文件,日志打印函数为qlog()函数,其功能和用法和printf()函数相似。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <stdarg.h>
int log_init();
void log_exit();
int qlog(const char *msg, ...);
static FILE *logfile = 0;
static char buff[4096];

int main()
{
    log_init();
    int a = 10;
    int i = 0;
    while(1)
    {
        sleep(1);
        qlog("asbdg\n");
        qlog("I am a log file %d\n",i++);
    }
    return 0;
}
int log_init()
{
    logfile = fopen("qlog.log", "w+");
    if(!logfile)
        return -1;
    return 0;
}
 
int qlog(const char *msg, ...)
{
    va_list ap;
 
    va_start(ap, msg);
    vsnprintf(buff, 4096, msg, ap);
    va_end(ap);
 
    fprintf(logfile, "%s", buff);
    fflush(logfile);
    return 0;
}
 
void log_exit()
{
    fclose(logfile);
}

原文链接:C/C++简单的日志函数总结(Qt Creator)_三公子Tjq的博客-CSDN博客_c语言日志函数

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_42475191

谢谢老板

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值