Usage

#include <uuid/log.h>

Create a uuid::log::Logger and call its functions for logging messages.

Create a class that implements uuid::log::Handler and registers to handle log messages. Output those messages by whatever means is appropriate for your application (e.g. serial console, over the WiFi network, by email).

There is no option to exclude compilation of debug-level messages, the expectation is that these will be disabled at runtime. If your program cannot fit in flash with the debug messages enabled or run with sufficient performance then there’s no benefit in having them.

The performance impact of disabled debug logging can be limited by using the Logger::enabled(LogLevel) function (but messages will not be formatted if the level isn’t enabled).

Example

#include <Arduino.h>
#include <uuid/common.h>
#include <uuid/log.h>

class SerialLogHandler: public uuid::log::Handler {
public:
	SerialLogHandler() = default;

	void start() {
		uuid::log::Logger::register_handler(this, uuid::log::Level::ALL);
	}

	/*
	 * It is not recommended to directly output from this function,
	 * this is only a simple example. Messages should normally be
	 * queued for later output when the application is less busy.
	 */
	void operator<<(std::shared_ptr<uuid::log::Message> message) {
		char temp[100] = { 0 };

		int ret = snprintf_P(temp, sizeof(temp), PSTR("%s %c [%S] %s\r\n"),
			uuid::log::format_timestamp_ms(message->uptime_ms).c_str(),
			uuid::log::format_level_char(message->level),
			message->name, message->text.c_str());

		if (ret > 0) {
			Serial.print(temp);
		}
	}
};

static SerialLogHandler log_handler;

void setup() {
	static uuid::log::Logger logger{F("setup")};

	Serial.begin(115200);

	log_handler.start();

	logger.info(F("Application started"));
}

void loop() {
	static uuid::log::Logger logger{F("loop")};
	static unsigned int i = 0;

	uuid::loop();

	logger.debug(F("Hello %u World!"), i++);

	delay(1000);
}

Output

0+00:00:00.000 I [setup] Application started
0+00:00:00.000 D [loop] Hello 0 World!
0+00:00:01.000 D [loop] Hello 1 World!
0+00:00:02.000 D [loop] Hello 2 World!
0+00:00:03.000 D [loop] Hello 3 World!
0+00:00:04.000 D [loop] Hello 4 World!
0+00:00:05.000 D [loop] Hello 5 World!
0+00:00:06.000 D [loop] Hello 6 World!
0+00:00:07.000 D [loop] Hello 7 World!
0+00:00:08.000 D [loop] Hello 8 World!
0+00:00:09.000 D [loop] Hello 9 World!
0+00:00:10.000 D [loop] Hello 10 World!