1 /*
2  * Copyright (C)2021-2022 Intel Corporation.
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 #ifndef _LOG_H_
6 #define _LOG_H_
7 #include <time.h>
8 
9 extern FILE *log_fd;
output_timestamp(void)10 static inline void output_timestamp(void)
11 {
12 	struct tm *t;
13 	time_t tt;
14 
15 	time(&tt);
16 	t = localtime(&tt);
17 	fprintf(log_fd, "[%4d-%02d-%02d %02d:%02d:%02d]", t->tm_year + 1900, \
18 		t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
19 }
20 #define LOG_PRINTF(format, args...) \
21 do { output_timestamp(); \
22 	fprintf(log_fd, format, args); \
23 	fflush(log_fd); } while (0)
24 
25 #define LOG_WRITE(args) \
26 do { output_timestamp(); \
27 	fwrite(args, 1, sizeof(args), log_fd); \
28 	fflush(log_fd); } while (0)
29 
open_log(const char * path)30 static inline bool open_log(const char *path)
31 {
32 	bool ret = false;
33 
34 	log_fd = fopen("/var/log/life_mngr.log", "a+");
35 	if (log_fd != NULL)
36 		ret = true;
37 	return ret;
38 }
close_log(void)39 static inline void close_log(void)
40 {
41 	if (log_fd != NULL)
42 		fclose(log_fd);
43 }
44 #endif
45