首页服务器Linux服务器 linux下C语言实现写日志功能

linux下C语言实现写日志功能

先上程序,该程序经过测试能够很好的实现写日志要求 /************************************************************************* > File Name: log.c > Author: *****…

先上程序,该程序经过测试能够很好的实现写日志要求

/*************************************************************************  > File Name: log.c  > Author:  ************************************************************************/#include #include  #include  #include #include #include #include #include #include int safe_asprintf(char **strp, const char *fmt, ...);int safe_vasprintf(char **strp, const char *fmt, va_list ap);void plog(const char *format, ...) ;void pinfo(const char *format, ...) ;#define DEBUG#ifdef DEBUGvoid plog(const char *format, ...);void pinfo(const char *format, ...);#define debug(fmt, args...) plog(fmt, ##args) #else#define debug(fmt, args...) do{}while(0)#endifstatic pthread_mutex_t fileMutex = PTHREAD_MUTEX_INITIALIZER;int main(int argc, char *argv){  return 0;}/* * safe_asprintf(); */int safe_asprintf(char **strp, const char *fmt, ...) {  va_list ap;  int retval;  va_start(ap, fmt);  retval = safe_vasprintf(strp, fmt, ap);  va_end(ap);  return retval;}/* * safe_vasprintf(); */int safe_vasprintf(char **strp, const char *fmt, va_list ap) {  int retval;  retval = vasprintf(strp, fmt, ap);  if (retval == -1)   {    printf("Failed to vasprintf: %s. Bailing out/n", strerror(errno));    return 1;  }  return retval;}/* * plog(); */void plog(const char *format, ...) {  pthread_mutex_lock(&fileMutex);  FILE *fp = NULL;  va_list vlist;  char *fmt = NULL;  // Open debug info output file.  if (!(fp = fopen("log.txt", "a+"))) {    pthread_mutex_unlock(&fileMutex);    return;  }  va_start(vlist, format);  safe_vasprintf(&fmt, format, vlist);  va_end(vlist);  if (!fmt) {    pthread_mutex_unlock(&fileMutex);    return;  }  time_t timep;  struct tm *ptm = NULL;  time(&timep);  ptm = localtime(&timep);  fprintf(fp, "[%04d-%02d-%02d-%02d-%02d-%02d] %s",       ptm->tm_year + 1900,       ptm->tm_mon + 1,      ptm->tm_mday,       ptm->tm_hour,       ptm->tm_min,       ptm->tm_sec,       fmt);  free(fmt);  fsync(fileno(fp));  fclose(fp);  pthread_mutex_unlock(&fileMutex);}/* * pinfo(); */void pinfo(const char *format, ...) {  pthread_mutex_lock(&fileMutex);  FILE *fp = NULL;  va_list vlist;  char *fmt = NULL;  // Open debug info output file.  if (!(fp = fopen("log.txt", "a+"))) {    pthread_mutex_unlock(&fileMutex);    return;  }  va_start(vlist, format);  safe_vasprintf(&fmt, format, vlist);  va_end(vlist);  if (!fmt) {    pthread_mutex_unlock(&fileMutex);    return;  }  fprintf(fp, "%s", fmt);  free(fmt);  fsync(fileno(fp));  fclose(fp);  pthread_mutex_unlock(&fileMutex);}
本文来自网络,不代表1号站长-站长学院|资讯交流平台立场。转载请注明出处: https://www.1cn.cc/fwq/Linux/9416.html
上一篇linux查看匹配内容的前后几行方法
下一篇 详解Linux iptables 命令
admin

作者: admin

这里可以再内容模板定义一些文字和说明,也可以调用对应作者的简介!或者做一些网站的描述之类的文字或者HTML!

为您推荐

评论列表()

    联系我们

    联系我们

    0898-88888888

    在线咨询: QQ交谈

    邮箱: email@wangzhan.com

    工作时间:周一至周五,9:00-17:30,节假日休息

    关注微信
    微信扫一扫关注我们

    微信扫一扫关注我们

    关注微博
    返回顶部