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 <xiangyang.wu@intel.com> Reviewed-by: fei1.li@intel.com
This commit is contained in:
parent
4e8adc7b14
commit
7fa42ff484
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Copyright (C)2021 Intel Corporation
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
#ifndef _LOG_H_
|
||||
#define _LOG_H_
|
||||
#include <time.h>
|
||||
|
||||
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
|
Loading…
Reference in New Issue