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