add debug_memory_log_buffer.c

This commit is contained in:
Steven Dan
2026-02-02 09:44:30 +08:00
parent fcde603d4f
commit 0617a76b21

View File

@@ -0,0 +1,52 @@
//#define DEBUG_MEMORY_LOG_ENABLED 1
#if DEBUG_MEMORY_LOG_ENABLED
#include <stdlib.h>
unsigned long get_reference_time();
int itoa(unsigned n, char *buf, unsigned base, int fill);
unsigned int debug_memory_log_buffer_index = 0;
#define DEBUG_MEMORY_LOG_BUFFER_SIZE 4048
unsigned char debug_memory_log_buffer[DEBUG_MEMORY_LOG_BUFFER_SIZE];
// Override the weak symbol used for print messages
int _write(int fd, const unsigned char *data, size_t len) {
#ifndef NO_LOG_TIMESTAPS
unsigned t = get_reference_time();
char ch_time[8];
itoa(t, ch_time, 10, 0);
if ((debug_memory_log_buffer_index + 9 + 1) > DEBUG_MEMORY_LOG_BUFFER_SIZE)
debug_memory_log_buffer_index = 0;
for(unsigned int i = 0; i < 8; i++) {
debug_memory_log_buffer[debug_memory_log_buffer_index] = ch_time[i];
debug_memory_log_buffer_index++;
}
debug_memory_log_buffer[debug_memory_log_buffer_index] = ' ';
debug_memory_log_buffer_index++;
#endif
// Check for wrap of the circular buffer
if ((debug_memory_log_buffer_index + len + 1) > DEBUG_MEMORY_LOG_BUFFER_SIZE)
debug_memory_log_buffer_index = 0;
// Copy write message into log buffer
for(unsigned int i = 0; i < len; i++) {
debug_memory_log_buffer[debug_memory_log_buffer_index] = data[i];
debug_memory_log_buffer_index++;
}
// Terminate the whole buffer after the current message
debug_memory_log_buffer[debug_memory_log_buffer_index] = '\0';
#if UART_DEBUG
print_uart(data, len);
#endif
return len;
}
#endif