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
Some time ago, the project needed to develop the ...
Every time you log in to the test server, you alw...
The advantage of the master-slave synchronization...
Table of contents 1. Introduction 2. Prototype ch...
1. Description Earlier we talked about the instal...
Preface: I reinstalled win10 and organized the fi...
Record lock locks a single index record. Record l...
Table of contents Component Design Defining the f...
Table of contents 1. The concept of filter 1. Cus...
Table of contents What is an index The difference...
Table of contents Question: When the button is cl...
1. VMware download and install Link: https://www....
Table of contents need: drive: Ideas: accomplish:...
After purchasing an Alibaba Cloud server, you nee...
1. To optimize the query, try to avoid full table...