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:
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:
|
<<: Implementation steps for building multi-page programs using Webpack
>>: WeChat applet realizes horizontal and vertical scrolling
Table of contents Concept Introduction Logical ru...
This article shares the specific code of Vue+Webs...
This article example shares the specific code of ...
Table of contents 1. Two setState, how many times...
Table of contents 1. writable: writable 2. enumer...
Preface In web applications, in order to save tra...
1. Data backup 1. Use mysqldump command to back u...
pthread_create function Function Introduction pth...
With the popularization of 3G, more and more peop...
Some people say that doing advertising is like bei...
1. VMware download and install Link: https://www....
As shown below: Mainly execute authorization comm...
This article example shares the specific code of ...
1. Install and start nginx # Install nginx sudo a...
explain is used to obtain query execution plan in...