1 /*
2  * Copyright (C) 2017-2019 Alibaba Group Holding Limited
3  */
4 
5 /******************************************************************************
6  * @file     syslog.h
7  * @brief    Defines syslog APIs and usage
8  * @version  V1.1
9  * @date     14. February 2019
10  * @usage    Add 3 lines codes below at head of source code file
11  *           // 0: Err; 1: Err&Warn; 2: Err&Warn&Info; 3: Err&Warn&Info&Debug
12  *           #define LOG_LEVEL 3
13  *           #include <syslog.h>
14  ******************************************************************************/
15 #include <stdio.h>
16 #include <csi_config.h>
17 
18 #ifndef _SYSLOG_H_
19 #define _SYSLOG_H_
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 #ifdef LOG_LEVEL
26 #if (LOG_LEVEL >= 3) && \
27     (defined CONFIG_SYSLOG_LEVEL_DEBUG)
28 #define LOG_ENABLE_D
29 #endif
30 
31 #if (LOG_LEVEL >= 2) && \
32     (defined CONFIG_SYSLOG_LEVEL_DEBUG || \
33      defined CONFIG_SYSLOG_LEVEL_INFO)
34 #define LOG_ENABLE_I
35 #endif
36 
37 #if (LOG_LEVEL >= 1) && \
38     (defined CONFIG_SYSLOG_LEVEL_DEBUG || \
39      defined CONFIG_SYSLOG_LEVEL_INFO  || \
40      defined CONFIG_SYSLOG_LEVEL_WARN)
41 #define LOG_ENABLE_W
42 #endif
43 
44 #if (LOG_LEVEL >= 0) && \
45     (defined CONFIG_SYSLOG_LEVEL_DEBUG || \
46      defined CONFIG_SYSLOG_LEVEL_INFO  || \
47      defined CONFIG_SYSLOG_LEVEL_WARN  || \
48      defined CONFIG_SYSLOG_LEVEL_ERROR)
49 #define LOG_ENABLE_E
50 #endif
51 #endif  /* #ifdef LOG_LEVEL */
52 
53 /* [LogLevel:FileName:Function:Line] */
54 extern const char *PFORMAT_D;
55 extern const char *PFORMAT_I;
56 extern const char *PFORMAT_W;
57 extern const char *PFORMAT_E;
58 
59 #define LOG_E_BASE_ARGS __FUNCTION__, __LINE__
60 #define LOG_W_BASE_ARGS __FUNCTION__, __LINE__
61 #define LOG_I_BASE_ARGS __FUNCTION__, __LINE__
62 #define LOG_D_BASE_ARGS __FUNCTION__, __LINE__
63 
64 /* Log in freely format without prefix */
65 #define LOG_F(fmt, args...) printf(fmt,##args)
66 
67 /* Log debug */
68 #ifdef LOG_ENABLE_D
69 #define LOG_D(fmt, args...) \
70     do {printf(PFORMAT_D,LOG_D_BASE_ARGS); printf(fmt,##args);} while(0)
71 #else
72 #define LOG_D(fmt, args...)
73 #endif
74 
75 /* Log information */
76 #ifdef LOG_ENABLE_I
77 #define LOG_I(fmt, args...) \
78     do {printf(PFORMAT_I ,LOG_I_BASE_ARGS); printf(fmt,##args);} while(0)
79 #else
80 #define LOG_I(fmt, args...)
81 #endif
82 
83 /* Log warning */
84 #ifdef LOG_ENABLE_W
85 #define LOG_W(fmt, args...) \
86     do {printf(PFORMAT_W,LOG_W_BASE_ARGS); printf(fmt,##args);} while(0)
87 #else
88 #define LOG_W(fmt, args...)
89 #endif
90 
91 /* Log error */
92 #ifdef LOG_ENABLE_E
93 #define LOG_E(fmt, args...) \
94     do {printf(PFORMAT_E,LOG_E_BASE_ARGS); printf(fmt,##args);} while(0)
95 #else
96 #define LOG_E(fmt, args...)
97 #endif
98 
99 #define ENTER()         LOG_D("Enter\n")
100 #define EXIT_VOID()     do { LOG_D("Exit\n"); return;} while(0)
101 #define EXIT_INT(val)   do { LOG_D("Exit, return val=%d\n", (int)val); return val;} while(0)
102 #define EXIT_PTR(ptr)   do { LOG_D("Exit, return ptr=%p\n", (void*)ptr); return ptr;} while(0)
103 
104 #ifdef __cplusplus
105 }
106 #endif
107 
108 #endif /* _SYSLOG_H_ */
109