One of our clients asked us to help them create a flexible logging library that would work on an Arduino. They were interested in the ability to control formatting and route the output to different endpoints, such as an SD card and over Bluetooth. We documented the process of creating this library, showing how it evolved in response to feedback from our client. You can find code and documentation on GitHub.
Table of Contents:
Background
We surveyed some existing libraries to see whether anything existing met our client’s specific requirements. There are a few interesting libraries that we considered, but many fell short in different ways:
- Using log levels that were different than the client’s preference
- Requiring Arduino-style print formatting, rather than a standard formatting approach
- Inability to be used in a unit testing environment
- Inability to route output to multiple sources
- Lack of customization potential
Since nothing perfectly fit the bill, we decided to adapt an internal Embedded Artistry logging library for use with the Arduino. We’ve used our logging library on many client projects, and we designed it for flexibilitybecause the usage varies from project to project. Now Arduino developers can benefit from this library as well.
The logging library is built on top of our arduino-printf library While some printf calls are included within the logging library to support an echo-to-console feature, the library is primarily included for other functions which can be leveraged to build a flexible logger.
Capabilities
This primary goal of the library is to allow the same logging interface to be used with multiple implementations. This is achieved through the Strategy Pattern. Users are able to define their own logging strategies specific to their system. A variety of example logging strategies are also provided:
- Circular Buffer Logger
- AVR-compatible Circular Buffer Logger
- SD File Logger (single file)
- SD File Logger (with log file rotation)
These strategies can be used directly, or they can be used as a starting point for implementing your own custom strategy.
The library supports additional features:
- Echo to console
- Compile-time removal of log statements (when macros are used)
- Run-time filtering of log level
- Customizable log message formatting
- Global enable/disable of logging
- Helper class which enforces a single global logger
- Macros that work with the global logger (and enable compile-time removal of log statements)
- Ability to declare multiple loggers and use them independently
Development Log
We documented the process of developing this library. We hope it serves as a useful example of how customer requirements are turned into source code and how the design evolves in response to customer feedback.
- Building a printf Library for Arduino (a precursor to the logging library)
- Building a Flexible Logging Library for Arduino, Part 1
- Building a Flexible Logging Library for Arduino, Part 2
- Building a Flexible Logging Library for Arduino, Part 3
- Building a Flexible Logging Library for Arduino, Part 4
- Building a Flexible Logging Library for Arduino, Part 5
Finding the Library
The arduino-logger library is released under the MIT license on GitHub. You can find it in the Arduino library manager, or you can clone the repository directly via:
git clone https://github.com/embeddedartistry/arduino-logger.git
Documentation and example sketches showing use of the library are provided within the repository.
