不管是服务器还是客户端,作为程序员都应该在自己的日志文件打印自己的程序输出信息。
如下函数,自测通过,将程序打印输出到日志文件,日志打印函数为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);
}
原文链接:
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- yrrf.cn 版权所有 赣ICP备2024042794号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务