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

JavaScript implementation of the Game of Life

Table of contents Concept Introduction Logical ru...

Vue+Websocket simply implements the chat function

This article shares the specific code of Vue+Webs...

Native js implementation of slider interval component

This article example shares the specific code of ...

How many times will multiple setStates in React be called?

Table of contents 1. Two setState, how many times...

Three properties of javascript objects

Table of contents 1. writable: writable 2. enumer...

MySQL data backup and restore sample code

1. Data backup 1. Use mysqldump command to back u...

Specific use of pthread_create in linux to create threads

pthread_create function Function Introduction pth...

What qualities should a good advertisement have?

Some people say that doing advertising is like bei...

Install Linux using VMware virtual machine (CentOS7 image)

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

How to open external network access rights for mysql

As shown below: Mainly execute authorization comm...

JS implements random generation of verification code

This article example shares the specific code of ...

Analysis of the problem of deploying vue project and configuring proxy in Nginx

1. Install and start nginx # Install nginx sudo a...

MySQL explain obtains query instruction information principle and example

explain is used to obtain query execution plan in...