1  /*
2  * Copyright (C) 2017-2024 Alibaba Group Holding Limited
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 /******************************************************************************
20  * @file     syslog.h
21  * @brief    Defines syslog APIs and usage
22  * @version  V1.1
23  * @date     14. February 2019
24  * @usage    Add 3 lines codes below at head of source code file
25  *           // 0: Err; 1: Err&Warn; 2: Err&Warn&Info; 3: Err&Warn&Info&Debug
26  *           #define LOG_LEVEL 3
27  *           #include <syslog.h>
28  ******************************************************************************/
29 #include <stdio.h>
30 
31 #ifndef _SYSLOG_H_
32 #define _SYSLOG_H_
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 #ifdef LOG_LEVEL
39 #if (LOG_LEVEL >= 3) && \
40     (defined CONFIG_SYSLOG_LEVEL_DEBUG)
41 #define LOG_ENABLE_D
42 #endif
43 
44 #if (LOG_LEVEL >= 2) && \
45     (defined CONFIG_SYSLOG_LEVEL_DEBUG || \
46      defined CONFIG_SYSLOG_LEVEL_INFO)
47 #define LOG_ENABLE_I
48 #endif
49 
50 #if (LOG_LEVEL >= 1) && \
51     (defined CONFIG_SYSLOG_LEVEL_DEBUG || \
52      defined CONFIG_SYSLOG_LEVEL_INFO  || \
53      defined CONFIG_SYSLOG_LEVEL_WARN)
54 #define LOG_ENABLE_W
55 #endif
56 
57 #if (LOG_LEVEL >= 0) && \
58     (defined CONFIG_SYSLOG_LEVEL_DEBUG || \
59      defined CONFIG_SYSLOG_LEVEL_INFO  || \
60      defined CONFIG_SYSLOG_LEVEL_WARN  || \
61      defined CONFIG_SYSLOG_LEVEL_ERROR)
62 #define LOG_ENABLE_E
63 #endif
64 #endif  /* #ifdef LOG_LEVEL */
65 
66 /* [LogLevel:FileName:Function:Line] */
67 extern const char *PFORMAT_D;
68 extern const char *PFORMAT_I;
69 extern const char *PFORMAT_W;
70 extern const char *PFORMAT_E;
71 
72 #define LOG_E_BASE_ARGS __FUNCTION__, __LINE__
73 #define LOG_W_BASE_ARGS __FUNCTION__, __LINE__
74 #define LOG_I_BASE_ARGS __FUNCTION__, __LINE__
75 #define LOG_D_BASE_ARGS __FUNCTION__, __LINE__
76 
77 /* Log in freely format without prefix */
78 #define LOG_F(fmt, args...) printf(fmt,##args)
79 
80 /* Log debug */
81 #ifdef LOG_ENABLE_D
82 #define LOG_D(fmt, args...) \
83     do {printf(PFORMAT_D,LOG_D_BASE_ARGS); printf(fmt,##args);} while(0)
84 #else
85 #define LOG_D(fmt, args...)
86 #endif
87 
88 /* Log information */
89 #ifdef LOG_ENABLE_I
90 #define LOG_I(fmt, args...) \
91     do {printf(PFORMAT_I ,LOG_I_BASE_ARGS); printf(fmt,##args);} while(0)
92 #else
93 #define LOG_I(fmt, args...)
94 #endif
95 
96 /* Log warning */
97 #ifdef LOG_ENABLE_W
98 #define LOG_W(fmt, args...) \
99     do {printf(PFORMAT_W,LOG_W_BASE_ARGS); printf(fmt,##args);} while(0)
100 #else
101 #define LOG_W(fmt, args...)
102 #endif
103 
104 /* Log error */
105 #ifdef LOG_ENABLE_E
106 #define LOG_E(fmt, args...) \
107     do {printf(PFORMAT_E,LOG_E_BASE_ARGS); printf(fmt,##args);} while(0)
108 #else
109 #define LOG_E(fmt, args...)
110 #endif
111 
112 #define ENTER()         LOG_D("Enter\n")
113 #define EXIT_VOID()     do { LOG_D("Exit\n"); return;} while(0)
114 #define EXIT_INT(val)   do { LOG_D("Exit, return val=%d\n", (int)val); return val;} while(0)
115 #define EXIT_PTR(ptr)   do { LOG_D("Exit, return ptr=%p\n", (void*)ptr); return ptr;} while(0)
116 
117 #ifdef __cplusplus
118 }
119 #endif
120 
121 #endif /* _SYSLOG_H_ */
122