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

Implementation of two basic images for Docker deployment of Go

1. golang:latest base image mkdir gotest touch ma...

How to create a file system in a Linux partition or logical volume

Preface Learn to create a file system on your sys...

How to backup and restore the mysql database if it is too large

Command: mysqlhotcopy This command will lock the ...

Detailed introduction to deploying k8s cluster on centos7 system

Table of contents 1 Version and planning 1.1 Vers...

Complete steps to quickly build a vue3.0 project

Table of contents 1. We must ensure that the vue/...

Example of adding music video to HTML page

1. Video tag Supports automatic playback in Firef...

MySQL stored functions detailed introduction

Table of contents 1. Create a stored function 2. ...

Vue implements paging function

This article example shares the specific code of ...

In-depth understanding of the matching logic of Server and Location in Nginx

Server matching logic When Nginx decides which se...

Detailed deployment of docker+gitlab+gitlab-runner

environment Server: centos7 Client: window Deploy...

Detailed explanation of the spacing problem between img tags

IMG tag basic analysis In HTML5, the img tag has ...

Each time Docker starts a container, the IP and hosts specified operations

Preface Every time you use Docker to start a Hado...