1 /*
2  *  Copyright (c) 2016-2018, 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  * @brief
32  *   This file includes OpenThread logging related definitions.
33  */
34 
35 #ifndef OPENTHREAD_LOGGING_H_
36 #define OPENTHREAD_LOGGING_H_
37 
38 #include <openthread/error.h>
39 #include <openthread/platform/logging.h>
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 /**
46  * @addtogroup api-logging
47  *
48  * @brief
49  *   This module includes OpenThread logging related definitions.
50  *
51  * @{
52  *
53  */
54 
55 /**
56  * Returns the current log level.
57  *
58  * If dynamic log level feature `OPENTHREAD_CONFIG_LOG_LEVEL_DYNAMIC_ENABLE` is enabled, this function returns the
59  * currently set dynamic log level. Otherwise, this function returns the build-time configured log level.
60  *
61  * @returns The log level.
62  *
63  */
64 otLogLevel otLoggingGetLevel(void);
65 
66 /**
67  * Sets the log level.
68  *
69  * @note This function requires `OPENTHREAD_CONFIG_LOG_LEVEL_DYNAMIC_ENABLE=1`.
70  *
71  * @param[in]  aLogLevel               The log level.
72  *
73  * @retval OT_ERROR_NONE            Successfully updated log level.
74  * @retval OT_ERROR_INVALID_ARGS    Log level value is invalid.
75  *
76  */
77 otError otLoggingSetLevel(otLogLevel aLogLevel);
78 
79 /**
80  * Emits a log message at critical log level.
81  *
82  * Is intended for use by platform. If `OPENTHREAD_CONFIG_LOG_PLATFORM` is not set or the current log
83  * level is below critical, this function does not emit any log message.
84  *
85  * @param[in]  aFormat  The format string.
86  * @param[in]  ...      Arguments for the format specification.
87  *
88  */
89 void otLogCritPlat(const char *aFormat, ...) OT_TOOL_PRINTF_STYLE_FORMAT_ARG_CHECK(1, 2);
90 
91 /**
92  * Emits a log message at warning log level.
93  *
94  * Is intended for use by platform. If `OPENTHREAD_CONFIG_LOG_PLATFORM` is not set or the current log
95  * level is below warning, this function does not emit any log message.
96  *
97  * @param[in]  aFormat  The format string.
98  * @param[in]  ...      Arguments for the format specification.
99  *
100  */
101 void otLogWarnPlat(const char *aFormat, ...) OT_TOOL_PRINTF_STYLE_FORMAT_ARG_CHECK(1, 2);
102 
103 /**
104  * Emits a log message at note log level.
105  *
106  * Is intended for use by platform. If `OPENTHREAD_CONFIG_LOG_PLATFORM` is not set or the current log
107  * level is below note, this function does not emit any log message.
108  *
109  * @param[in]  aFormat  The format string.
110  * @param[in]  ...      Arguments for the format specification.
111  *
112  */
113 void otLogNotePlat(const char *aFormat, ...) OT_TOOL_PRINTF_STYLE_FORMAT_ARG_CHECK(1, 2);
114 
115 /**
116  * Emits a log message at info log level.
117  *
118  * Is intended for use by platform. If `OPENTHREAD_CONFIG_LOG_PLATFORM` is not set or the current log
119  * level is below info, this function does not emit any log message.
120  *
121  * @param[in]  aFormat  The format string.
122  * @param[in]  ...      Arguments for the format specification.
123  *
124  */
125 void otLogInfoPlat(const char *aFormat, ...) OT_TOOL_PRINTF_STYLE_FORMAT_ARG_CHECK(1, 2);
126 
127 /**
128  * Emits a log message at debug log level.
129  *
130  * Is intended for use by platform. If `OPENTHREAD_CONFIG_LOG_PLATFORM` is not set or the current log
131  * level is below debug, this function does not emit any log message.
132  *
133  * @param[in]  aFormat  The format string.
134  * @param[in]  ...      Arguments for the format specification.
135  *
136  */
137 void otLogDebgPlat(const char *aFormat, ...) OT_TOOL_PRINTF_STYLE_FORMAT_ARG_CHECK(1, 2);
138 
139 /**
140  * Generates a memory dump at critical log level.
141  *
142  * If `OPENTHREAD_CONFIG_LOG_PLATFORM` or `OPENTHREAD_CONFIG_LOG_PKT_DUMP` is not set or the current log level is below
143  * critical this function does not emit any log message.
144  *
145  * @param[in]  aText         A string that is printed before the bytes.
146  * @param[in]  aData         A pointer to the data buffer.
147  * @param[in]  aDataLength   Number of bytes in @p aData.
148  *
149  */
150 void otDumpCritPlat(const char *aText, const void *aData, uint16_t aDataLength);
151 
152 /**
153  * Generates a memory dump at warning log level.
154  *
155  * If `OPENTHREAD_CONFIG_LOG_PLATFORM` or `OPENTHREAD_CONFIG_LOG_PKT_DUMP` is not set or the current log level is below
156  * warning this function does not emit any log message.
157  *
158  * @param[in]  aText         A string that is printed before the bytes.
159  * @param[in]  aData         A pointer to the data buffer.
160  * @param[in]  aDataLength   Number of bytes in @p aData.
161  *
162  */
163 void otDumpWarnPlat(const char *aText, const void *aData, uint16_t aDataLength);
164 
165 /**
166  * Generates a memory dump at note log level.
167  *
168  * If `OPENTHREAD_CONFIG_LOG_PLATFORM` or `OPENTHREAD_CONFIG_LOG_PKT_DUMP` is not set or the current log level is below
169  * note this function does not emit any log message.
170  *
171  * @param[in]  aText         A string that is printed before the bytes.
172  * @param[in]  aData         A pointer to the data buffer.
173  * @param[in]  aDataLength   Number of bytes in @p aData.
174  *
175  */
176 void otDumpNotePlat(const char *aText, const void *aData, uint16_t aDataLength);
177 
178 /**
179  * Generates a memory dump at info log level.
180  *
181  * If `OPENTHREAD_CONFIG_LOG_PLATFORM` or `OPENTHREAD_CONFIG_LOG_PKT_DUMP` is not set or the current log level is below
182  * info this function does not emit any log message.
183  *
184  * @param[in]  aText         A string that is printed before the bytes.
185  * @param[in]  aData         A pointer to the data buffer.
186  * @param[in]  aDataLength   Number of bytes in @p aData.
187  *
188  */
189 void otDumpInfoPlat(const char *aText, const void *aData, uint16_t aDataLength);
190 
191 /**
192  * Generates a memory dump at debug log level.
193  *
194  * If `OPENTHREAD_CONFIG_LOG_PLATFORM` or `OPENTHREAD_CONFIG_LOG_PKT_DUMP` is not set or the current log level is below
195  * debug this function does not emit any log message.
196  *
197  * @param[in]  aText         A string that is printed before the bytes.
198  * @param[in]  aData         A pointer to the data buffer.
199  * @param[in]  aDataLength   Number of bytes in @p aData.
200  *
201  */
202 void otDumpDebgPlat(const char *aText, const void *aData, uint16_t aDataLength);
203 
204 /**
205  * Emits a log message at given log level using a platform module name.
206  *
207  * This is is intended for use by platform. If `OPENTHREAD_CONFIG_LOG_PLATFORM` is not set or the current log
208  * level is below @p aLogLevel , this function does not emit any log message.
209  *
210  * The @p aPlatModuleName name is used to determine the log module name in the emitted log message, following the
211  * `P-{PlatModuleName}---` format. This means that the prefix string "P-" is added to indicate that this is a platform
212  * sub-module, followed by the next 12 characters of the @p PlatModuleName string, with padded hyphens `-` at the end
213  * to ensure that the region name is 14 characters long.
214 
215  * @param[in] aLogLevel         The log level.
216  * @param[in] aPlatModuleName   The platform sub-module name.
217  * @param[in] aFormat           The format string.
218  * @param[in] ...               Arguments for the format specification.
219  *
220  */
221 void otLogPlat(otLogLevel aLogLevel, const char *aPlatModuleName, const char *aFormat, ...)
222     OT_TOOL_PRINTF_STYLE_FORMAT_ARG_CHECK(3, 4);
223 
224 /**
225  * Emits a log message at given log level using a platform module name.
226  *
227  * This is is intended for use by platform. If `OPENTHREAD_CONFIG_LOG_PLATFORM` is not set or the current log
228  * level is below @p aLogLevel , this function does not emit any log message.
229  *
230  * The @p aPlatModuleName name is used to determine the log module name in the emitted log message, following the
231  * `P-{PlatModuleName}---` format. This means that the prefix string "P-" is added to indicate that this is a platform
232  * sub-module, followed by the next 12 characters of the @p PlatModuleName string, with padded hyphens `-` at the end
233  * to ensure that the region name is 14 characters long.
234  *
235  * @param[in] aLogLevel         The log level.
236  * @param[in] aPlatModuleName   The platform sub-module name.
237  * @param[in] aFormat           The format string.
238  * @param[in] aArgs             Arguments for the format specification.
239  *
240  */
241 void otLogPlatArgs(otLogLevel aLogLevel, const char *aPlatModuleName, const char *aFormat, va_list aArgs);
242 
243 /**
244  * Emits a log message at a given log level.
245  *
246  * Is intended for use by CLI only. If `OPENTHREAD_CONFIG_LOG_CLI` is not set or the current log
247  * level is below the given log level, this function does not emit any log message.
248  *
249  * @param[in]  aLogLevel The log level.
250  * @param[in]  aFormat   The format string.
251  * @param[in]  ...       Arguments for the format specification.
252  *
253  */
254 void otLogCli(otLogLevel aLogLevel, const char *aFormat, ...) OT_TOOL_PRINTF_STYLE_FORMAT_ARG_CHECK(2, 3);
255 
256 #define OT_LOG_HEX_DUMP_LINE_SIZE 73 ///< Hex dump line string size.
257 
258 /**
259  * Represents information used for generating hex dump output.
260  *
261  */
262 typedef struct
263 {
264     const uint8_t *mDataBytes;                       ///< The data byes.
265     uint16_t       mDataLength;                      ///< The data length (number of bytes in @p mDataBytes)
266     const char    *mTitle;                           ///< Title string to add table header (MUST NOT be `NULL`).
267     char           mLine[OT_LOG_HEX_DUMP_LINE_SIZE]; ///< Buffer to output one line of generated hex dump.
268     uint16_t       mIterator;                        ///< Iterator used by OT stack. MUST be initialized to zero.
269 } otLogHexDumpInfo;
270 
271 /**
272  * Generates the next hex dump line.
273  *
274  * Can call this method back-to-back to generate the hex dump output line by line. On the first call the `mIterator`
275  * field in @p aInfo MUST be set to zero.
276  *
277  * Here is an example of the generated hex dump output:
278  *
279  *  "==========================[{mTitle} len=070]============================"
280  *  "| 41 D8 87 34 12 FF FF 25 | 4C 57 DA F2 FB 2F 62 7F | A..4...%LW.../b. |"
281  *  "| 3B 01 F0 4D 4C 4D 4C 54 | 4F 00 15 15 00 00 00 00 | ;..MLMLTO....... |"
282  *  "| 00 00 00 01 80 DB 60 82 | 7E 33 72 3B CC B3 A1 84 | ......`.~3r;.... |"
283  *  "| 3B E6 AD B2 0B 45 E7 45 | C5 B9 00 1A CB 2D 6D 1C | ;....E.E.....-m. |"
284  *  "| 10 3E 3C F5 D3 70       |                         | .><..p           |"
285  *  "------------------------------------------------------------------------"
286  *
287  * @param[in,out] aInfo        A pointer to `otLogHexDumpInfo` to use to generate hex dump.
288  *
289  * @retval OT_ERROR_NONE       Successfully generated the next line, `mLine` field in @p aInfo is updated.
290  * @retval OT_ERROR_NOT_FOUND  Reached the end and no more line to generate.
291  *
292  */
293 otError otLogGenerateNextHexDumpLine(otLogHexDumpInfo *aInfo);
294 
295 /**
296  * @}
297  *
298  */
299 
300 #ifdef __cplusplus
301 } // extern "C"
302 #endif
303 
304 #endif // OPENTHREAD_LOGGING_H_
305