Skip to content

Commit

Permalink
#1 get logging working - on ESP32
Browse files Browse the repository at this point in the history
  • Loading branch information
davetcc committed Sep 14, 2024
1 parent ebe6616 commit 7539627
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 40 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@

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
40 changes: 17 additions & 23 deletions examples/ioLogging/ioLogging.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
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
7 changes: 0 additions & 7 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 @@ -71,12 +70,6 @@ const char* niceErrorCode(tm_internal::TmErrorCode code) {
}
}

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
14 changes: 9 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 @@ -48,10 +46,15 @@ enum SerLoggingLevel {

#ifdef IO_LOGGING_DEBUG

#if defined(__MBED__) && !defined(BUILD_FOR_PICO_CMAKE) && !defined(ARDUINO_ARCH_MBED)
#define LOGGING_USES_MBED
#endif


/** 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 +128,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 +184,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

0 comments on commit 7539627

Please sign in to comment.