linux C language realizes the writing log function

  • 2020-11-18 06:36:12
  • OfStack

First on the program, the program has been tested to be able to write log requirements


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

The log format implemented by the program is:

Time + space + specific implementation (own debugging content)

What this program is worth learning:

Use of va_list structures Formatted output string for linux The use of the pthread_mutex lock during file manipulation, and its advantages linux DEBUG application, convenient debugging

linux How to view logs:

tail command can be used to achieve the log query, as well as other functions, do not understand the words, their own search data to solve.

Please check the data if you do not understand the above application.


Related articles: