• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *    Copyright (c) 2017, 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 #define OTBR_LOG_TAG "LOG"
30 
31 #ifndef OTBR_SYSLOG_FACILITY_ID
32 #define OTBR_SYSLOG_FACILITY_ID LOG_USER
33 #endif
34 
35 #include "common/logging.hpp"
36 
37 #include <assert.h>
38 #include <errno.h>
39 #include <stdarg.h>
40 #include <stdint.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <sys/time.h>
45 #include <syslog.h>
46 
47 #include <sstream>
48 
49 #include "common/code_utils.hpp"
50 #include "common/time.hpp"
51 
52 static otbrLogLevel sLevel            = OTBR_LOG_INFO;
53 static const char   sLevelString[][8] = {
54     "[EMERG]", "[ALERT]", "[CRIT]", "[ERR ]", "[WARN]", "[NOTE]", "[INFO]", "[DEBG]",
55 };
56 
57 /** Get the current debug log level */
otbrLogGetLevel(void)58 otbrLogLevel otbrLogGetLevel(void)
59 {
60     return sLevel;
61 }
62 
63 /**
64  * Set current log level.
65  */
otbrLogSetLevel(otbrLogLevel aLevel)66 void otbrLogSetLevel(otbrLogLevel aLevel)
67 {
68     assert(aLevel >= OTBR_LOG_EMERG && aLevel <= OTBR_LOG_DEBUG);
69     sLevel = aLevel;
70 }
71 
72 /** Initialize logging */
otbrLogInit(const char * aIdent,otbrLogLevel aLevel,bool aPrintStderr)73 void otbrLogInit(const char *aIdent, otbrLogLevel aLevel, bool aPrintStderr)
74 {
75     assert(aIdent);
76     assert(aLevel >= OTBR_LOG_EMERG && aLevel <= OTBR_LOG_DEBUG);
77 
78     openlog(aIdent, (LOG_CONS | LOG_PID) | (aPrintStderr ? LOG_PERROR : 0), OTBR_SYSLOG_FACILITY_ID);
79     sLevel = aLevel;
80 }
81 
GetPrefix(const char * aLogTag)82 static const char *GetPrefix(const char *aLogTag)
83 {
84     // Log prefix format : -xxx-----
85     const uint8_t kMaxTagSize = 7;
86     const uint8_t kBufferSize = kMaxTagSize + 3;
87     static char   prefix[kBufferSize];
88     uint8_t       tagLength = strlen(aLogTag) > kMaxTagSize ? kMaxTagSize : strlen(aLogTag);
89     int           index     = 0;
90 
91     if (strlen(aLogTag) > 0)
92     {
93         prefix[0] = '-';
94         memcpy(&prefix[1], aLogTag, tagLength);
95 
96         index = tagLength + 1;
97 
98         memset(&prefix[index], '-', kMaxTagSize - tagLength + 1);
99         index += kMaxTagSize - tagLength + 1;
100     }
101 
102     prefix[index++] = '\0';
103 
104     return prefix;
105 }
106 
107 /** log to the syslog or log file */
otbrLog(otbrLogLevel aLevel,const char * aLogTag,const char * aFormat,...)108 void otbrLog(otbrLogLevel aLevel, const char *aLogTag, const char *aFormat, ...)
109 {
110     const uint16_t kBufferSize = 1024;
111     va_list        ap;
112     char           buffer[kBufferSize];
113 
114     va_start(ap, aFormat);
115 
116     if ((aLevel <= sLevel) && (vsnprintf(buffer, sizeof(buffer), aFormat, ap) > 0))
117     {
118         syslog(static_cast<int>(aLevel), "%s%s: %s", sLevelString[aLevel], GetPrefix(aLogTag), buffer);
119     }
120 
121     va_end(ap);
122 
123     return;
124 }
125 
126 /** log to the syslog or log file */
otbrLogv(otbrLogLevel aLevel,const char * aFormat,va_list aArgList)127 void otbrLogv(otbrLogLevel aLevel, const char *aFormat, va_list aArgList)
128 {
129     assert(aFormat);
130 
131     if (aLevel <= sLevel)
132     {
133         otbrLogvNoFilter(aLevel, aFormat, aArgList);
134     }
135 }
136 
otbrLogvNoFilter(otbrLogLevel aLevel,const char * aFormat,va_list aArgList)137 void otbrLogvNoFilter(otbrLogLevel aLevel, const char *aFormat, va_list aArgList)
138 {
139     vsyslog(static_cast<int>(aLevel), aFormat, aArgList);
140 }
141 
142 /** Hex dump data to the log */
otbrDump(otbrLogLevel aLevel,const char * aLogTag,const char * aPrefix,const void * aMemory,size_t aSize)143 void otbrDump(otbrLogLevel aLevel, const char *aLogTag, const char *aPrefix, const void *aMemory, size_t aSize)
144 {
145     static const char kHexChars[] = "0123456789abcdef";
146     assert(aPrefix && (aMemory || aSize == 0));
147     const uint8_t *pEnd;
148     const uint8_t *p8;
149     int            addr;
150 
151     if (aLevel >= sLevel)
152     {
153         return;
154     }
155 
156     /* break hex dumps into 16byte lines
157      * In the form ADDR: XX XX XX XX ...
158      */
159 
160     // we pre-increment... so subtract
161     addr = -16;
162 
163     while (aSize > 0)
164     {
165         size_t this_size;
166         char   hex[16 * 3 + 1];
167 
168         addr = addr + 16;
169         p8   = (const uint8_t *)(aMemory) + addr;
170 
171         /* truncate line to max 16 bytes */
172         this_size = aSize;
173         if (this_size > 16)
174         {
175             this_size = 16;
176         }
177         aSize = aSize - this_size;
178 
179         char *ch = hex - 1;
180 
181         for (pEnd = p8 + this_size; p8 < pEnd; p8++)
182         {
183             *++ch = kHexChars[(*p8) >> 4];
184             *++ch = kHexChars[(*p8) & 0x0f];
185             *++ch = ' ';
186         }
187         *ch = 0;
188 
189         otbrLog(aLevel, aLogTag, "%s: %04x: %s", aPrefix, addr, hex);
190     }
191 }
192 
otbrErrorString(otbrError aError)193 const char *otbrErrorString(otbrError aError)
194 {
195     const char *error;
196 
197     switch (aError)
198     {
199     case OTBR_ERROR_NONE:
200         error = "OK";
201         break;
202 
203     case OTBR_ERROR_ERRNO:
204         error = strerror(errno);
205         break;
206 
207     case OTBR_ERROR_DBUS:
208         error = "DBUS error";
209         break;
210 
211     case OTBR_ERROR_MDNS:
212         error = "MDNS error";
213         break;
214 
215     case OTBR_ERROR_OPENTHREAD:
216         error = "OpenThread error";
217         break;
218 
219     case OTBR_ERROR_NOT_FOUND:
220         error = "Not found";
221         break;
222 
223     case OTBR_ERROR_PARSE:
224         error = "Parse error";
225         break;
226 
227     case OTBR_ERROR_NOT_IMPLEMENTED:
228         error = "Not implemented";
229         break;
230 
231     case OTBR_ERROR_INVALID_ARGS:
232         error = "Invalid arguments";
233         break;
234 
235     case OTBR_ERROR_DUPLICATED:
236         error = "Duplicated";
237         break;
238 
239     case OTBR_ERROR_ABORTED:
240         error = "Aborted";
241         break;
242 
243     case OTBR_ERROR_INVALID_STATE:
244         error = "Invalid state";
245         break;
246 
247     default:
248         error = "Unknown";
249     }
250 
251     return error;
252 }
253 
otbrLogDeinit(void)254 void otbrLogDeinit(void)
255 {
256     closelog();
257 }
258