I just wanted to share something super quick that I found makes my serial output so much better to understand and read.
This formats the output like this.
Theres LOG INFO DEBUG WARN and ERROR
it prints the uptime, the log type (in color) the file and line number (clickable) the function name, and the log with color
#ifndef CONSOLE_LOGGER_H
#define CONSOLE_LOGGER_H
//USAGE.
//CONSOLE_INFO("Hello, world!");
//CONSOLE_WARN("Warning: This is a warning");
//CONSOLE_ERROR("Error: This is an error");
//CONSOLE_LOG("Log: This is a log");
//MUST SET monitor_raw = yes in platformio.ini
#include <Arduino.h>
#include <stdio.h>
#include <stdarg.h>
// ANSI color codes - Updated for PlatformIO serial monitor
#define COLOR_RESET "\e[0m"
#define COLOR_LOG "\e[1;37m" // Bright White
#define COLOR_INFO "\e[1;36m" // Bright Cyan
#define COLOR_WARN "\e[1;33m" // Bright Yellow
#define COLOR_ERROR "\e[1;91m" // Bright Red
#define COLOR_DEBUG "\e[1;32m" // Bright Green
// [INFO] [filename:line] [function] Your message
#define CONSOLE_LOG(format, ...) do { \
printf("[%-8lu] [" COLOR_LOG "%-5s" COLOR_RESET "] [%-45s] [%-20s] " COLOR_LOG format COLOR_RESET "\n", \
(unsigned long) millis(), "LOG", __FILE__ ":" STRINGIFY(__LINE__), __FUNCTION__, ##__VA_ARGS__); \
} while(0)
#define CONSOLE_INFO(format, ...) do { \
printf("[%-8lu] [" COLOR_INFO "%-5s" COLOR_RESET "] [%-45s] [%-20s] " COLOR_INFO format COLOR_RESET "\n", \
(unsigned long) millis(), "INFO", __FILE__ ":" STRINGIFY(__LINE__), __FUNCTION__, ##__VA_ARGS__); \
} while(0)
#define CONSOLE_DEBUG(format, ...) do { \
printf("[%-8lu] [" COLOR_DEBUG "%-5s" COLOR_RESET "] [%-45s] [%-20s] " COLOR_DEBUG format COLOR_RESET "\n", \
(unsigned long) millis(), "DEBUG", __FILE__ ":" STRINGIFY(__LINE__), __FUNCTION__, ##__VA_ARGS__); \
} while(0)
#define CONSOLE_WARN(format, ...) do { \
printf("[%-8lu] [" COLOR_WARN "%-5s" COLOR_RESET "] [%-45s] [%-20s] " COLOR_WARN format COLOR_RESET "\n", \
(unsigned long) millis(), "WARN", __FILE__ ":" STRINGIFY(__LINE__), __FUNCTION__, ##__VA_ARGS__); \
} while(0)
#define CONSOLE_ERROR(format, ...) do { \
printf("[%-8lu] [" COLOR_ERROR "%-5s" COLOR_RESET "] [%-45s] [%-20s] " COLOR_ERROR format COLOR_RESET "\n", \
(unsigned long) millis(), "ERROR", __FILE__ ":" STRINGIFY(__LINE__), __FUNCTION__, ##__VA_ARGS__); \
} while(0)
// Helper macro to stringify line numbers
#define STRINGIFY(x) STRINGIFY_(x)
#define STRINGIFY_(x) #x
#endif // CONSOLE_LOGGER_H
Hope someone finds it helpful. Just include this in the top of each file you’ll use it