不管是服务器还是客户端,作为程序员都应该在自己的日志文件打印自己的程序输出信息。
如下函数,自测通过,将程序打印输出到日志文件,日志打印函数为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);
}