hideakitai / DebugLog

Logging library for Arduino that can output to both Serial and File with one line
MIT License
51 stars 12 forks source link

[QUESTION/FEATURE REQUEST] Cutsom log format - add timestamp #8

Closed bobemoe closed 1 year ago

bobemoe commented 1 year ago

Is it possible to add a custom log format? Specifically I'm wanting to prefix each log with a timestamp, either millis() or YYMMDDHHMMSS if RTC is available.

Instead of implementing the time handling internally, probably best bet is a custom format callback or something?

Does anything like this exists?

If not would you be interested in PR? If so any suggestion on best place to implement?

ErezBinyamin commented 1 year ago

I think I have a solution to this. I have made my changes on my fork of the project, perhaps they can be merged?

How to use it

Before you #include <DebugLog.h> simply define a LOG_PREAMBLE macro.

#define LOG_PREAMBLE __LINE__, __func__, "This string will go before all log statements: "
#include <DebugLog.h>

What I changed

// Before the log macros looked like this
#define LOG_ERROR(...) DebugLog::Manager::get().log(arx::debug::LogLevel::LVL_ERROR, LOG_SHORT_FILENAME, __LINE__, __func__, __VA_ARGS__)

// Now it looks like this
#define LOG_ERROR(...) DebugLog::Manager::get().log(arx::debug::LogLevel::LVL_ERROR, LOG_SHORT_FILENAME, LOG_PREAMBLE, __VA_ARGS__)
ErezBinyamin commented 1 year ago

I realize this addresses the "Custom log format" part of your question but not the "add timestamp" part. For adding a timestamp you will have to define your own timestamp function and make a call to that when defining LOG_PREAMBLE

Like so:

#define LOG_PREAMBLE __LINE__, __func__, get_time()
#include <DebugLog.h>
hideakitai commented 1 year ago

Fixed by https://github.com/hideakitai/DebugLog/pull/9