Skip to content

Commit

Permalink
Examples improved #2
Browse files Browse the repository at this point in the history
Examples improved (plus fix previous squash and merge commits).
  • Loading branch information
jibrilsharafi authored Apr 7, 2024
2 parents 0503389 + c5357f1 commit 4628578
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 28 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@
*.app

# Visual Studio Code
.vscode/
.vscode/

# Platformio
platformio.ini
.pio/
src/test.cpp
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ A **simple** logging library capable of **saving logs to memory** and with a **c
Usage:
```cpp
AdvancedLogger logger;

...

logger.begin();
...
logger.log("This is an info message!", "main::loop", ADVANCEDLOGGER_INFO);
delay(1000);
logger.log("This is an error message!!", "main::loop", ADVANCEDLOGGER_ERROR);
Expand All @@ -17,7 +17,7 @@ Output (both in the Serial and in the log file in memory):
[2024-03-23 09:44:10] [1450 ms] [INFO] [Core 1] [main::loop] This is an info message!
[2024-03-23 09:44:11] [1550 ms] [ERROR] [Core 1] [main::loop] This is an error message!!
```
For a detailed example, see the [basicExample](examples/basicUsage/basicUsage.cpp) in the examples folder.
For a detailed example, see the [basicUsage](examples/basicUsage/basicUsage.cpp) and [basicServer](examples/basicServer/basicServer.cpp) in the examples folder.

## Why?
There is no way of sugar-coat it: *every well-developed project will eventually need a proper logging* in place to make sure that not only everything is fully monitored, but also that everything monitored is later accessible for debugging.
Expand Down Expand Up @@ -64,9 +64,11 @@ This project depends on the following libraries:
- [ArduinoJson](https://github.com/bblanchon/ArduinoJson), version 7.0.0 or later.

## 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.
- [ ] **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.
- [ ] **Dump to serial**: implement a functino 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.
- [ ] **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 ESP32S3, 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.
112 changes: 112 additions & 0 deletions examples/basicServer/basicServer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* File: basicServer.cpp
* --------------------
* This file provides a simple example to show how to use the AdvancedLogger library.
*
* Author: Jibril Sharafi, @jibrilsharafi
* 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.
*
* This example is a simple web server that serves a webpage with a button
* that sends the user to the /log page, where the logs are displayed, and
* another button that sends the user to the /config page, where the configuration
* is displayed.
*
* Remember to change the ssid and password to your own.
*
* Tested only on the ESP32. For other boards, you may need to change the
* WebServer library.
*
* Possible logging levels:
* - ADVANCEDLOGGER_VERBOSE
* - ADVANCEDLOGGER_DEBUG
* - ADVANCEDLOGGER_INFO
* - ADVANCEDLOGGER_WARNING
* - ADVANCEDLOGGER_ERROR
* - ADVANCEDLOGGER_FATAL
*/
#include <Arduino.h>
#include <SPIFFS.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>

#include "advancedLogger.h"

AdvancedLogger logger;

AsyncWebServer server(80);

String printLevel;
String saveLevel;

// **** CHANGE THESE TO YOUR SSID AND PASSWORD ****
const char *ssid = "YOUR_SSID";
const char *password = "YOUR_PASSWORD";

void setup()
{
// Initialize Serial and SPIFFS (mandatory for the AdvancedLogger library)
// --------------------
Serial.begin(115200);

if (!SPIFFS.begin(true)) // Setting to true will format the SPIFFS if mounting fails
{
Serial.println("An Error has occurred while mounting SPIFFS");
}

logger.begin();

// Setting the print and save levels is not mandatory.
// If you don't set them, the default levels are first taken
// from the SPIFFS file, and if it doesn't exist, the default
// levels are used (DEBUG for print and INFO for save).
logger.setPrintLevel(ADVANCEDLOGGER_DEBUG);
logger.setSaveLevel(ADVANCEDLOGGER_INFO);
logger.log("AdvancedLogger setup done!", "basicServer::setup", ADVANCEDLOGGER_INFO);

// Connect to WiFi
// --------------------
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
logger.log("Connecting to WiFi..", "basicServer::setup", ADVANCEDLOGGER_INFO);
}
logger.log(("IP address: " + WiFi.localIP().toString()).c_str(), "basicServer::setup", ADVANCEDLOGGER_INFO);

// Serve a simple webpage with a button that sends the user to the page /log and /config
// --------------------
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.onNotFound([](AsyncWebServerRequest *request)
{ request->send(404, "text/plain", "Not found"); });
server.begin();
logger.log("Server started!", "basicServer::setup", ADVANCEDLOGGER_INFO);

logger.log("Setup done!", "basicServer::setup", ADVANCEDLOGGER_INFO);
}

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

printLevel = logger.getPrintLevel();
saveLevel = logger.getSaveLevel();

if (millis() > 60000)
{
logger.clearLog();
logger.setDefaultLogLevels();
logger.log("Log cleared!", "basicServer::loop", ADVANCEDLOGGER_WARNING);
}
}
48 changes: 30 additions & 18 deletions examples/basicUsage/basicUsage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* File: basicUsage.cpp
* --------------------
* This file provides a simple example to show how to use the AdvancedLogger library.
*
*
* Author: Jibril Sharafi, @jibrilsharafi
* Date: 21/03/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 All @@ -20,6 +20,7 @@
* - ADVANCEDLOGGER_INFO
* - ADVANCEDLOGGER_WARNING
* - ADVANCEDLOGGER_ERROR
* - ADVANCEDLOGGER_FATAL
*/
#include <Arduino.h>
#include <SPIFFS.h>
Expand All @@ -31,35 +32,46 @@ AdvancedLogger logger;
String printLevel;
String saveLevel;

void setup() {
Serial.begin(9600);
void setup()
{
// Initialize Serial and SPIFFS (mandatory for the AdvancedLogger library)
// --------------------
Serial.begin(115200);

if (!SPIFFS.begin()) {
if (!SPIFFS.begin(true)) // Setting to true will format the SPIFFS if mounting fails
{
Serial.println("An Error has occurred while mounting SPIFFS");
}

logger.begin();

logger.setPrintLevel(ADVANCEDLOGGER_VERBOSE);

// Setting the print and save levels is not mandatory.
// If you don't set them, the default levels are first taken
// from the SPIFFS file, and if it doesn't exist, the default
// levels are used (DEBUG for print and INFO for save).
logger.setPrintLevel(ADVANCEDLOGGER_DEBUG);
logger.setSaveLevel(ADVANCEDLOGGER_INFO);

logger.log("Setup done!", "simpleExample::setup", ADVANCEDLOGGER_INFO);
logger.log("Setup done!", "basicUsage::setup", ADVANCEDLOGGER_INFO);
}

void loop() {
logger.log("This is an debug message!", "simpleExample::loop", ADVANCEDLOGGER_VERBOSE);
logger.log("This is an info message!!", "simpleExample::loop", ADVANCEDLOGGER_INFO);
logger.log("This is an warning message!!!", "simpleExample::loop", ADVANCEDLOGGER_WARNING);
logger.log("This is an error message!!!!", "simpleExample::loop", ADVANCEDLOGGER_ERROR);
logger.log("This is an fatal message!!!!!", "simpleExample::loop", ADVANCEDLOGGER_FATAL);
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);
delay(1000);
logger.logOnly("This is an info message (logOnly)!!", "simpleExample::loop", ADVANCEDLOGGER_INFO);
logger.logOnly("This is an info message (logOnly)!!", "basicUsage::loop", ADVANCEDLOGGER_INFO);

printLevel = logger.getPrintLevel();
saveLevel = logger.getSaveLevel();

if (millis() > 60000) {
logger.clearLog();
logger.setDefaultLogLevels();
if (millis() > 60000)
{
logger.clearLog();
logger.setDefaultLogLevels();
logger.log("Log cleared!", "basicUsage::loop", ADVANCEDLOGGER_WARNING);
}
}
4 changes: 2 additions & 2 deletions library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "AdvancedLogger",
"keywords": "onewire, 1-wire, bus, sensor, temperature",
"keywords": "logger, log, logging, memory, format",
"description": "Library for simple logging to memory with comprehensive format.",
"repository":
{
Expand All @@ -22,7 +22,7 @@
"authors": "bblanchon",
"frameworks": "arduino"
},
"version": "1.0.0",
"version": "1.1.0",
"frameworks": "arduino",
"platforms": "*"
}
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=AdvancedLogger
version=1.0.0
version=1.1.0
author=Jibril Sharafi <[email protected]>
maintainer=Jibril Sharafi <[email protected]>
sentence=Library for simple logging to memory with comprehensive format.
Expand Down
2 changes: 0 additions & 2 deletions src/advancedLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ bool AdvancedLogger::setLogLevelsFromSpiffs()
}

log("JSON deserialized from SPIFFS correctly", "utils::deserializeJsonFromSpiffs", ADVANCEDLOGGER_DEBUG);
serializeJson(_jsonDocument, Serial);
Serial.println();

if (_jsonDocument.isNull())
{
Expand Down

0 comments on commit 4628578

Please sign in to comment.