• 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  * Get default log level.
70  */
71 otbrLogLevel otbrLogGetDefaultLevel(void);
72 
73 /**
74  * Set current log level.
75  */
76 void otbrLogSetLevel(otbrLogLevel aLevel);
77 
78 /**
79  * Control log to syslog.
80  *
81  * @param[in] aEnabled  True to enable logging to/via syslog.
82  *
83  */
84 void otbrLogSyslogSetEnabled(bool aEnabled);
85 
86 /**
87  * This function initialize the logging service.
88  *
89  * @param[in] aProgramName    The name of this runnable program.
90  * @param[in] aLevel          Log level of the logger.
91  * @param[in] aPrintStderr    Whether to log to stderr.
92  * @param[in] aSyslogDisable  Whether to disable logging to syslog.
93  *
94  */
95 void otbrLogInit(const char *aProgramName, otbrLogLevel aLevel, bool aPrintStderr, bool aSyslogDisable);
96 
97 /**
98  * This function log at level @p aLevel.
99  *
100  * @param[in] aLevel   Log level of the logger.
101  * @param[in] aLogTag  Log tag.
102  * @param[in] aFormat  Format string as in printf.
103  *
104  */
105 void otbrLog(otbrLogLevel aLevel, const char *aLogTag, const char *aFormat, ...);
106 
107 /**
108  * This function log at level @p aLevel.
109  *
110  * @param[in] aLevel    Log level of the logger.
111  * @param[in] aFormat   Format string as in printf.
112  * @param[in] aArgList  The variable-length arguments list.
113  *
114  */
115 void otbrLogv(otbrLogLevel aLevel, const char *aFormat, va_list aArgList);
116 
117 /**
118  * This function writes logs without filtering with the log level.
119  *
120  * @param[in] aLevel    Log level of the logger.
121  * @param[in] aFormat   Format string as in printf.
122  * @param[in] aArgList  The variable-length arguments list.
123  *
124  */
125 void otbrLogvNoFilter(otbrLogLevel aLevel, const char *aFormat, va_list aArgList);
126 
127 /**
128  * This function dump memory as hex string at level @p aLevel.
129  *
130  * @param[in] aLevel   Log level of the logger.
131  * @param[in] aLogTag  Log tag.
132  * @param[in] aPrefix  String before dumping memory.
133  * @param[in] aMemory  The pointer to the memory to be dumped.
134  * @param[in] aSize    The size of memory in bytes to be dumped.
135  *
136  */
137 void otbrDump(otbrLogLevel aLevel, const char *aLogTag, const char *aPrefix, const void *aMemory, size_t aSize);
138 
139 /**
140  * This function converts error code to string.
141  *
142  * @param[in] aError  The error code.
143  *
144  * @returns The string information of error.
145  *
146  */
147 const char *otbrErrorString(otbrError aError);
148 
149 /**
150  * This function deinitializes the logging service.
151  *
152  */
153 void otbrLogDeinit(void);
154 
155 /**
156  * This macro log an action result according to @p aError.
157  *
158  * If @p aError is OTBR_ERROR_NONE, the log level will be OTBR_LOG_INFO,
159  * otherwise OTBR_LOG_WARNING.
160  *
161  * @param[in] aError   The action result.
162  * @param[in] aFormat  Format string as in printf.
163  * @param[in] ...      Arguments for the format specification.
164  *
165  */
166 #define otbrLogResult(aError, aFormat, ...)                                                               \
167     do                                                                                                    \
168     {                                                                                                     \
169         otbrError _err = (aError);                                                                        \
170         otbrLog(_err == OTBR_ERROR_NONE ? OTBR_LOG_INFO : OTBR_LOG_WARNING, OTBR_LOG_TAG, aFormat ": %s", \
171                 ##__VA_ARGS__, otbrErrorString(_err));                                                    \
172     } while (0)
173 
174 /**
175  * @def otbrLogEmerg
176  *
177  * Log at level emergency.
178  *
179  * @param[in] ...  Arguments for the format specification.
180  *
181  */
182 
183 /**
184  * @def otbrLogAlert
185  *
186  * Log at level alert.
187  *
188  * @param[in] ...  Arguments for the format specification.
189  *
190  */
191 
192 /**
193  * @def otbrLogCrit
194  *
195  * Log at level critical.
196  *
197  * @param[in] ...  Arguments for the format specification.
198  *
199  */
200 
201 /**
202  * @def otbrLogErr
203  *
204  * Log at level error.
205  *
206  * @param[in] ...  Arguments for the format specification.
207  *
208  */
209 
210 /**
211  * @def otbrLogWarning
212  *
213  * Log at level warning.
214  *
215  * @param[in] ...  Arguments for the format specification.
216  *
217  */
218 
219 /**
220  * @def otbrLogNotice
221  *
222  * Log at level notice.
223  *
224  * @param[in] ...  Arguments for the format specification.
225  *
226  */
227 
228 /**
229  * @def otbrLogInfo
230  *
231  * Log at level information.
232  *
233  * @param[in] ...  Arguments for the format specification.
234  *
235  */
236 
237 /**
238  * @def otbrLogDebug
239  *
240  * Log at level debug.
241  *
242  * @param[in] ...  Arguments for the format specification.
243  *
244  */
245 #define otbrLogEmerg(...) otbrLog(OTBR_LOG_EMERG, OTBR_LOG_TAG, __VA_ARGS__)
246 #define otbrLogAlert(...) otbrLog(OTBR_LOG_ALERT, OTBR_LOG_TAG, __VA_ARGS__)
247 #define otbrLogCrit(...) otbrLog(OTBR_LOG_CRIT, OTBR_LOG_TAG, __VA_ARGS__)
248 #define otbrLogErr(...) otbrLog(OTBR_LOG_ERR, OTBR_LOG_TAG, __VA_ARGS__)
249 #define otbrLogWarning(...) otbrLog(OTBR_LOG_WARNING, OTBR_LOG_TAG, __VA_ARGS__)
250 #define otbrLogNotice(...) otbrLog(OTBR_LOG_NOTICE, OTBR_LOG_TAG, __VA_ARGS__)
251 #define otbrLogInfo(...) otbrLog(OTBR_LOG_INFO, OTBR_LOG_TAG, __VA_ARGS__)
252 #define otbrLogDebug(...) otbrLog(OTBR_LOG_DEBUG, OTBR_LOG_TAG, __VA_ARGS__)
253 
254 #endif // OTBR_COMMON_LOGGING_HPP_
255