Linux C log output code template sample code

Linux C log output code template sample code

Preface

This article mainly introduces the relevant content about the log output code template under Linux C, and shares it for your reference and study. Let's take a look at the detailed introduction.

template

The template is divided into two files: log.c and log.h.

log.c

/** log.c **/
#include <unistd.h>
#include "log.h"

// log file path #define filepath "./ps_com_log.log"
 
//Set time static char * settime(char * time_s){
 time_t timer = time(NULL);
 strftime(time_s, 20, "%Y-%m-%d %H:%M:%S",localtime(&timer));
 return time_s;
}
 
/*
 *Print* */
static int PrintfLog(char * logText, char * string){
 FILE *fd = NULL;
 char s[1024];
 char tmp[256];

 //Open the file using append method fd = fopen(filepath,"a+");
 if(fd == NULL){
  return -1;
 }
 
 memset(s, 0, sizeof(s));
 memset(tmp, 0, sizeof(tmp));
 
 sprintf(tmp, "*****[pid=%d]:[", getpid());
 strcpy(s, tmp);
 
 memset(tmp, 0, sizeof(tmp));
 settime(tmp);
 strcat(s, tmp);

 strcat(s, "]*****");
 fprintf(fd, "%s", s);

 fprintf(fd, "*[%s]*****:\n",logText); 
 fprintf(fd, "%s\n",string); 
 fclose(fd);
}
 
 /*
 *Log write* */
void LogWrite(char *logText,char *string)
{
 //[Lock is needed to support multithreading] pthread_mutex_lock(&mutex_log); //lock. 
 //Print log information PrintfLog(logText, string);
                  
 //[Lock is needed to support multithreading] pthread_mutex_unlock(&mutex_log); //unlock.            
}

log.h

#ifndef __LOG_H__
#define __LOG_H__
#include <stdio.h>
#include <string.h>
#include <time.h>
 

void LogWrite(char * logText,char *string);

#endif /* __LOG_H__ */

Test Files

Now that we have the log output function, let's do a simple test:

#include "stdio.h"
#include "log.h"
int main(int argv,char**argc){
 printf("test\n");
 LogWrite("INFO","Hello World!");
 LogWrite("error","Hello World!");
 LogWrite("mint","Hello World!");
 LogWrite("iout","Hallo World!");

 return 0;
}

The above code is very simple and will not be explained in detail.

Running results:

*****[pid=15971]:[2018-12-05 14:24:21]******[INFO]*****:
Hello World!
*****[pid=15971]:[2018-12-05 14:24:21]******[error]*****:
Hello World!
*****[pid=15971]:[2018-12-05 14:24:21]******[mint]*****:
Hello World!
*****[pid=15971]:[2018-12-05 14:24:21]******[iout]*****:
Hallo World!

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • Linux uses logrotate to cut log files
  • Summary of logrotate log polling operation in Linux

<<:  Implementation steps for building multi-page programs using Webpack

>>:  WeChat applet realizes horizontal and vertical scrolling

Recommend

Sample code for generating QR code using js

Some time ago, the project needed to develop the ...

How to set up ssh password-free login to Linux server

Every time you log in to the test server, you alw...

Master-slave synchronous replication configuration of MySQL database under Linux

The advantage of the master-slave synchronization...

JavaScript Composition and Inheritance Explained

Table of contents 1. Introduction 2. Prototype ch...

Detailed discussion of InnoDB locks (record, gap, Next-Key lock)

Record lock locks a single index record. Record l...

Vue3 implements Message component example

Table of contents Component Design Defining the f...

Vue global filter concepts, precautions and basic usage methods

Table of contents 1. The concept of filter 1. Cus...

Detailed explanation of the role of key in React

Table of contents Question: When the button is cl...

Install Linux using VMware virtual machine (CentOS7 image)

1. VMware download and install Link: https://www....