___ ___ ___ ___
/\__\ ___ /\__\ /\ \ /\ \
/:/ / /\ \ /:/ / /::\ \ /::\ \
/:/__/ \:\ \ /:/ / /:/\:\ \ /:/\:\ \
/::\ \ ___ /::\__\ /:/ / /:/ \:\ \ /:/ \:\ \
/:/\:\ /\__\ __/:/\/__/ /:/__/ /:/__/ \:\__\ /:/__/_\:\__\
\/__\:\/:/ / /\/:/ / \:\ \ \:\ \ /:/ / \:\ /\ \/__/
\::/ / \::/__/ \:\ \ \:\ /:/ / \:\ \:\__\
/:/ / \:\__\ \:\ \ \:\/:/ / \:\/:/ /
/:/ / \/__/ \:\__\ \::/ / \::/ /
\/__/ \/__/ \/__/ \/__/
Lightweight logging for Arduino/ESP/PlatformIO projects with:
- Levels: ERROR, WARN, INFO, DEBUG, VERBOSE
- Verbose details: optional file:line and function
- ANSI colors on console (toggleable)
- File logging to SD with independent level threshold
- Timestamp provider hook
#include <Arduino.h>
#include <SPI.h>
#include <SD.h>
#include <HiLOG.h>
// Optional timestamp provider, can be hooked to RTC call or NTP.
static String TimestampProvider() { return String(millis()); }
static bool MountSD()
{
SPI.begin(SD_CLK, SD_MISO, SD_MOSI, SD_CS);
return SD.begin(SD_CS);
}
void setup()
{
Serial.begin(115200);
while (!Serial) { }
HiLOG::Initialize(&Serial);
HiLOG::SetContext("[APP] ");
HiLOG::SetVerboseDetails(true);
HiLOG::SetAnsiColorEnabled(true);
HiLOG::SetTimestampProvider(TimestampProvider);
HiLOG::SetGlobalLevel(HILOG_DEBUG); // applies to both console and file
HiLOG::SetConsoleLevel(HILOG_VERBOSE);
if (MountSD())
{
HiLOG::SetFileLoggingEnabled(true);
if (HiLOG::SetLogFile("/system_log.txt"))
{
HiLOG::SetFileLevel(HILOG_INFO);
}
else
{
HiLOG::SetFileLoggingEnabled(false);
HILOGE("Failed to open /system_log.txt");
}
}
else
{
HiLOG::SetFileLoggingEnabled(false);
HILOGE("SD mount failed; file logging disabled");
}
HILOGE("This is an error message");
HILOGW("This is a warning message");
HILOGI("This is an info message");
HILOGD("This is a debug message");
HILOGV("This is a verbose message");
}
void loop()
{
static unsigned long last_tick_ms = 0;
if (millis() - last_tick_ms > 1000)
{
last_tick_ms = millis();
HILOGI(String("Tick: ") + String(last_tick_ms));
}
}-
Initialize: Set the output stream for console logs
void HiLOG::Initialize(Print* console_output = &Serial);
-
Levels
void HiLOG::SetConsoleLevel(HILOG_LEVEL level);void HiLOG::SetFileLevel(HILOG_LEVEL level);void HiLOG::SetGlobalLevel(HILOG_LEVEL level);
-
Formatting & context
void HiLOG::SetVerboseDetails(bool enabled);void HiLOG::SetContext(const String& context_prefix);void HiLOG::SetTimestampProvider(String (*provider)());void HiLOG::SetAnsiColorEnabled(bool enabled);
-
File logging
void HiLOG::SetFileLoggingEnabled(bool enabled);bool HiLOG::SetLogFile(const String& file_path);
-
Emit
HILOGE(msg), HILOGW(msg), HILOGI(msg), HILOGD(msg), HILOGV(msg)
- File logging requires SD to be mounted before
SetLogFile. See the exampleMountSD() - ANSI colors are applied to console output only, not file logs.
- When
SetVerboseDetails(true)is enabled, the macros capture file, line, and function automatically.