#include "log/Log.h" #include #include #include #include #define LOG_FILE_PATH_MAX 256 // #define LOG_LINE_MAX 256 static FILE* logFile = NULL; static char logFilePath[LOG_FILE_PATH_MAX]; void initLogger(const char* logfile) { strcpy(logFilePath, logfile); if (logFile != NULL) { fclose(logFile); } logFile = fopen(logFilePath, "a"); if (logFile == NULL) { fprintf(stderr, "Unable to open logfile\n"); } } void clearLogs(void) { if (logFile != NULL) { closeLogger(); } if (remove(logFilePath) != 0) { fprintf(stderr, "Unable to remove logfile\n"); } initLogger(logFilePath); } __attribute__((__format__ (__printf__, 2, 0))) void writeLog(const char* tag, const char* format, ...) { time_t now = time(NULL); struct tm* timeinfo = localtime(&now); char timestamp[20]; va_list args; strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", timeinfo); va_start(args, format); fprintf(logFile, "%s %s ", timestamp, tag); vfprintf(logFile, format, args); fprintf(logFile, "\n"); va_end(args); fflush(logFile); } void closeLogger(void) { if (logFile != NULL) { fclose(logFile); logFile = NULL; } }