Intro: Logger for Arduino Over Serial Port
I am bit tired of using Serial as logger - mainly for two reasons: it does not support sprintf syntax and string are being held in RAM. For this reason I've implemented new library: https://github.com/maciejmiklas/ArdLog
ArdLog serves as simple logger for Arduino that creates formatted messages over Serial:
- Each message has timestamp.
- Each message within single loop has the same timestamp, so that you can logically connect activities together.
- Messages can be formatted using sprintf syntax.
- Text for the messages is being held in PROGMEM.
Step 1: Installation
In order to install ArdLog you have to download desired release and unpack in into folder containing Arduino libraries. The is the result on MacOS:
$ ls ArdLog.cpp ArdLog.h LICENSE README.md
Step 2: Configuration
- Logger is disabled by default, in order to enable it set LOG to true.
- Messages are created over default Serial port.
- You can choose alternative port by setting: USE_SERIAL_1, USE_SERIAL_2 or USE_SERIAL_3 to true.
- In order to print current time for each message set USE_CURRENT_TIME to true. By default logger will sample time only once at the beginning of each loop.
Step 3: Getting Up and Running
- Choose suitable configuration in ArdLog.h. In most cases you have to only set LOG to true.
- Call log_setup() in setup() method - this will initialize serial port.
- Call log_cycle() at the beginning of each loop() - it will sample current time.
- Put log messages into #if LOG log(F("....") #endif - once logger is disabled, it will not waste RAM and CUP.
This is output created by example above:
>>[000-00:00:00,000]-> Logger initialized, free RAM: 1537
>>[000-00:00:00,003]-> Free RAM: 1527
>>[000-00:00:00,003]-> **** Loop 1 ****
>>[000-00:00:00,003]-> T1 = 9
>>[000-00:00:00,003]-> T2 = 112
>>[000-00:00:01,113]-> **** Loop 2 ****
>>[000-00:00:01,113]-> T1 = 1114
>>[000-00:00:01,113]-> T2 = 1215
>>[000-00:00:02,215]-> **** Loop 3 ****
>>[000-00:00:02,215]-> T1 = 2217
>>[000-00:00:02,215]-> T2 = 2318
>>[000-00:00:03,319]-> **** Loop 4 ****
>>[000-00:00:03,319]-> T1 = 3320
>>[000-00:00:03,319]-> T2 = 3421
>>[000-00:00:04,422]-> **** Loop 5 ****
>>[000-00:00:04,422]-> T1 = 4423
>>[000-00:00:04,422]-> T2 = 4525