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

吾爱主题 阅读:125 2024-04-05 13:54:30 评论:0

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

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 /*************************************************************************    > File Name: log.c    > Author:   ************************************************************************/ #include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <stdarg.h> #include <time.h> #include <pthread.h>   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 DEBUG void plog( const char *format, ...); void pinfo( const char *format, ...); #define debug(fmt, args...) plog(fmt, ##args) #else #define debug(fmt, args...) do{}while(0) #endif   static 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. va_list 结构体的使用
  2. linux 的格式化输出字符串
  3. 文件操作过程中pthread_mutex锁的使用,以及他的优点
  4. linux DEBUG 的应用,方便调试

linux如何查看日志:

使用tail 命令可以实现日志的查询,以及其他功能,不了解的话,自行查资料解决。

对上面应用不明白的请自行查资料解决。

原文链接:https://blog.csdn.net/u013711616/article/details/52293447

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容