• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *    Copyright (c) 2017, The OpenThread Authors.
3  *    All rights reserved.
4  *
5  *    Redistribution and use in source and binary forms, with or without
6  *    modification, are permitted provided that the following conditions are met:
7  *    1. Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *    2. Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *    3. Neither the name of the copyright holder nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *    POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /**
30  * @file
31  * This file define logging interface.
32  */
33 #ifndef OTBR_COMMON_LOGGING_HPP_
34 #define OTBR_COMMON_LOGGING_HPP_
35 
36 #include "openthread-br/config.h"
37 
38 #include <stdarg.h>
39 #include <stddef.h>
40 
41 #ifndef OTBR_LOG_TAG
42 #error "OTBR_LOG_TAG is not defined"
43 #endif
44 
45 #include "common/types.hpp"
46 
47 /**
48  * Logging level.
49  *
50  */
51 typedef enum
52 {
53     OTBR_LOG_EMERG,   ///< System is unusable
54     OTBR_LOG_ALERT,   ///< Action must be taken immediately
55     OTBR_LOG_CRIT,    ///< Critical conditions
56     OTBR_LOG_ERR,     ///< Error conditions
57     OTBR_LOG_WARNING, ///< Warning conditions
58     OTBR_LOG_NOTICE,  ///< Normal but significant condition
59     OTBR_LOG_INFO,    ///< Informational
60     OTBR_LOG_DEBUG,   ///< Debug level messages
61 } otbrLogLevel;
62 
63 /**
64  * Get current log level.
65  */
66 otbrLogLevel otbrLogGetLevel(void);
67 
68 /**
69  * Set current log level.
70  */
71 void otbrLogSetLevel(otbrLogLevel aLevel);
72 
73 /**
74  * Control log to syslog.
75  *
76  * @param[in] enable  True to log to/via syslog.
77  *
78  */
79 void otbrLogEnableSyslog(bool aEnabled);
80 
81 /**
82  * This function initialize the logging service.
83  *
84  * @param[in] aIdent        Identity of the logger.
85  * @param[in] aLevel        Log level of the logger.
86  * @param[in] aPrintStderr  Whether to log to stderr.
87  *
88  */
89 void otbrLogInit(const char *aIdent, otbrLogLevel aLevel, bool aPrintStderr);
90 
91 /**
92  * This function log at level @p aLevel.
93  *
94  * @param[in] aLevel   Log level of the logger.
95  * @param[in] aLogTag  Log tag.
96  * @param[in] aFormat  Format string as in printf.
97  *
98  */
99 void otbrLog(otbrLogLevel aLevel, const char *aLogTag, const char *aFormat, ...);
100 
101 /**
102  * This function log at level @p aLevel.
103  *
104  * @param[in] aLevel    Log level of the logger.
105  * @param[in] aFormat   Format string as in printf.
106  * @param[in] aArgList  The variable-length arguments list.
107  *
108  */
109 void otbrLogv(otbrLogLevel aLevel, const char *aFormat, va_list aArgList);
110 
111 /**
112  * This function writes logs without filtering with the log level.
113  *
114  * @param[in] aLevel    Log level of the logger.
115  * @param[in] aFormat   Format string as in printf.
116  * @param[in] aArgList  The variable-length arguments list.
117  *
118  */
119 void otbrLogvNoFilter(otbrLogLevel aLevel, const char *aFormat, va_list aArgList);
120 
121 /**
122  * This function dump memory as hex string at level @p aLevel.
123  *
124  * @param[in] aLevel   Log level of the logger.
125  * @param[in] aLogTag  Log tag.
126  * @param[in] aPrefix  String before dumping memory.
127  * @param[in] aMemory  The pointer to the memory to be dumped.
128  * @param[in] aSize    The size of memory in bytes to be dumped.
129  *
130  */
131 void otbrDump(otbrLogLevel aLevel, const char *aLogTag, const char *aPrefix, const void *aMemory, size_t aSize);
132 
133 /**
134  * This function converts error code to string.
135  *
136  * @param[in] aError  The error code.
137  *
138  * @returns The string information of error.
139  *
140  */
141 const char *otbrErrorString(otbrError aError);
142 
143 /**
144  * This function deinitializes the logging service.
145  *
146  */
147 void otbrLogDeinit(void);
148 
149 /**
150  * This macro log an action result according to @p aError.
151  *
152  * If @p aError is OTBR_ERROR_NONE, the log level will be OTBR_LOG_INFO,
153  * otherwise OTBR_LOG_WARNING.
154  *
155  * @param[in] aError   The action result.
156  * @param[in] aFormat  Format string as in printf.
157  * @param[in] ...      Arguments for the format specification.
158  *
159  */
160 #define otbrLogResult(aError, aFormat, ...)                                                               \
161     do                                                                                                    \
162     {                                                                                                     \
163         otbrError _err = (aError);                                                                        \
164         otbrLog(_err == OTBR_ERROR_NONE ? OTBR_LOG_INFO : OTBR_LOG_WARNING, OTBR_LOG_TAG, aFormat ": %s", \
165                 ##__VA_ARGS__, otbrErrorString(_err));                                                    \
166     } while (0)
167 
168 /**
169  * @def otbrLogEmerg
170  *
171  * Log at level emergency.
172  *
173  * @param[in] ...  Arguments for the format specification.
174  *
175  */
176 
177 /**
178  * @def otbrLogAlert
179  *
180  * Log at level alert.
181  *
182  * @param[in] ...  Arguments for the format specification.
183  *
184  */
185 
186 /**
187  * @def otbrLogCrit
188  *
189  * Log at level critical.
190  *
191  * @param[in] ...  Arguments for the format specification.
192  *
193  */
194 
195 /**
196  * @def otbrLogErr
197  *
198  * Log at level error.
199  *
200  * @param[in] ...  Arguments for the format specification.
201  *
202  */
203 
204 /**
205  * @def otbrLogWarning
206  *
207  * Log at level warning.
208  *
209  * @param[in] ...  Arguments for the format specification.
210  *
211  */
212 
213 /**
214  * @def otbrLogNotice
215  *
216  * Log at level notice.
217  *
218  * @param[in] ...  Arguments for the format specification.
219  *
220  */
221 
222 /**
223  * @def otbrLogInfo
224  *
225  * Log at level information.
226  *
227  * @param[in] ...  Arguments for the format specification.
228  *
229  */
230 
231 /**
232  * @def otbrLogDebug
233  *
234  * Log at level debug.
235  *
236  * @param[in] ...  Arguments for the format specification.
237  *
238  */
239 #define otbrLogEmerg(...) otbrLog(OTBR_LOG_EMERG, OTBR_LOG_TAG, __VA_ARGS__)
240 #define otbrLogAlert(...) otbrLog(OTBR_LOG_ALERT, OTBR_LOG_TAG, __VA_ARGS__)
241 #define otbrLogCrit(...) otbrLog(OTBR_LOG_CRIT, OTBR_LOG_TAG, __VA_ARGS__)
242 #define otbrLogErr(...) otbrLog(OTBR_LOG_ERR, OTBR_LOG_TAG, __VA_ARGS__)
243 #define otbrLogWarning(...) otbrLog(OTBR_LOG_WARNING, OTBR_LOG_TAG, __VA_ARGS__)
244 #define otbrLogNotice(...) otbrLog(OTBR_LOG_NOTICE, OTBR_LOG_TAG, __VA_ARGS__)
245 #define otbrLogInfo(...) otbrLog(OTBR_LOG_INFO, OTBR_LOG_TAG, __VA_ARGS__)
246 #define otbrLogDebug(...) otbrLog(OTBR_LOG_DEBUG, OTBR_LOG_TAG, __VA_ARGS__)
247 
248 #endif // OTBR_COMMON_LOGGING_HPP_
249