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/locator_getters.hpp"
39 #include "common/log.hpp"
40 #include "common/string.hpp"
41 #include "instance/instance.hpp"
42 
43 using namespace ot;
44 
otLoggingGetLevel(void)45 otLogLevel otLoggingGetLevel(void) { return static_cast<otLogLevel>(Instance::GetLogLevel()); }
46 
47 #if OPENTHREAD_CONFIG_LOG_LEVEL_DYNAMIC_ENABLE
otLoggingSetLevel(otLogLevel aLogLevel)48 otError otLoggingSetLevel(otLogLevel aLogLevel)
49 {
50     Error error = kErrorNone;
51 
52     VerifyOrExit(aLogLevel <= kLogLevelDebg && aLogLevel >= kLogLevelNone, error = kErrorInvalidArgs);
53     Instance::SetLogLevel(static_cast<LogLevel>(aLogLevel));
54 
55 exit:
56     return error;
57 }
58 #endif
59 
60 static const char kPlatformModuleName[] = "Platform";
61 
otLogCritPlat(const char * aFormat,...)62 void otLogCritPlat(const char *aFormat, ...)
63 {
64 #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_CRIT) && OPENTHREAD_CONFIG_LOG_PLATFORM
65     va_list args;
66 
67     va_start(args, aFormat);
68     Logger::LogVarArgs(kPlatformModuleName, kLogLevelCrit, aFormat, args);
69     va_end(args);
70 #else
71     OT_UNUSED_VARIABLE(aFormat);
72     OT_UNUSED_VARIABLE(kPlatformModuleName);
73 #endif
74 }
75 
otLogWarnPlat(const char * aFormat,...)76 void otLogWarnPlat(const char *aFormat, ...)
77 {
78 #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_WARN) && OPENTHREAD_CONFIG_LOG_PLATFORM
79     va_list args;
80 
81     va_start(args, aFormat);
82     Logger::LogVarArgs(kPlatformModuleName, kLogLevelWarn, aFormat, args);
83     va_end(args);
84 #else
85     OT_UNUSED_VARIABLE(aFormat);
86 #endif
87 }
88 
otLogNotePlat(const char * aFormat,...)89 void otLogNotePlat(const char *aFormat, ...)
90 {
91 #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_NOTE) && OPENTHREAD_CONFIG_LOG_PLATFORM
92     va_list args;
93 
94     va_start(args, aFormat);
95     Logger::LogVarArgs(kPlatformModuleName, kLogLevelNote, aFormat, args);
96     va_end(args);
97 #else
98     OT_UNUSED_VARIABLE(aFormat);
99 #endif
100 }
101 
otLogInfoPlat(const char * aFormat,...)102 void otLogInfoPlat(const char *aFormat, ...)
103 {
104 #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_INFO) && OPENTHREAD_CONFIG_LOG_PLATFORM
105     va_list args;
106 
107     va_start(args, aFormat);
108     Logger::LogVarArgs(kPlatformModuleName, kLogLevelInfo, aFormat, args);
109     va_end(args);
110 #else
111     OT_UNUSED_VARIABLE(aFormat);
112 #endif
113 }
114 
otLogDebgPlat(const char * aFormat,...)115 void otLogDebgPlat(const char *aFormat, ...)
116 {
117 #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_DEBG) && OPENTHREAD_CONFIG_LOG_PLATFORM
118     va_list args;
119 
120     va_start(args, aFormat);
121     Logger::LogVarArgs(kPlatformModuleName, kLogLevelDebg, aFormat, args);
122     va_end(args);
123 #else
124     OT_UNUSED_VARIABLE(aFormat);
125 #endif
126 }
127 
otDumpCritPlat(const char * aText,const void * aData,uint16_t aDataLength)128 void otDumpCritPlat(const char *aText, const void *aData, uint16_t aDataLength)
129 {
130 #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_CRIT) && OPENTHREAD_CONFIG_LOG_PLATFORM && OPENTHREAD_CONFIG_LOG_PKT_DUMP
131     Logger::DumpInModule(kPlatformModuleName, kLogLevelCrit, aText, aData, aDataLength);
132 #else
133     OT_UNUSED_VARIABLE(aText);
134     OT_UNUSED_VARIABLE(aData);
135     OT_UNUSED_VARIABLE(aDataLength);
136 #endif
137 }
138 
otDumpWarnPlat(const char * aText,const void * aData,uint16_t aDataLength)139 void otDumpWarnPlat(const char *aText, const void *aData, uint16_t aDataLength)
140 {
141 #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_WARN) && OPENTHREAD_CONFIG_LOG_PLATFORM && OPENTHREAD_CONFIG_LOG_PKT_DUMP
142     Logger::DumpInModule(kPlatformModuleName, kLogLevelWarn, aText, aData, aDataLength);
143 #else
144     OT_UNUSED_VARIABLE(aText);
145     OT_UNUSED_VARIABLE(aData);
146     OT_UNUSED_VARIABLE(aDataLength);
147 #endif
148 }
149 
otDumpNotePlat(const char * aText,const void * aData,uint16_t aDataLength)150 void otDumpNotePlat(const char *aText, const void *aData, uint16_t aDataLength)
151 {
152 #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_NOTE) && OPENTHREAD_CONFIG_LOG_PLATFORM && OPENTHREAD_CONFIG_LOG_PKT_DUMP
153     Logger::DumpInModule(kPlatformModuleName, kLogLevelNote, aText, aData, aDataLength);
154 #else
155     OT_UNUSED_VARIABLE(aText);
156     OT_UNUSED_VARIABLE(aData);
157     OT_UNUSED_VARIABLE(aDataLength);
158 #endif
159 }
160 
otDumpInfoPlat(const char * aText,const void * aData,uint16_t aDataLength)161 void otDumpInfoPlat(const char *aText, const void *aData, uint16_t aDataLength)
162 {
163 #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_INFO) && OPENTHREAD_CONFIG_LOG_PLATFORM && OPENTHREAD_CONFIG_LOG_PKT_DUMP
164     Logger::DumpInModule(kPlatformModuleName, kLogLevelInfo, aText, aData, aDataLength);
165 #else
166     OT_UNUSED_VARIABLE(aText);
167     OT_UNUSED_VARIABLE(aData);
168     OT_UNUSED_VARIABLE(aDataLength);
169 #endif
170 }
171 
otDumpDebgPlat(const char * aText,const void * aData,uint16_t aDataLength)172 void otDumpDebgPlat(const char *aText, const void *aData, uint16_t aDataLength)
173 {
174 #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_DEBG) && OPENTHREAD_CONFIG_LOG_PLATFORM && OPENTHREAD_CONFIG_LOG_PKT_DUMP
175     Logger::DumpInModule(kPlatformModuleName, kLogLevelDebg, aText, aData, aDataLength);
176 #else
177     OT_UNUSED_VARIABLE(aText);
178     OT_UNUSED_VARIABLE(aData);
179     OT_UNUSED_VARIABLE(aDataLength);
180 #endif
181 }
182 
otLogPlat(otLogLevel aLogLevel,const char * aPlatModuleName,const char * aFormat,...)183 void otLogPlat(otLogLevel aLogLevel, const char *aPlatModuleName, const char *aFormat, ...)
184 {
185 #if OPENTHREAD_CONFIG_LOG_PLATFORM
186     va_list args;
187 
188     va_start(args, aFormat);
189     otLogPlatArgs(aLogLevel, aPlatModuleName, aFormat, args);
190     va_end(args);
191 #else
192     OT_UNUSED_VARIABLE(aLogLevel);
193     OT_UNUSED_VARIABLE(aPlatModuleName);
194     OT_UNUSED_VARIABLE(aFormat);
195 #endif
196 }
197 
otLogPlatArgs(otLogLevel aLogLevel,const char * aPlatModuleName,const char * aFormat,va_list aArgs)198 void otLogPlatArgs(otLogLevel aLogLevel, const char *aPlatModuleName, const char *aFormat, va_list aArgs)
199 {
200 #if OT_SHOULD_LOG && OPENTHREAD_CONFIG_LOG_PLATFORM
201     String<kMaxLogModuleNameLength> moduleName;
202 
203     OT_ASSERT(aLogLevel >= kLogLevelNone && aLogLevel <= kLogLevelDebg);
204 
205     moduleName.Append("P-%s", aPlatModuleName);
206     Logger::LogVarArgs(moduleName.AsCString(), static_cast<LogLevel>(aLogLevel), aFormat, aArgs);
207 #else
208     OT_UNUSED_VARIABLE(aLogLevel);
209     OT_UNUSED_VARIABLE(aPlatModuleName);
210     OT_UNUSED_VARIABLE(aFormat);
211     OT_UNUSED_VARIABLE(aArgs);
212 #endif
213 }
214 
otLogCli(otLogLevel aLogLevel,const char * aFormat,...)215 void otLogCli(otLogLevel aLogLevel, const char *aFormat, ...)
216 {
217 #if OT_SHOULD_LOG && OPENTHREAD_CONFIG_LOG_CLI
218     static const char kCliModuleName[] = "Cli";
219 
220     va_list args;
221 
222     OT_ASSERT(aLogLevel >= kLogLevelNone && aLogLevel <= kLogLevelDebg);
223     VerifyOrExit(aLogLevel >= kLogLevelNone && aLogLevel <= kLogLevelDebg);
224 
225     va_start(args, aFormat);
226     Logger::LogVarArgs(kCliModuleName, static_cast<LogLevel>(aLogLevel), aFormat, args);
227     va_end(args);
228 exit:
229 #else
230     OT_UNUSED_VARIABLE(aLogLevel);
231     OT_UNUSED_VARIABLE(aFormat);
232 #endif
233     return;
234 }
235 
otLogGenerateNextHexDumpLine(otLogHexDumpInfo * aInfo)236 otError otLogGenerateNextHexDumpLine(otLogHexDumpInfo *aInfo)
237 {
238     AssertPointerIsNotNull(aInfo);
239 
240     return GenerateNextHexDumpLine(*aInfo);
241 }
242