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 
17 #ifndef _SYSLOG_H_
18 #define _SYSLOG_H_
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #ifdef LOG_LEVEL
25 #if (LOG_LEVEL >= 3) && \
26     (defined CONFIG_SYSLOG_LEVEL_DEBUG)
27 #define LOG_ENABLE_D
28 #endif
29 
30 #if (LOG_LEVEL >= 2) && \
31     (defined CONFIG_SYSLOG_LEVEL_DEBUG || \
32      defined CONFIG_SYSLOG_LEVEL_INFO)
33 #define LOG_ENABLE_I
34 #endif
35 
36 #if (LOG_LEVEL >= 1) && \
37     (defined CONFIG_SYSLOG_LEVEL_DEBUG || \
38      defined CONFIG_SYSLOG_LEVEL_INFO  || \
39      defined CONFIG_SYSLOG_LEVEL_WARN)
40 #define LOG_ENABLE_W
41 #endif
42 
43 #if (LOG_LEVEL >= 0) && \
44     (defined CONFIG_SYSLOG_LEVEL_DEBUG || \
45      defined CONFIG_SYSLOG_LEVEL_INFO  || \
46      defined CONFIG_SYSLOG_LEVEL_WARN  || \
47      defined CONFIG_SYSLOG_LEVEL_ERROR)
48 #define LOG_ENABLE_E
49 #endif
50 #endif  /* #ifdef LOG_LEVEL */
51 
52 /* [LogLevel:FileName:Function:Line] */
53 extern const char *PFORMAT_D;
54 extern const char *PFORMAT_I;
55 extern const char *PFORMAT_W;
56 extern const char *PFORMAT_E;
57 
58 #define LOG_E_BASE_ARGS __FUNCTION__, __LINE__
59 #define LOG_W_BASE_ARGS __FUNCTION__, __LINE__
60 #define LOG_I_BASE_ARGS __FUNCTION__, __LINE__
61 #define LOG_D_BASE_ARGS __FUNCTION__, __LINE__
62 
63 /* Log in freely format without prefix */
64 #define LOG_F(fmt, args...) printf(fmt,##args)
65 
66 /* Log debug */
67 #ifdef LOG_ENABLE_D
68 #define LOG_D(fmt, args...) \
69     do {printf(PFORMAT_D,LOG_D_BASE_ARGS); printf(fmt,##args);} while(0)
70 #else
71 #define LOG_D(fmt, args...)
72 #endif
73 
74 /* Log information */
75 #ifdef LOG_ENABLE_I
76 #define LOG_I(fmt, args...) \
77     do {printf(PFORMAT_I ,LOG_I_BASE_ARGS); printf(fmt,##args);} while(0)
78 #else
79 #define LOG_I(fmt, args...)
80 #endif
81 
82 /* Log warning */
83 #ifdef LOG_ENABLE_W
84 #define LOG_W(fmt, args...) \
85     do {printf(PFORMAT_W,LOG_W_BASE_ARGS); printf(fmt,##args);} while(0)
86 #else
87 #define LOG_W(fmt, args...)
88 #endif
89 
90 /* Log error */
91 #ifdef LOG_ENABLE_E
92 #define LOG_E(fmt, args...) \
93     do {printf(PFORMAT_E,LOG_E_BASE_ARGS); printf(fmt,##args);} while(0)
94 #else
95 #define LOG_E(fmt, args...)
96 #endif
97 
98 #define ENTER()         LOG_D("Enter\n")
99 #define EXIT_VOID()     do { LOG_D("Exit\n"); return;} while(0)
100 #define EXIT_INT(val)   do { LOG_D("Exit, return val=%d\n", (int)val); return val;} while(0)
101 #define EXIT_PTR(ptr)   do { LOG_D("Exit, return ptr=%p\n", (void*)ptr); return ptr;} while(0)
102 
103 #ifdef __cplusplus
104 }
105 #endif
106 
107 #endif /* _SYSLOG_H_ */
108