Skip to content

Commit

Permalink
Automatic log clearing + other (#4)
Browse files Browse the repository at this point in the history
* Removed ArduinoJson dependancy

* Added automatic log clearing functionality

* Added customizable paths

* Bugfixing in path in example
  • Loading branch information
jibrilsharafi committed May 11, 2024
1 parent 95bd086 commit 9994444
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 109 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,15 @@ Saving should be quite straightforward: just copy any message that is sent to th
Last but not least, the use should be as simple as possible: you just need to create `AdvancedLogger advancedLogger;` and simply use `advancedLogger.log("Setting up ADE7953...", "main::setup", INFO);`

## Dependencies
This project depends on the following libraries:
- [ArduinoJson](https://github.com/bblanchon/ArduinoJson), version 7.0.0 or later.
This project has no external dependencies, and uses only the standard libraries.

## What's next?
- [ ] **Customizable paths**: allow to set a custom path when creating the AdvancedLogger object.
- [ ] **Automatic log clearing**: if the free memory is less than a certain threshold, the oldest logs should be deleted, keeping the memory usage under control.
- [x] **Customizable paths**: allow to set a custom path when creating the AdvancedLogger object.
- [x] **Automatic log clearing**: if the free memory is less than a certain threshold, the oldest logs should be deleted, keeping the memory usage under control.
- [ ] **Log to SD card**: the ability to log to an external SD card would be a great addition, as it would allow to store a much larger amount of logs.
- [x] **Dump to serial**: implement a function that dumps the entire log to the serial, so that it can be accessed in real time.
- [ ] **Remove ArduinoJson dependency**: the library is used only for the configuration file, and as such it could be removed by implementing a simpler configuration in .txt format.
- [x] **Remove ArduinoJson dependency**: the library is used only for the configuration file, and as such it could be removed by implementing a simpler configuration in .txt format.
- [ ] **Upgrade to LittleFS**: the SPIFFS library is deprecated, and as such it should be replaced with the LittleFS library.
- [ ] **Test other microcontrollers**: the library is currently tested only on the ESP32S3, but it should be tested on other microcontrollers to ensure compatibility.
- [ ] **Test other microcontrollers**: the library is currently tested only on the ESP32, but it should be tested on other microcontrollers to ensure compatibility.
- [ ] **MQTT integration**: the ability to send logs to an MQTT server would be a great addition, as it would allow to monitor the device remotely.
- [ ] ~~**Consistent spacing**: the spacing between the different parts of the log should be consistent, to make it easier to read.~~ Not needed, as the format is already quite clear.
- [x] ~~**Consistent spacing**: the spacing between the different parts of the log should be consistent, to make it easier to read.~~ Not needed, as the format is already quite clear.
29 changes: 23 additions & 6 deletions examples/basicServer/basicServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This file provides a simple example to show how to use the AdvancedLogger library.
*
* Author: Jibril Sharafi, @jibrilsharafi
* Date: 11/05/2024
* Date: 07/04/2024
* GitHub repository: https://github.com/jibrilsharafi/AdvancedLogger
*
* This library is licensed under the MIT License. See the LICENSE file for more information.
Expand Down Expand Up @@ -34,7 +34,10 @@

#include "advancedLogger.h"

AdvancedLogger logger;
String customLogPath = "/customPath/log.txt";
String customConfigPath = "/customPath/config.txt";

AdvancedLogger logger(customLogPath.c_str(), customConfigPath.c_str()); // Leave empty for default paths

AsyncWebServer server(80);

Expand All @@ -47,6 +50,8 @@ const long intervalLogDump = 10000;
long lastMillisLogClear = 0;
const long intervalLogClear = 30000;

int maxLogLines = 10; // Low value for testing purposes

// **** CHANGE THESE TO YOUR SSID AND PASSWORD ****
const char *ssid = "YOUR_SSID";
const char *password = "YOUR_PASSWORD";
Expand All @@ -70,6 +75,9 @@ void setup()
// levels are used (DEBUG for print and INFO for save).
logger.setPrintLevel(ADVANCEDLOGGER_DEBUG);
logger.setSaveLevel(ADVANCEDLOGGER_INFO);
// Set the maximum number of log lines before the log is cleared
// If you don't set this, the default is used
logger.setMaxLogLines(maxLogLines);
logger.log("AdvancedLogger setup done!", "basicServer::setup", ADVANCEDLOGGER_INFO);

// Connect to WiFi
Expand All @@ -86,8 +94,8 @@ void setup()
// --------------------
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
{ request->send(200, "text/html", "<button onclick=\"window.location.href='/log'\">Explore the logs</button><br><br><button onclick=\"window.location.href='/config'\">Explore the configuration</button>"); });
server.serveStatic("/log", SPIFFS, "/AdvancedLogger/log.txt");
server.serveStatic("/config", SPIFFS, "/AdvancedLogger/config.json");
server.serveStatic("/log", SPIFFS, customLogPath.c_str());
server.serveStatic("/config", SPIFFS, customConfigPath.c_str());
server.onNotFound([](AsyncWebServerRequest *request)
{ request->send(404, "text/plain", "Not found"); });
server.begin();
Expand All @@ -101,12 +109,17 @@ void setup()
void loop()
{
logger.log("This is an debug message!", "basicServer::loop", ADVANCEDLOGGER_DEBUG);
delay(500);
logger.log("This is an info message!!", "basicServer::loop", ADVANCEDLOGGER_INFO);
delay(500);
logger.log("This is an warning message!!!", "basicServer::loop", ADVANCEDLOGGER_WARNING);
delay(500);
logger.log("This is an error message!!!!", "basicServer::loop", ADVANCEDLOGGER_ERROR);
delay(500);
logger.log("This is an fatal message!!!!!", "basicServer::loop", ADVANCEDLOGGER_FATAL);
delay(1000);
delay(500);
logger.logOnly("This is an info message (logOnly)!!", "basicServer::loop", ADVANCEDLOGGER_INFO);
delay(1000);

printLevel = logger.getPrintLevel();
saveLevel = logger.getSaveLevel();
Expand All @@ -120,7 +133,11 @@ void loop()

if (millis() - lastMillisLogClear > intervalLogClear)
{
logger.dumpToSerial();
logger.log(
("Current number of log lines: " + String(logger.getLogLines())).c_str(),
"basicServer::loop",
ADVANCEDLOGGER_INFO
);
logger.clearLog();
logger.setDefaultLogLevels();
logger.log("Log cleared!", "basicServer::loop", ADVANCEDLOGGER_WARNING);
Expand Down
34 changes: 26 additions & 8 deletions examples/basicUsage/basicUsage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@

#include "advancedLogger.h"

AdvancedLogger logger;
String customLogPath = "/customPath/log.txt";
String customConfigPath = "/customPath/config.txt";

AdvancedLogger logger(customLogPath.c_str(), customConfigPath.c_str()); // Leave empty for default paths

String printLevel;
String saveLevel;
Expand All @@ -38,6 +41,8 @@ const long intervalLogDump = 10000;
long lastMillisLogClear = 0;
const long intervalLogClear = 30000;

int maxLogLines = 10; // Low value for testing purposes

void setup()
{
// Initialize Serial and SPIFFS (mandatory for the AdvancedLogger library)
Expand All @@ -57,6 +62,10 @@ void setup()
// levels are used (DEBUG for print and INFO for save).
logger.setPrintLevel(ADVANCEDLOGGER_DEBUG);
logger.setSaveLevel(ADVANCEDLOGGER_INFO);
// Set the maximum number of log lines before the log is cleared
// If you don't set this, the default is used
logger.setMaxLogLines(maxLogLines);
logger.log("AdvancedLogger setup done!", "basicUsage::setup", ADVANCEDLOGGER_INFO);

lastMillisLogDump = millis();
lastMillisLogClear = millis();
Expand All @@ -65,13 +74,18 @@ void setup()

void loop()
{
logger.log("This is an debug message!", "basicUsage::loop", ADVANCEDLOGGER_DEBUG);
logger.log("This is an info message!!", "basicUsage::loop", ADVANCEDLOGGER_INFO);
logger.log("This is an warning message!!!", "basicUsage::loop", ADVANCEDLOGGER_WARNING);
logger.log("This is an error message!!!!", "basicUsage::loop", ADVANCEDLOGGER_ERROR);
logger.log("This is an fatal message!!!!!", "basicUsage::loop", ADVANCEDLOGGER_FATAL);
logger.log("This is an debug message!", "basicServer::loop", ADVANCEDLOGGER_DEBUG);
delay(500);
logger.log("This is an info message!!", "basicServer::loop", ADVANCEDLOGGER_INFO);
delay(500);
logger.log("This is an warning message!!!", "basicServer::loop", ADVANCEDLOGGER_WARNING);
delay(500);
logger.log("This is an error message!!!!", "basicServer::loop", ADVANCEDLOGGER_ERROR);
delay(500);
logger.log("This is an fatal message!!!!!", "basicServer::loop", ADVANCEDLOGGER_FATAL);
delay(500);
logger.logOnly("This is an info message (logOnly)!!", "basicServer::loop", ADVANCEDLOGGER_INFO);
delay(1000);
logger.logOnly("This is an info message (logOnly)!!", "basicUsage::loop", ADVANCEDLOGGER_INFO);

printLevel = logger.getPrintLevel();
saveLevel = logger.getSaveLevel();
Expand All @@ -85,7 +99,11 @@ void loop()

if (millis() - lastMillisLogClear > intervalLogClear)
{
logger.dumpToSerial();
logger.log(
("Current number of log lines: " + String(logger.getLogLines())).c_str(),
"basicServer::loop",
ADVANCEDLOGGER_INFO
);
logger.clearLog();
logger.setDefaultLogLevels();
logger.log("Log cleared!", "basicServer::loop", ADVANCEDLOGGER_WARNING);
Expand Down
46 changes: 19 additions & 27 deletions library.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
{
"name": "AdvancedLogger",
"keywords": "logger, log, logging, memory, format",
"description": "Library for simple logging to memory with comprehensive format.",
"repository":
"name": "AdvancedLogger",
"keywords": "logger, log, logging, memory, format",
"description": "Library for simple logging to memory with comprehensive format.",
"repository": {
"type": "git",
"url": "https://github.com/jibrilsharafi/AdvancedLogger.git"
},
"authors": [
{
"type": "git",
"url": "https://github.com/jibrilsharafi/AdvancedLogger.git"
},
"authors":
[
{
"name": "Jibril Sharafi",
"email": "[email protected]",
"url": "https://github.com/jibrilsharafi",
"maintainer": true
}
],
"dependencies":
{
"name": "ArduinoJson",
"authors": "bblanchon",
"frameworks": "arduino"
},
"version": "1.1.0",
"frameworks": "arduino",
"platforms": "*"
}

"name": "Jibril Sharafi",
"email": "[email protected]",
"url": "https://github.com/jibrilsharafi",
"maintainer": true
}
],
"dependencies": {},
"version": "1.1.0",
"frameworks": "arduino",
"platforms": "*"
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ paragraph=Easy to use, logs to memory using SPIFFS and the format contains all t
category=Communication
url=https://github.com/jibrilsharafi/AdvancedLogger
architectures=*
depends=ArduinoJson
depends=
Loading

0 comments on commit 9994444

Please sign in to comment.