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