10 char buffer[TWR_LOG_BUFFER_SIZE];
16 static twr_log_t _twr_log = { .initialized =
false };
18 void application_error(twr_error_t code);
20 static void _twr_log_message(
twr_log_level_t level,
char id,
const char *format, va_list ap);
24 if (_twr_log.initialized)
29 memset(&_twr_log, 0,
sizeof(_twr_log));
31 _twr_log.level = level;
32 _twr_log.timestamp = timestamp;
37 _twr_log.initialized =
true;
40 void twr_log_dump(
const void *buffer,
size_t length,
const char *format, ...)
53 size_t offset_base = 0;
55 for (; offset_base <
sizeof(_twr_log.buffer); offset_base++)
57 if (_twr_log.buffer[offset_base] ==
'>')
69 if (buffer != NULL && length != 0)
71 for (position = 0; position < length; position += TWR_LOG_DUMP_WIDTH)
73 offset = offset_base + snprintf(_twr_log.buffer + offset_base,
sizeof(_twr_log.buffer) - offset_base,
"%3d: ", position);
75 char *ptr_hex = _twr_log.buffer + offset;
77 offset += (TWR_LOG_DUMP_WIDTH * 3 + 2 + 1);
79 char *ptr_text = _twr_log.buffer + offset;
81 offset += TWR_LOG_DUMP_WIDTH;
87 if ((position + TWR_LOG_DUMP_WIDTH) <= length)
89 line_size = TWR_LOG_DUMP_WIDTH;
93 line_size = length - position;
96 for (i = 0; i < line_size; i++)
98 uint8_t value = ((uint8_t *) buffer)[position + i];
100 if (i == (TWR_LOG_DUMP_WIDTH / 2))
106 snprintf(ptr_hex, 4,
"%02X ", value);
110 if (value < 32 || value > 126)
120 for (; i < TWR_LOG_DUMP_WIDTH; i++)
122 if (i == (TWR_LOG_DUMP_WIDTH / 2))
128 strcpy(ptr_hex,
" ");
135 _twr_log.buffer[offset++] =
'\r';
136 _twr_log.buffer[offset++] =
'\n';
147 va_start(ap, format);
156 va_start(ap, format);
165 va_start(ap, format);
174 va_start(ap, format);
179 static void _twr_log_message(
twr_log_level_t level,
char id,
const char *format, va_list ap)
181 if (!_twr_log.initialized)
183 application_error(TWR_ERROR_LOG_NOT_INITIALIZED);
186 if (_twr_log.level > level)
197 uint32_t timestamp_abs = tick_now / 10;
199 offset = sprintf(_twr_log.buffer,
"# %lu.%02lu <%c> ", timestamp_abs / 100, timestamp_abs % 100,
id);
205 uint32_t timestamp_rel = (tick_now - _twr_log.tick_last) / 10;
207 offset = sprintf(_twr_log.buffer,
"# +%lu.%02lu <%c> ", timestamp_rel / 100, timestamp_rel % 100,
id);
209 _twr_log.tick_last = tick_now;
213 strcpy(_twr_log.buffer,
"# <!> ");
215 _twr_log.buffer[3] = id;
220 offset += vsnprintf(&_twr_log.buffer[offset],
sizeof(_twr_log.buffer) - offset - 1, format, ap);
222 if (offset >=
sizeof(_twr_log.buffer))
224 offset =
sizeof(_twr_log.buffer) - 3 - 3;
225 _twr_log.buffer[offset++] =
'.';
226 _twr_log.buffer[offset++] =
'.';
227 _twr_log.buffer[offset++] =
'.';
230 _twr_log.buffer[offset++] =
'\r';
231 _twr_log.buffer[offset++] =
'\n';
void void void void void twr_log_error(const char *format,...) __attribute__((format(printf
Log ERROR message (annotated in log as <E>)
twr_log_level_t
Log level.
void void void void twr_log_warning(const char *format,...) __attribute__((format(printf
Log WARNING message (annotated in log as <W>)
void void twr_log_debug(const char *format,...) __attribute__((format(printf
Log DEBUG message (annotated in log as <D>)
twr_log_timestamp_t
Log timestamp.
void twr_log_init(twr_log_level_t level, twr_log_timestamp_t timestamp)
Initialize logging facility.
void twr_log_dump(const void *buffer, size_t length, const char *format,...) __attribute__((format(printf
Log DUMP message (annotated in log as <X>)
void void void twr_log_info(const char *format,...) __attribute__((format(printf
Log INFO message (annotated in log as )
@ TWR_LOG_LEVEL_DUMP
Logging DUMP.
@ TWR_LOG_LEVEL_WARNING
Log level WARNING.
@ TWR_LOG_LEVEL_ERROR
Log level ERROR.
@ TWR_LOG_LEVEL_INFO
Log level INFO.
@ TWR_LOG_LEVEL_DEBUG
Log level DEBUG.
@ TWR_LOG_TIMESTAMP_REL
Timestamp logging enabled (relative time format)
@ TWR_LOG_TIMESTAMP_ABS
Timestamp logging enabled (absolute time format)
twr_tick_t twr_tick_get(void)
Get absolute timestamp since start of program.
uint64_t twr_tick_t
Timestamp data type.
void twr_uart_init(twr_uart_channel_t channel, twr_uart_baudrate_t baudrate, twr_uart_setting_t setting)
Initialize UART channel.
size_t twr_uart_write(twr_uart_channel_t channel, const void *buffer, size_t length)
Write data to UART channel (blocking call)
@ TWR_UART_BAUDRATE_115200
UART baudrat 115200 bps.
@ TWR_UART_SETTING_8N1
8N1: 8 data bits, none parity bit, 1 stop bit