亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

linux下C語言實現(xiàn)寫日志功能

 更新時間:2018年07月22日 10:33:40   作者:zgs2014  
這篇文章給大家分享了linux下C語言實現(xiàn)寫日志的相關功能方法以及實例代碼,有興趣的朋友可以學習參考下。

先上程序,該程序經過測試能夠很好的實現(xiàn)寫日志要求

/*************************************************************************
  > 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);
}

程序實現(xiàn)的日志格式為:

時間 + 空格 + 具體實現(xiàn)(自己的調試內容)

本段程序值得學習的地方:

  1. va_list 結構體的使用
  2. linux 的格式化輸出字符串
  3. 文件操作過程中pthread_mutex鎖的使用,以及他的優(yōu)點
  4. linux DEBUG 的應用,方便調試

linux如何查看日志:

使用tail 命令可以實現(xiàn)日志的查詢,以及其他功能,不了解的話,自行查資料解決。

對上面應用不明白的請自行查資料解決。

相關文章

最新評論