#include <stdio.h> #include <stdlib.h> #include <time.h> #include <stdarg.h> #define LOG_SWITCH 1
/* 在想要打印的.c文件中定义并调用即可,注意设置好文件路径 */ void DEV_LOG(const char* ptrFormatInput, ...) { time_t systemTime; struct tm* formatSystemTime = NULL; char buffer[512]; FILE* ptrFile = NULL; if (LOG_SWITCH) { ptrFile = fopen("./DEV_LOG.log", "a"); if (ptrFile == NULL) { return; } /* 获取系统时间 */ time(&systemTime); formatSystemTime = localtime(&systemTime); (void)sprintf(buffer, " [ %u-%u-%u %u:%u:%u ] ", formatSystemTime->tm_year + 1900, formatSystemTime->tm_mon + 1, formatSystemTime->tm_mday, formatSystemTime->tm_hour, formatSystemTime->tm_min, formatSystemTime->tm_sec); (void)fprintf(ptrFile, "%s", buffer); va_list ptrArgs; va_start(ptrArgs, ptrFormatInput); vsnprintf(buffer, sizeof(buffer), ptrFormatInput, ptrArgs); va_end(ptrArgs); buffer[512 - 1] = '