From 7fa42ff484ee6ff75a2d229b4ac6a198ea10ae30 Mon Sep 17 00:00:00 2001 From: Xiangyang Wu Date: Tue, 19 Oct 2021 10:33:11 +0800 Subject: [PATCH] misc: life_mngr: add log module In the log module, the following functions are implemented: - open_log Provide one interface to open log file; -close_log Provide one interface to close log file; - LOG_PRINTF LOG_WRITE Provide interfaces to write log message to log file. v1-->v3: Add timestamp in the log message. Tracked-On: #6652 Signed-off-by: Xiangyang Wu Reviewed-by: fei1.li@intel.com --- misc/services/life_mngr/log.h | 44 +++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 misc/services/life_mngr/log.h diff --git a/misc/services/life_mngr/log.h b/misc/services/life_mngr/log.h new file mode 100644 index 000000000..edc4f9b98 --- /dev/null +++ b/misc/services/life_mngr/log.h @@ -0,0 +1,44 @@ +/* + * Copyright (C)2021 Intel Corporation + * SPDX-License-Identifier: BSD-3-Clause + */ +#ifndef _LOG_H_ +#define _LOG_H_ +#include + +extern FILE *log_fd; +static inline void output_timestamp(void) +{ + struct tm *t; + time_t tt; + + time(&tt); + t = localtime(&tt); + fprintf(log_fd, "[%4d-%02d-%02d %02d:%02d:%02d]", t->tm_year + 1900, \ + t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec); +} +#define LOG_PRINTF(format, args...) \ +do { output_timestamp(); \ + fprintf(log_fd, format, args); \ + fflush(log_fd); } while (0) + +#define LOG_WRITE(args) \ +do { output_timestamp(); \ + fwrite(args, 1, sizeof(args), log_fd); \ + fflush(log_fd); } while (0) + +static inline bool open_log(const char *path) +{ + bool ret = false; + + log_fd = fopen("/var/log/life_mngr.log", "a+"); + if (log_fd != NULL) + ret = true; + return ret; +} +static inline void close_log(void) +{ + if (log_fd != NULL) + fclose(log_fd); +} +#endif