• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    linux下C语言实现写日志功能

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

    /*************************************************************************
      > 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 命令可以实现日志的查询,以及其他功能,不了解的话,自行查资料解决。

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

    上一篇:详解linux tail命令显示最后n行
    下一篇:linux tail 查看日志文件方法
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

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