1 /*
2 * Copyright (c) 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 * This file implements the OpenThread logging related APIs.
32 */
33
34 #include "openthread-core-config.h"
35
36 #include "common/code_utils.hpp"
37 #include "common/debug.hpp"
38 #include "common/instance.hpp"
39 #include "common/locator_getters.hpp"
40 #include "common/log.hpp"
41
42 using namespace ot;
43
otLoggingGetLevel(void)44 otLogLevel otLoggingGetLevel(void) { return static_cast<otLogLevel>(Instance::GetLogLevel()); }
45
46 #if OPENTHREAD_CONFIG_LOG_LEVEL_DYNAMIC_ENABLE
otLoggingSetLevel(otLogLevel aLogLevel)47 otError otLoggingSetLevel(otLogLevel aLogLevel)
48 {
49 Error error = kErrorNone;
50
51 VerifyOrExit(aLogLevel <= kLogLevelDebg && aLogLevel >= kLogLevelNone, error = kErrorInvalidArgs);
52 Instance::SetLogLevel(static_cast<LogLevel>(aLogLevel));
53
54 exit:
55 return error;
56 }
57 #endif
58
59 static const char kPlatformModuleName[] = "Platform";
60
otLogCritPlat(const char * aFormat,...)61 void otLogCritPlat(const char *aFormat, ...)
62 {
63 #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_CRIT) && OPENTHREAD_CONFIG_LOG_PLATFORM
64 va_list args;
65
66 va_start(args, aFormat);
67 Logger::LogVarArgs(kPlatformModuleName, kLogLevelCrit, aFormat, args);
68 va_end(args);
69 #else
70 OT_UNUSED_VARIABLE(aFormat);
71 OT_UNUSED_VARIABLE(kPlatformModuleName);
72 #endif
73 }
74
otLogWarnPlat(const char * aFormat,...)75 void otLogWarnPlat(const char *aFormat, ...)
76 {
77 #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_WARN) && OPENTHREAD_CONFIG_LOG_PLATFORM
78 va_list args;
79
80 va_start(args, aFormat);
81 Logger::LogVarArgs(kPlatformModuleName, kLogLevelWarn, aFormat, args);
82 va_end(args);
83 #else
84 OT_UNUSED_VARIABLE(aFormat);
85 #endif
86 }
87
otLogNotePlat(const char * aFormat,...)88 void otLogNotePlat(const char *aFormat, ...)
89 {
90 #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_NOTE) && OPENTHREAD_CONFIG_LOG_PLATFORM
91 va_list args;
92
93 va_start(args, aFormat);
94 Logger::LogVarArgs(kPlatformModuleName, kLogLevelNote, aFormat, args);
95 va_end(args);
96 #else
97 OT_UNUSED_VARIABLE(aFormat);
98 #endif
99 }
100
otLogInfoPlat(const char * aFormat,...)101 void otLogInfoPlat(const char *aFormat, ...)
102 {
103 #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_INFO) && OPENTHREAD_CONFIG_LOG_PLATFORM
104 va_list args;
105
106 va_start(args, aFormat);
107 Logger::LogVarArgs(kPlatformModuleName, kLogLevelInfo, aFormat, args);
108 va_end(args);
109 #else
110 OT_UNUSED_VARIABLE(aFormat);
111 #endif
112 }
113
otLogDebgPlat(const char * aFormat,...)114 void otLogDebgPlat(const char *aFormat, ...)
115 {
116 #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_DEBG) && OPENTHREAD_CONFIG_LOG_PLATFORM
117 va_list args;
118
119 va_start(args, aFormat);
120 Logger::LogVarArgs(kPlatformModuleName, kLogLevelDebg, aFormat, args);
121 va_end(args);
122 #else
123 OT_UNUSED_VARIABLE(aFormat);
124 #endif
125 }
126
otDumpCritPlat(const char * aText,const void * aData,uint16_t aDataLength)127 void otDumpCritPlat(const char *aText, const void *aData, uint16_t aDataLength)
128 {
129 #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_CRIT) && OPENTHREAD_CONFIG_LOG_PLATFORM && OPENTHREAD_CONFIG_LOG_PKT_DUMP
130 Logger::DumpInModule(kPlatformModuleName, kLogLevelCrit, aText, aData, aDataLength);
131 #else
132 OT_UNUSED_VARIABLE(aText);
133 OT_UNUSED_VARIABLE(aData);
134 OT_UNUSED_VARIABLE(aDataLength);
135 #endif
136 }
137
otDumpWarnPlat(const char * aText,const void * aData,uint16_t aDataLength)138 void otDumpWarnPlat(const char *aText, const void *aData, uint16_t aDataLength)
139 {
140 #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_WARN) && OPENTHREAD_CONFIG_LOG_PLATFORM && OPENTHREAD_CONFIG_LOG_PKT_DUMP
141 Logger::DumpInModule(kPlatformModuleName, kLogLevelWarn, aText, aData, aDataLength);
142 #else
143 OT_UNUSED_VARIABLE(aText);
144 OT_UNUSED_VARIABLE(aData);
145 OT_UNUSED_VARIABLE(aDataLength);
146 #endif
147 }
148
otDumpNotePlat(const char * aText,const void * aData,uint16_t aDataLength)149 void otDumpNotePlat(const char *aText, const void *aData, uint16_t aDataLength)
150 {
151 #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_NOTE) && OPENTHREAD_CONFIG_LOG_PLATFORM && OPENTHREAD_CONFIG_LOG_PKT_DUMP
152 Logger::DumpInModule(kPlatformModuleName, kLogLevelNote, aText, aData, aDataLength);
153 #else
154 OT_UNUSED_VARIABLE(aText);
155 OT_UNUSED_VARIABLE(aData);
156 OT_UNUSED_VARIABLE(aDataLength);
157 #endif
158 }
159
otDumpInfoPlat(const char * aText,const void * aData,uint16_t aDataLength)160 void otDumpInfoPlat(const char *aText, const void *aData, uint16_t aDataLength)
161 {
162 #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_INFO) && OPENTHREAD_CONFIG_LOG_PLATFORM && OPENTHREAD_CONFIG_LOG_PKT_DUMP
163 Logger::DumpInModule(kPlatformModuleName, kLogLevelInfo, aText, aData, aDataLength);
164 #else
165 OT_UNUSED_VARIABLE(aText);
166 OT_UNUSED_VARIABLE(aData);
167 OT_UNUSED_VARIABLE(aDataLength);
168 #endif
169 }
170
otDumpDebgPlat(const char * aText,const void * aData,uint16_t aDataLength)171 void otDumpDebgPlat(const char *aText, const void *aData, uint16_t aDataLength)
172 {
173 #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_DEBG) && OPENTHREAD_CONFIG_LOG_PLATFORM && OPENTHREAD_CONFIG_LOG_PKT_DUMP
174 Logger::DumpInModule(kPlatformModuleName, kLogLevelDebg, aText, aData, aDataLength);
175 #else
176 OT_UNUSED_VARIABLE(aText);
177 OT_UNUSED_VARIABLE(aData);
178 OT_UNUSED_VARIABLE(aDataLength);
179 #endif
180 }
181
otLogCli(otLogLevel aLogLevel,const char * aFormat,...)182 void otLogCli(otLogLevel aLogLevel, const char *aFormat, ...)
183 {
184 #if OT_SHOULD_LOG && OPENTHREAD_CONFIG_LOG_CLI
185 static const char kCliModuleName[] = "Cli";
186
187 va_list args;
188
189 OT_ASSERT(aLogLevel >= kLogLevelNone && aLogLevel <= kLogLevelDebg);
190 VerifyOrExit(aLogLevel >= kLogLevelNone && aLogLevel <= kLogLevelDebg);
191
192 va_start(args, aFormat);
193 Logger::LogVarArgs(kCliModuleName, static_cast<LogLevel>(aLogLevel), aFormat, args);
194 va_end(args);
195 exit:
196 #else
197 OT_UNUSED_VARIABLE(aLogLevel);
198 OT_UNUSED_VARIABLE(aFormat);
199 #endif
200 return;
201 }
202