38#if !defined(ARDUINO_ARCH_MBED) && !defined(ARDUINO_ARCH_ZEPHYR)
39#define PLATFORM_HAS_PRINTF
43#if !defined(ARDUINO_ARCH_ESP32) && defined(LOG_PORT) && defined(ARDUINO) \
44 && !defined(ARDUINO_ARCH_MBED) && !defined(ARDUINO_ARCH_ZEPHYR)
46#define SENSORLIB_LOG_FILE_LINE __FILE__, __LINE__
48#define SENSORLIB_LOG_E(fmt, ...) LOG_PORT.printf("[E][%s:%d] " fmt "\n", SENSORLIB_LOG_FILE_LINE, ##__VA_ARGS__)
49#define SENSORLIB_LOG_I(fmt, ...) LOG_PORT.printf("[I][%s:%d] " fmt "\n", SENSORLIB_LOG_FILE_LINE, ##__VA_ARGS__)
50#define SENSORLIB_LOG_D(fmt, ...) LOG_PORT.printf("[D][%s:%d] " fmt "\n", SENSORLIB_LOG_FILE_LINE, ##__VA_ARGS__)
51#define SENSORLIB_LOG_W(fmt, ...) LOG_PORT.printf("[W][%s:%d] " fmt "\n", SENSORLIB_LOG_FILE_LINE, ##__VA_ARGS__)
53static inline void _dump_buffer(
const uint8_t *
buf,
size_t size)
55 const size_t bytesPerLine = 16;
56 for (
size_t i = 0;
i < size;
i += bytesPerLine) {
57 LOG_PORT.printf(
"%08x ", (
unsigned int)
i);
58 for (
size_t j = 0; j < bytesPerLine; ++j) {
60 LOG_PORT.printf(
"%02x ",
buf[
i + j]);
67 for (
size_t j = 0; j < bytesPerLine; ++j) {
69 LOG_PORT.printf(
"%c", isprint(
buf[
i + j]) ? (
char)
buf[
i + j] :
'.');
73 LOG_PORT.printf(
"\n");
77#define SENSORLIB_DUMP_BUFFER(buf, size) _dump_buffer((const uint8_t *)(buf), (size_t)(size))
80#elif defined(ARDUINO_ARCH_MBED) || defined(ARDUINO_ARCH_ZEPHYR)
82#define SENSORLIB_LOG_FILE_LINE __FILE__, __LINE__
84#define SENSORLIB_LOG_E(fmt, ...) printf("[E][%s:%d] " fmt "\n", SENSORLIB_LOG_FILE_LINE, ##__VA_ARGS__)
85#define SENSORLIB_LOG_I(fmt, ...) printf("[I][%s:%d] " fmt "\n", SENSORLIB_LOG_FILE_LINE, ##__VA_ARGS__)
86#define SENSORLIB_LOG_D(fmt, ...) printf("[D][%s:%d] " fmt "\n", SENSORLIB_LOG_FILE_LINE, ##__VA_ARGS__)
87#define SENSORLIB_LOG_W(fmt, ...) printf("[W][%s:%d] " fmt "\n", SENSORLIB_LOG_FILE_LINE, ##__VA_ARGS__)
89static inline void _dump_buffer(
const uint8_t *
buf,
size_t size)
91 const size_t bytesPerLine = 16;
92 for (
size_t i = 0;
i < size;
i += bytesPerLine) {
93 printf(
"%08x ", (
unsigned int)
i);
94 for (
size_t j = 0; j < bytesPerLine; ++j) {
96 printf(
"%02x ",
buf[
i + j]);
103 for (
size_t j = 0; j < bytesPerLine; ++j) {
105 printf(
"%c", isprint(
buf[
i + j]) ? (
char)
buf[
i + j] :
'.');
113#define SENSORLIB_DUMP_BUFFER(buf, size) _dump_buffer((const uint8_t *)(buf), (size_t)(size))
117#elif defined(ESP_PLATFORM) && defined(ARDUINO)
119#define SENSORLIB_LOG_E(format, ...) log_e(format, ##__VA_ARGS__)
120#define SENSORLIB_LOG_W(format, ...) log_w(format, ##__VA_ARGS__)
121#define SENSORLIB_LOG_I(format, ...) log_i(format, ##__VA_ARGS__)
122#define SENSORLIB_LOG_D(format, ...) log_d(format, ##__VA_ARGS__)
123#define SENSORLIB_LOG_V(format, ...) log_v(format, ##__VA_ARGS__)
126static inline void _dump_buffer(
const uint8_t *
buf,
size_t size)
128 const size_t bytesPerLine = 16;
129 for (
size_t i = 0;
i < size;
i += bytesPerLine) {
130 char hex[3 * 16 + 2 + 1] = {0};
131 char ascii[16 + 1] = {0};
132 size_t pos_h = 0, pos_a = 0;
133 for (
size_t j = 0; j < bytesPerLine; ++j) {
135 pos_h += snprintf(hex + pos_h,
sizeof(hex) - pos_h,
"%02x ",
buf[
i + j]);
136 ascii[pos_a++] = isprint(
buf[
i + j]) ? (char)
buf[
i + j] :
'.';
138 pos_h += snprintf(hex + pos_h,
sizeof(hex) - pos_h,
" ");
139 ascii[pos_a++] =
' ';
148 log_d(
"%08x %s %s", (
unsigned int)
i, hex, ascii);
152#define SENSORLIB_DUMP_BUFFER(buf, size) _dump_buffer((const uint8_t *)(buf), (size_t)(size))
155#elif defined(ESP_PLATFORM) && !defined(ARDUINO)
159#define SENSORLIB_LOG_TAG "SensorLib"
161#if defined(__cplusplus) && (__cplusplus > 201703L)
162#define SENSORLIB_LOG_E(format, ...) ESP_LOG_LEVEL_LOCAL(ESP_LOG_ERROR, SENSORLIB_LOG_TAG, format __VA_OPT__(,) __VA_ARGS__)
163#define SENSORLIB_LOG_W(format, ...) ESP_LOG_LEVEL_LOCAL(ESP_LOG_WARN, SENSORLIB_LOG_TAG, format __VA_OPT__(,) __VA_ARGS__)
164#define SENSORLIB_LOG_I(format, ...) ESP_LOG_LEVEL_LOCAL(ESP_LOG_INFO, SENSORLIB_LOG_TAG, format __VA_OPT__(,) __VA_ARGS__)
165#define SENSORLIB_LOG_D(format, ...) ESP_LOG_LEVEL_LOCAL(ESP_LOG_DEBUG, SENSORLIB_LOG_TAG, format __VA_OPT__(,) __VA_ARGS__)
166#define SENSORLIB_LOG_V(format, ...) ESP_LOG_LEVEL_LOCAL(ESP_LOG_VERBOSE, SENSORLIB_LOG_TAG, format __VA_OPT__(,) __VA_ARGS__)
168#define SENSORLIB_LOG_E(format, ...) ESP_LOG_LEVEL_LOCAL(ESP_LOG_ERROR, SENSORLIB_LOG_TAG, format, ##__VA_ARGS__)
169#define SENSORLIB_LOG_W(format, ...) ESP_LOG_LEVEL_LOCAL(ESP_LOG_WARN, SENSORLIB_LOG_TAG, format, ##__VA_ARGS__)
170#define SENSORLIB_LOG_I(format, ...) ESP_LOG_LEVEL_LOCAL(ESP_LOG_INFO, SENSORLIB_LOG_TAG, format, ##__VA_ARGS__)
171#define SENSORLIB_LOG_D(format, ...) ESP_LOG_LEVEL_LOCAL(ESP_LOG_DEBUG, SENSORLIB_LOG_TAG, format, ##__VA_ARGS__)
172#define SENSORLIB_LOG_V(format, ...) ESP_LOG_LEVEL_LOCAL(ESP_LOG_VERBOSE, SENSORLIB_LOG_TAG, format, ##__VA_ARGS__)
175static inline void _dump_buffer(
const uint8_t *
buf,
size_t size)
177 const size_t bytesPerLine = 16;
178 for (
size_t i = 0;
i < size;
i += bytesPerLine) {
179 char hex[3 * 16 + 2 + 1] = {0};
180 char ascii[16 + 1] = {0};
181 size_t pos_h = 0, pos_a = 0;
182 for (
size_t j = 0; j < bytesPerLine; ++j) {
184 pos_h += snprintf(hex + pos_h,
sizeof(hex) - pos_h,
"%02x ",
buf[
i + j]);
185 ascii[pos_a++] = isprint(
buf[
i + j]) ? (char)
buf[
i + j] :
'.';
187 pos_h += snprintf(hex + pos_h,
sizeof(hex) - pos_h,
" ");
188 ascii[pos_a++] =
' ';
197 ESP_LOG_LEVEL_LOCAL(ESP_LOG_DEBUG, SENSORLIB_LOG_TAG,
198 "%08x %s %s", (
unsigned int)
i, hex, ascii);
202#define SENSORLIB_DUMP_BUFFER(buf, size) _dump_buffer((const uint8_t *)(buf), (size_t)(size))
207#define SENSORLIB_LOG_E(...)
208#define SENSORLIB_LOG_I(...)
209#define SENSORLIB_LOG_D(...)
210#define SENSORLIB_LOG_W(...)
212#define SENSORLIB_DUMP_BUFFER(buf, size)