Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / ot-br-posix / repo / src / common / logging.cpp
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 #include "common/logging.hpp"
30
31 #include <assert.h>
32 #include <errno.h>
33 #include <stdarg.h>
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/time.h>
39 #include <syslog.h>
40
41 #include "common/time.hpp"
42
43 static int sLevel = LOG_INFO;
44
45 /** Get the current debug log level */
46 int otbrLogGetLevel(void)
47 {
48     return sLevel;
49 }
50
51 /** Initialize logging */
52 void otbrLogInit(const char *aIdent, int aLevel, bool aPrintStderr)
53 {
54     assert(aIdent);
55     assert(aLevel >= LOG_EMERG && aLevel <= LOG_DEBUG);
56
57     openlog(aIdent, (LOG_CONS | LOG_PID) | (aPrintStderr ? LOG_PERROR : 0), LOG_USER);
58     sLevel = aLevel;
59 }
60
61 /** log to the syslog or log file */
62 void otbrLog(int aLevel, const char *aFormat, ...)
63 {
64     va_list ap;
65
66     va_start(ap, aFormat);
67     otbrLogv(aLevel, aFormat, ap);
68     va_end(ap);
69 }
70
71 /** log to the syslog or log file */
72 void otbrLogv(int aLevel, const char *aFormat, va_list ap)
73 {
74     assert(aFormat);
75
76     if (aLevel <= sLevel)
77     {
78         vsyslog(aLevel, aFormat, ap);
79     }
80 }
81
82 /** Hex dump data to the log */
83 void otbrDump(int aLevel, const char *aPrefix, const void *aMemory, size_t aSize)
84 {
85     static const char kHexChars[] = "0123456789abcdef";
86     assert(aPrefix && (aMemory || aSize == 0));
87     const uint8_t *pEnd;
88     const uint8_t *p8;
89     int            addr;
90
91     if (aLevel >= sLevel)
92     {
93         return;
94     }
95
96     /* break hex dumps into 16byte lines
97      * In the form ADDR: XX XX XX XX ...
98      */
99
100     // we pre-increment... so subtract
101     addr = -16;
102
103     while (aSize > 0)
104     {
105         size_t this_size;
106         char   hex[16 * 3 + 1];
107
108         addr = addr + 16;
109         p8   = (const uint8_t *)(aMemory) + addr;
110
111         /* truncate line to max 16 bytes */
112         this_size = aSize;
113         if (this_size > 16)
114         {
115             this_size = 16;
116         }
117         aSize = aSize - this_size;
118
119         char *ch = hex - 1;
120
121         for (pEnd = p8 + this_size; p8 < pEnd; p8++)
122         {
123             *++ch = kHexChars[(*p8) >> 4];
124             *++ch = kHexChars[(*p8) & 0x0f];
125             *++ch = ' ';
126         }
127         *ch = 0;
128
129         syslog(aLevel, "%s: %04x: %s", aPrefix, addr, hex);
130     }
131 }
132
133 const char *otbrErrorString(otbrError aError)
134 {
135     const char *error;
136
137     switch (aError)
138     {
139     case OTBR_ERROR_NONE:
140         error = "OK";
141         break;
142
143     case OTBR_ERROR_ERRNO:
144         error = strerror(errno);
145         break;
146
147     case OTBR_ERROR_DBUS:
148         error = "DBUS error";
149         break;
150
151     case OTBR_ERROR_MDNS:
152         error = "MDNS error";
153         break;
154
155     case OTBR_ERROR_OPENTHREAD:
156         error = "OpenThread error";
157         break;
158
159     case OTBR_ERROR_NOT_FOUND:
160         error = "Not found";
161         break;
162
163     case OTBR_ERROR_PARSE:
164         error = "Parse error";
165         break;
166
167     case OTBR_ERROR_NOT_IMPLEMENTED:
168         error = "Not implemented";
169         break;
170
171     case OTBR_ERROR_INVALID_ARGS:
172         error = "Invalid arguments";
173         break;
174
175     default:
176         error = "Unknown";
177     }
178
179     return error;
180 }
181
182 void otbrLogDeinit(void)
183 {
184     closelog();
185 }