can_wizard/main/xvprintf.c

45 lines
1.3 KiB
C
Raw Normal View History

#include "xvprintf.h"
#include <stdarg.h>
#include <stdio.h>
#include "stdbool.h"
#include "esp_log.h"
RingbufHandle_t can_messages;
bool timestamp_enabled = true;
void init_tx_ringbuf() {
2023-11-24 04:19:41 +00:00
can_messages = xRingbufferCreate(2200, RINGBUF_TYPE_NOSPLIT);
}
// This function will be called by the ESP log library every time ESP_LOG needs to be performed.
// @important Do NOT use the ESP_LOG* macro's in this function ELSE recursive loop and stack overflow! So use printf() instead for debug messages.
2023-11-24 04:16:58 +00:00
int vxprintf(const char *fmt, va_list args) {
2023-11-24 04:18:05 +00:00
char msg_to_send[300];
size_t str_len;
2023-11-24 04:18:05 +00:00
str_len = vsnprintf(msg_to_send, 299, fmt, args);
2023-11-24 04:19:01 +00:00
xRingbufferSend(can_messages, msg_to_send, str_len + 1, pdMS_TO_TICKS(200));
return str_len;
}
int xprintf(const char *fmt, ...) {
va_list(args);
va_start(args, fmt);
2023-11-24 04:16:58 +00:00
return vxprintf(fmt, args);
}
int print_w_clr_time(char *msg, char *color, bool use_printf) {
print_func pr_func;
if (use_printf) pr_func = printf;
else pr_func = xprintf;
char timestamp[20];
timestamp[0] = '\0';
if (timestamp_enabled) {
snprintf(timestamp, 19, "[%s] ", esp_log_system_timestamp());
}
if (color != NULL) {
return(pr_func("\033[0;%sm%s%s\033[0m\n", color, timestamp, msg));
} else {
return(pr_func("%s%s\n", timestamp, msg));
}
}