成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

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

服務(wù)器

先上程序,該程序經(jīng)過測(cè)試能夠很好的實(shí)現(xiàn)寫日志要求

天鎮(zhèn)網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)建站!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)建站從2013年開始到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(liá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);
}

程序?qū)崿F(xiàn)的日志格式為:

時(shí)間 + 空格 + 具體實(shí)現(xiàn)(自己的調(diào)試內(nèi)容)

本段程序值得學(xué)習(xí)的地方:

va_list 結(jié)構(gòu)體的使用 linux 的格式化輸出字符串 文件操作過程中pthread_mutex鎖的使用,以及他的優(yōu)點(diǎn) linux DEBUG 的應(yīng)用,方便調(diào)試

linux如何查看日志:

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

對(duì)上面應(yīng)用不明白的請(qǐng)自行查資料解決。

文章題目:linux下C語言實(shí)現(xiàn)寫日志功能
鏈接URL:http://jinyejixie.com/article48/chcpep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)公司、App設(shè)計(jì)自適應(yīng)網(wǎng)站、網(wǎng)站維護(hù)、定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)