First, file operations
See link https://www.cnblogs.com/dolphin0520/archive/2011/10/05/2199598.html
Second, the logs are encapsulated to achieve
FILE *LOG_FP; #define LOG(level, format, ...) \ LOG_FP = fopen("/home/macrored/Desktop/test.log", "a"), \ fprintf(LOG_FP, "[%s|%s@%s:%d] " format "\n", \ level, __func__, __FILE__, __LINE__, ##__VA_ARGS__ ), \ fclose(LOG_FP)
Third, to achieve results
#include <stdio.h> FILE *LOG_FP; #define LOG(level, format, ...) \ LOG_FP = fopen("/home/macrored/Desktop/test.log", "a"), \ fprintf(LOG_FP, "[%s|%s@%s:%d] " format "\n", \ level, __func__, __FILE__, __LINE__, ##__VA_ARGS__ ), \ fclose(LOG_FP) int main() { LOG("LOG_DEBUG", "a=%d", 10); return 0; }
Log output function contents as follows:
macrored@ubuntu:~/Desktop$ cat test.log [LOG_DEBUG|[email protected]:11] a=10
Reference links https://blog.csdn.net/shanzhizi/article/details/8983768