SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
SensorLibLog.hpp
Go to the documentation of this file.
1
31#pragma once
32
33#include <stdint.h>
34#include <stddef.h>
35#include <ctype.h>
36#include <cstdio>
37
38#if !defined(ARDUINO_ARCH_MBED) && !defined(ARDUINO_ARCH_ZEPHYR)
39#define PLATFORM_HAS_PRINTF
40#endif
41
42// ─── Arduino (non-MBED, non-Zephyr) ────────────────────────────────────────
43#if !defined(ARDUINO_ARCH_ESP32) && defined(LOG_PORT) && defined(ARDUINO) \
44 && !defined(ARDUINO_ARCH_MBED) && !defined(ARDUINO_ARCH_ZEPHYR)
45
46#define SENSORLIB_LOG_FILE_LINE __FILE__, __LINE__
47
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__)
52
53static inline void _dump_buffer(const uint8_t *buf, size_t size)
54{
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) {
59 if (i + j < size)
60 LOG_PORT.printf("%02x ", buf[i + j]);
61 else
62 LOG_PORT.printf(" ");
63 if (j == 7)
64 LOG_PORT.printf(" ");
65 }
66 LOG_PORT.printf(" ");
67 for (size_t j = 0; j < bytesPerLine; ++j) {
68 if (i + j < size)
69 LOG_PORT.printf("%c", isprint(buf[i + j]) ? (char)buf[i + j] : '.');
70 else
71 LOG_PORT.printf(" ");
72 }
73 LOG_PORT.printf("\n");
74 }
75}
76
77#define SENSORLIB_DUMP_BUFFER(buf, size) _dump_buffer((const uint8_t *)(buf), (size_t)(size))
78
79// ─── Mbed / Zephyr ─────────────────────────────────────────────────────────
80#elif defined(ARDUINO_ARCH_MBED) || defined(ARDUINO_ARCH_ZEPHYR)
81
82#define SENSORLIB_LOG_FILE_LINE __FILE__, __LINE__
83
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__)
88
89static inline void _dump_buffer(const uint8_t *buf, size_t size)
90{
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) {
95 if (i + j < size)
96 printf("%02x ", buf[i + j]);
97 else
98 printf(" ");
99 if (j == 7)
100 printf(" ");
101 }
102 printf(" ");
103 for (size_t j = 0; j < bytesPerLine; ++j) {
104 if (i + j < size)
105 printf("%c", isprint(buf[i + j]) ? (char)buf[i + j] : '.');
106 else
107 printf(" ");
108 }
109 printf("\n");
110 }
111}
112
113#define SENSORLIB_DUMP_BUFFER(buf, size) _dump_buffer((const uint8_t *)(buf), (size_t)(size))
114
115
116
117#elif defined(ESP_PLATFORM) && defined(ARDUINO)
118
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__)
124
125
126static inline void _dump_buffer(const uint8_t *buf, size_t size)
127{
128 const size_t bytesPerLine = 16;
129 for (size_t i = 0; i < size; i += bytesPerLine) {
130 char hex[3 * 16 + 2 + 1] = {0}; // "xx "×16 + " " + NUL
131 char ascii[16 + 1] = {0};
132 size_t pos_h = 0, pos_a = 0;
133 for (size_t j = 0; j < bytesPerLine; ++j) {
134 if (i + j < size) {
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] : '.';
137 } else {
138 pos_h += snprintf(hex + pos_h, sizeof(hex) - pos_h, " ");
139 ascii[pos_a++] = ' ';
140 }
141 if (j == 7) {
142 hex[pos_h++] = ' ';
143 hex[pos_h] = '\0';
144 }
145 }
146 ascii[pos_a] = '\0';
147 (void)ascii;
148 log_d("%08x %s %s", (unsigned int)i, hex, ascii);
149 }
150}
151
152#define SENSORLIB_DUMP_BUFFER(buf, size) _dump_buffer((const uint8_t *)(buf), (size_t)(size))
153
154// ─── ESP-IDF (native, not Arduino) ─────────────────────────────────────────
155#elif defined(ESP_PLATFORM) && !defined(ARDUINO)
156
157#include "esp_log.h"
158
159#define SENSORLIB_LOG_TAG "SensorLib"
160
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__)
167#else
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__)
173#endif
174
175static inline void _dump_buffer(const uint8_t *buf, size_t size)
176{
177 const size_t bytesPerLine = 16;
178 for (size_t i = 0; i < size; i += bytesPerLine) {
179 char hex[3 * 16 + 2 + 1] = {0}; // "xx "×16 + " " + NUL
180 char ascii[16 + 1] = {0};
181 size_t pos_h = 0, pos_a = 0;
182 for (size_t j = 0; j < bytesPerLine; ++j) {
183 if (i + j < size) {
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] : '.';
186 } else {
187 pos_h += snprintf(hex + pos_h, sizeof(hex) - pos_h, " ");
188 ascii[pos_a++] = ' ';
189 }
190 if (j == 7) {
191 hex[pos_h++] = ' ';
192 hex[pos_h] = '\0';
193 }
194 }
195 ascii[pos_a] = '\0';
196 (void)ascii;
197 ESP_LOG_LEVEL_LOCAL(ESP_LOG_DEBUG, SENSORLIB_LOG_TAG,
198 "%08x %s %s", (unsigned int)i, hex, ascii);
199 }
200}
201
202#define SENSORLIB_DUMP_BUFFER(buf, size) _dump_buffer((const uint8_t *)(buf), (size_t)(size))
203
204// ─── Fallback (no-op) ──────────────────────────────────────────────────────
205#else
206
207#define SENSORLIB_LOG_E(...)
208#define SENSORLIB_LOG_I(...)
209#define SENSORLIB_LOG_D(...)
210#define SENSORLIB_LOG_W(...)
211
212#define SENSORLIB_DUMP_BUFFER(buf, size)
213
214#endif
uint8_t i
char buf[64]