zephyr/subsys/logging/log_minimal.c
Josh Gao 9f2916b943 logging: fix LOG_HEXDUMP_* in C++.
Previously, there were two issues when attempting to use LOG_HEXDUMP_*
from C++:

First, gcc and clang in C mode both allow implicit pointer conversion
by default, but require -fpermissive, which no one should ever use, in
C++ mode. Furthermore, -Wpointer-sign, the warning emitted in C for
convertion between pointers to types of different signedness (e.g. char*
vs u8_t*) is explicitly disabled in Zephyr. Switch the various hexdump
functions to void*, which is guaranteed to work in both languages.

Second, the soon-to-be-standardized C++20 version of designated
initializers requires that the designators appear in the same order as
they are declared in the type being initialized.

Signed-off-by: Josh Gao <josh@jmgao.dev>
2019-12-18 21:54:18 +01:00

52 lines
973 B
C

/*
* Copyright (c) 2019 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <sys/printk.h>
#include <ctype.h>
#include <logging/log.h>
#define HEXDUMP_BYTES_IN_LINE 8
static void minimal_hexdump_line_print(const char *data, size_t length)
{
for (int i = 0; i < HEXDUMP_BYTES_IN_LINE; i++) {
if (i < length) {
printk("%02x ", data[i] & 0xFF);
} else {
printk(" ");
}
}
printk("|");
for (int i = 0; i < HEXDUMP_BYTES_IN_LINE; i++) {
if (i < length) {
char c = data[i];
printk("%c", isprint((int)c) ? c : '.');
} else {
printk(" ");
}
}
printk("\n");
}
void log_minimal_hexdump_print(int level, const void *_data, size_t size)
{
const char *data = (const char *)_data;
while (size > 0) {
printk("%c: ", z_log_minimal_level_to_char(level));
minimal_hexdump_line_print(data, size);
if (size < HEXDUMP_BYTES_IN_LINE) {
break;
}
size -= HEXDUMP_BYTES_IN_LINE;
data += HEXDUMP_BYTES_IN_LINE;
}
}