2 * Copyright (c) 2017, The OpenThread Authors.
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.
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.
29 #include "common/logging.hpp"
41 #include "common/time.hpp"
43 static int sLevel = LOG_INFO;
45 /** Get the current debug log level */
46 int otbrLogGetLevel(void)
51 /** Initialize logging */
52 void otbrLogInit(const char *aIdent, int aLevel, bool aPrintStderr)
55 assert(aLevel >= LOG_EMERG && aLevel <= LOG_DEBUG);
57 openlog(aIdent, (LOG_CONS | LOG_PID) | (aPrintStderr ? LOG_PERROR : 0), LOG_USER);
61 /** log to the syslog or log file */
62 void otbrLog(int aLevel, const char *aFormat, ...)
66 va_start(ap, aFormat);
67 otbrLogv(aLevel, aFormat, ap);
71 /** log to the syslog or log file */
72 void otbrLogv(int aLevel, const char *aFormat, va_list ap)
78 vsyslog(aLevel, aFormat, ap);
82 /** Hex dump data to the log */
83 void otbrDump(int aLevel, const char *aPrefix, const void *aMemory, size_t aSize)
85 static const char kHexChars[] = "0123456789abcdef";
86 assert(aPrefix && (aMemory || aSize == 0));
96 /* break hex dumps into 16byte lines
97 * In the form ADDR: XX XX XX XX ...
100 // we pre-increment... so subtract
106 char hex[16 * 3 + 1];
109 p8 = (const uint8_t *)(aMemory) + addr;
111 /* truncate line to max 16 bytes */
117 aSize = aSize - this_size;
121 for (pEnd = p8 + this_size; p8 < pEnd; p8++)
123 *++ch = kHexChars[(*p8) >> 4];
124 *++ch = kHexChars[(*p8) & 0x0f];
129 syslog(aLevel, "%s: %04x: %s", aPrefix, addr, hex);
133 const char *otbrErrorString(otbrError aError)
139 case OTBR_ERROR_NONE:
143 case OTBR_ERROR_ERRNO:
144 error = strerror(errno);
147 case OTBR_ERROR_DBUS:
148 error = "DBUS error";
151 case OTBR_ERROR_MDNS:
152 error = "MDNS error";
155 case OTBR_ERROR_OPENTHREAD:
156 error = "OpenThread error";
159 case OTBR_ERROR_NOT_FOUND:
163 case OTBR_ERROR_PARSE:
164 error = "Parse error";
167 case OTBR_ERROR_NOT_IMPLEMENTED:
168 error = "Not implemented";
171 case OTBR_ERROR_INVALID_ARGS:
172 error = "Invalid arguments";
182 void otbrLogDeinit(void)