Skip to content

Commit

Permalink
#1 get logging working - tested on ESP32 so far (#2)
Browse files Browse the repository at this point in the history
Get logging working Arduino, incorporate testing
  • Loading branch information
davetcc authored Sep 14, 2024
1 parent ebe6616 commit b6b3fcd
Show file tree
Hide file tree
Showing 13 changed files with 277 additions and 71 deletions.
62 changes: 62 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: Test
on: [push]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: |
~/.cache/pip
~/.platformio/.cache
key: ${{ runner.os }}-pio
- name: Install dependencies
run: sudo apt-get update && sudo apt-get install -y libsdl2-2.0-0
- uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Install PlatformIO Core
run: pip install --upgrade platformio
- name: Set up QEMU
id: setup-qemu
run: |
if [[ "$(uname -m)" == "x86_64" ]]; then
QEMU_URL="/~https://github.com/espressif/qemu/releases/download/esp-develop-8.2.0-20240122/qemu-xtensa-softmmu-esp_develop_8.2.0_20240122-x86_64-linux-gnu.tar.xz"
elif [[ "$(uname -m)" == "aarch64" ]]; then
QEMU_URL="/~https://github.com/espressif/qemu/releases/download/esp-develop-8.2.0-20240122/qemu-xtensa-softmmu-esp_develop_8.2.0_20240122-aarch64-linux-gnu.tar.xz"
else
echo "Unsupported architecture: $(uname -m)"
exit 1
fi
wget $QEMU_URL -O qemu.tar.xz
mkdir -p qemu
tar -xf qemu.tar.xz -C qemu --strip-components=1
sudo mv qemu /usr/local/qemu
- name: Add QEMU to PATH
run: echo "/usr/local/qemu/bin" >> $GITHUB_PATH

- name: Run unit tests
run: pio test --without-uploading --project-conf=platformio-test.ini

static-analysis:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: |
~/.cache/pip
~/.platformio/.cache
key: ${{ runner.os }}-pio
- uses: actions/setup-python@v5
with:
python-version: '3.12'

- name: Install PlatformIO Core
run: pip install --upgrade platformio

- name: Run static analysis
run: pio check
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
# TcMenuLog
[![PlatformIO](/~https://github.com/TcMenu/TcMenuLog/actions/workflows/platformio.yml/badge.svg)](/~https://github.com/TcMenu/TcMenuLog/actions/workflows/platformio.yml)
[![Test](/~https://github.com/TcMenu/TcMenuLog/actions/workflows/test.yml/badge.svg)](/~https://github.com/TcMenu/IoAbstraction/actions/workflows/test.yml)
[![License: Apache 2.0](https://img.shields.io/badge/license-Apache--2.0-green.svg)](/~https://github.com/TcMenu/TcMenuLog/blob/main/LICENSE)
[![GitHub release](https://img.shields.io/github/release/TcMenu/TcMenuLog.svg?maxAge=3600)](/~https://github.com/TcMenu/TcMenuLog/releases)
[![davetcc](https://img.shields.io/badge/davetcc-dev-blue.svg)](/~https://github.com/davetcc)
[![JSC TechMinds](https://img.shields.io/badge/JSC-TechMinds-green.svg)](https://www.jsctm.cz)

TcMenu organisation made this library available for you to use. It takes significant effort to keep all our libraries current and working on a wide range of boards. Please consider making at least a one off donation via the sponsor button if you find it useful. In forks, please keep text to here intact.

This library provides several useful extensions that make programming Arduino / mbed for non-trivial apps simpler. You can find examples packaged with it in the `examples` folder.
## Summary

This library provides logging facilities that are used by all our other libraries. You can consider this like a logging API with a simple implementation that backs onto the serial port by default.

The library works on most Arduino devices, PicoSDK and mbed. You can see our library compatibility matrix: https://tcmenu.github.io/documentation/

You can find examples packaged with it in the `examples` folder. The example should work on most platforms without needing changes.

## Supporting another output format

The easiest way would be to create another implementation of `LoggingPort` that met the `Print` (or `PrintCompat`) interface, this will then integrate without changing anything. You can see examples of this in `IoLogging.h` for mbed and PicoSDK.

## License

Expand Down
52 changes: 23 additions & 29 deletions examples/ioLogging/ioLogging.ino
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* Advanced feature of library.
*
* This example shows how to use the very simple logging that's built into IoAbstraction.
* To enable logging open IoLogging.h in the IoAbstraction directory and uncomment
* #define IO_LOGGING_DEBUG
* This example shows how to use the TcMenu logging framework, to log simple information
* to the serial port. It should work on Arduino, mbed and PicoSDK with little to no
* adjustment.
*
* As this example logs output, either define flag `IO_LOGGING_DEBUG` or open IoLogging.h
* and uncomment `#define IO_LOGGING_DEBUG` to enable.
*
* This logging is only complied in when the above define is set, if it is not set then
* the logging is completely removed.
*
*/

/* The logging levels are below:
Expand All @@ -33,8 +33,6 @@
At runtime a level can be turned on/off using: void serEnableLevel(SerLoggingLevel level, bool active)
*/

#include <TaskManagerIO.h>
#include <IoAbstraction.h>
#include <IoLogging.h>

char sz[] = {"hello world"};
Expand All @@ -49,38 +47,34 @@ void setup() {

Serial.println("Starting ioLogging example");

// we can use this to start the logging delegate that logs task manager notifications at IOA_DEBUG level
startTaskManagerLogDelegate();

// enable an extra level
serEnableLevel(SER_IOA_DEBUG, true);

// write a string entry that is applied with F(..) so in progmem on AVR
// with an integer second value.
serdebugF2("In setup function - A0=", analogRead(A0));

taskManager.scheduleFixedRate(10, [] {
// write values to log in HEX - first parameter is wrapped in F(..) using the F variant
serlogFHex2(SER_DEBUG, "Two Values in hex: ", 0xFADE, 0xFACE);
serlogFHex(SER_DEBUG, "One Values in hex: ", 0xFADE);
serlogF4(SER_ERROR, "This is an error", 100, 200, 300);
serlogF3(SER_WARNING, "This is an warning", 100, 200);
// write values to log in HEX - first parameter is wrapped in F(..) using the F variant
serlogFHex2(SER_DEBUG, "Two Values in hex: ", 0xFADE, 0xFACE);
serlogFHex(SER_DEBUG, "One Values in hex: ", 0xFADE);
serlogF4(SER_ERROR, "This is an error", 100, 200, 300);
serlogF3(SER_WARNING, "This is an warning", 100, 200);

// log at SER_DEBUG, for legacy support
serdebugF2("Int value: ", 109298384L);
serdebugF2("Bool value: ", true);

// log at SER_DEBUG, for legacy support
serdebugF2("Int value: ", 109298384L);
serdebugF2("Bool value: ", true);
// here we hex dump an array
serlogHexDump(SER_DEBUG, "Hex dump", sz, sizeof sz);

// here we hex dump an array
serlogHexDump(SER_DEBUG, "Hex dump", sz, sizeof sz);
// the F variant always tries to use F(..) to save ram on the first parameter on AVR
serdebugF("String in flash");

// the F variant always tries to use F(..) to save ram on the first parameter on AVR
serdebugF("String in flash");

// this version does not use F(..) so we can pass RAM strings even on AVR
serdebug(sz);
}, TIME_SECONDS);
// this version does not use F(..) so we can pass RAM strings even on AVR
serdebug(sz);
}

void loop() {
taskManager.runLoop();
delay(1000);
serlogF2(SER_USER_1, "Millis: ", millis());
}
66 changes: 66 additions & 0 deletions examples/textUtilities/textUtilities.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* This example shows how to use the text utilities that are packaged with TcMenu logging
* framework. It should work on Arduino, mbed and PicoSDK with little to no adjustment.
*
* As this example logs output, either define flag `IO_LOGGING_DEBUG` or open IoLogging.h
* and uncomment `#define IO_LOGGING_DEBUG` to enable.
*
* This logging is only complied in when the above define is set, if it is not set then
* the logging is completely removed.
*
*/

#include <IoLogging.h>
#include <TextUtilities.h>

char sz[32] = {0};

void setup() {
// This example logs using IoLogging, see the following guide to enable
// https://www.thecoderscorner.com/products/arduino-libraries/io-abstraction/arduino-logging-with-io-logging/
IOLOG_START_SERIAL

// convert 102934 to 8 decimal places not padded.
// the clrBuff version also clears the sz buffer first
// IE does not concatenate.
ltoaClrBuff(sz, 102934, 8, NOT_PADDED, sizeof sz);
serlogF2(SER_DEBUG, "Str is: ", sz);

// concatenate 00102934 (to 8 decimal places zero padded).
// this variant concatenates to a zero terminated string
fastltoa(sz, 102934, 8, '0', sizeof sz);
serlogF2(SER_DEBUG, "Str is: ", sz);

// clear the string
sz[0] = 0;

// convert a float into sz string to 4 places.
fastftoa(sz, 1024.512F, 4, sizeof sz);
serlogF2(SER_DEBUG, "Float: ", sz);

// find a power of ten for decimal place calculation
serlogF2(SER_DEBUG, "Dp To Div: ", dpToDivisor(5));

// find the significant places needed to represent a value
serlogF2(SER_DEBUG, "Dp To Div: ", valueToSignificantPlaces(50123, false));

// get the single hex character of a digit between 0..15 and vice versa
serlogF2(SER_DEBUG, "Hexchar: ", hexChar(12));
serlogF2(SER_DEBUG, "HexVal: ", hexValueOf('D'));

// convert a number into a hex string
sz[0]=0;

// convert a int into hex string
intToHexString(sz, sizeof sz, 0xFACE, 4, true);
serlogF2(SER_DEBUG, "Hex str: ", sz);
}

void loop() {
delay(1000);
// convert the time into seconds and then print via logging to 3dp.
float now = millis() / 1000.0F;
sz[0]=0;
fastftoa(sz, now, 3, sizeof sz);
serlogF2(SER_DEBUG, "Time = ", sz);
}
4 changes: 2 additions & 2 deletions library.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
},
"authors": [
{
"name": "DaveTcc",
"url": "https://www.thecoderscorner.com",
"name": "TcMenu",
"url": "https://tcmenu.github.io/documentation/",
"maintainer": true
}
],
Expand Down
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

name=TcMenuLog
version=1.0.0
maintainer=https://www.thecoderscorner.com
author=davetcc
maintainer=https://tcmenu.github.io/documentation/
author=TcMenu
category=Other
url=/~https://github.com/TcMenu/TcMenuLog
sentence=Logging framework and helper text utils for the TcMenu framework.
Expand Down
14 changes: 14 additions & 0 deletions platformio-test.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[env:esp32dev]
platform = espressif32
framework = arduino
board = esp32dev
extra_scripts = post:merge-bin.py
test_build_src = true

test_testing_command =
qemu-system-xtensa
-nographic
-machine
esp32
-drive
file=${platformio.build_dir}/${this.__env__}/firmware-merged.bin,if=mtd,format=raw
4 changes: 0 additions & 4 deletions platformio.ini
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
[env]
framework = arduino

lib_deps =
davetcc/TaskManagerIO@^1.4.3
davetcc/IoAbstraction@^4.0.2

[env:megaatmega2560]
platform = atmelavr
board = megaatmega2560
Expand Down
25 changes: 0 additions & 25 deletions src/IoLogging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

#include "IoLogging.h"
#include "TaskPlatformDeps.h"

#ifdef IO_LOGGING_DEBUG

Expand Down Expand Up @@ -53,30 +52,6 @@ const char* prettyLevel(SerLoggingLevel level) {
}
}

const char* niceErrorCode(tm_internal::TmErrorCode code) {
switch(code) {
case tm_internal::TM_INFO_REALLOC:
return "mem";
case tm_internal::TM_INFO_TASK_ALLOC:
return "alloc";
case tm_internal::TM_INFO_TASK_FREE:
return "free";
case tm_internal::TM_WARN_HIGH_SPINCOUNT:
return "spin";
case tm_internal::TM_ERROR_FULL:
return "full";
default:
case tm_internal::TM_ERROR_LOCK_FAILURE:
return "err";
}
}

void startTaskManagerLogDelegate() {
tm_internal::setLoggingDelegate([](tm_internal::TmErrorCode errorCode, int task) {
serlogF3(SER_IOA_DEBUG, "TMLog ", niceErrorCode(errorCode), task);
});
}

#ifdef BUILD_FOR_PICO_CMAKE
PrintfLogger LoggingPort;
#endif
Expand Down
16 changes: 11 additions & 5 deletions src/IoLogging.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
* by un-commenting the define. Should NOT be used in production.
*/

#include "PlatformDetermination.h"

// START user adjustable section.

// the below definition controls logging, enable logging by either defining this build flag
Expand Down Expand Up @@ -46,12 +44,19 @@ enum SerLoggingLevel {
SER_LOG_EVERYTHING = 0xffff
};

#if defined(__MBED__) && !defined(BUILD_FOR_PICO_CMAKE) && !defined(ARDUINO_ARCH_MBED)
#define LOGGING_USES_MBED
#elif !defined(BUILD_FOR_PICO_CMAKE)
#include <Arduino.h>
#endif

#ifdef IO_LOGGING_DEBUG


/** This uint16_t stores the enabled logging levels, don't use directly */
extern unsigned int enabledLevels;

#ifdef IOA_USE_MBED
#ifdef LOGGING_USES_MBED

#include "PrintCompat.h"
#include <FileHandle.h>
Expand Down Expand Up @@ -125,14 +130,14 @@ unsigned long millis(); // available from task manager
#else
// Arduino:
// You can change the logging serial port by defining LoggingPort to your chosen serial port.
#include <Arduino.h>
#ifndef LoggingPort
#define LoggingPort Serial
#endif
#define IOLOG_START_SERIAL LoggingPort.begin(115200);
#define IOLOG_MBED_PORT_IF_NEEDED(tx, rx)
#endif


const char* prettyLevel(SerLoggingLevel level);
#define logTimeAndLevel(title, lvl) LoggingPort.print(millis());LoggingPort.print('-');LoggingPort.print(prettyLevel(lvl));LoggingPort.print(':');LoggingPort.print(title)

Expand Down Expand Up @@ -181,7 +186,8 @@ inline void serdebugHexDump(const char *title, const void* data, size_t len) { s
#define serdebug3(x1, x2, x3) serlog3(SER_DEBUG, x1, x2, x3)
#define serdebugHex(x1, x2) serlogHex(SER_DEBUG, x1, x2)

void startTaskManagerLogDelegate();
// no longer does anything but don't want code compilation errors.
#define startTaskManagerLogDelegate()

#else
// all loging to no operations (commenting out the above define of IO_LOGGING_DEBUG to remove in production builds).
Expand Down
Loading

0 comments on commit b6b3fcd

Please sign in to comment.