Skip to content

Commit

Permalink
Allow for log callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Johboh committed Apr 1, 2024
1 parent f15a840 commit 13ac12c
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 112 deletions.
9 changes: 9 additions & 0 deletions examples/arduino/simple/WifiAndOta.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ WiFiHelper _wifi_helper(hostname);
void setup() {
Serial.begin(115200);

// Add logging callbacks when using Arduino framework. When using ESP-IDF, use set_log_level() instead. See
// constructor and addOnLog().
_ota_helper.addOnLog([](const std::string message, const esp_log_level_t log_level) {
Serial.println("OtaHelper: " + String(message.c_str())); // ignoring log_level, logs everything. Noisy.
});
_wifi_helper.addOnLog([](const std::string message, const esp_log_level_t log_level) {
Serial.println("WifiHelper: " + String(message.c_str())); // ignoring log_level, logs everything. Noisy.
});

bool initialize_nvs = true;
bool timeout_ms = 10000;
auto connected = _wifi_helper.connectToAp(wifi_ssid, wifi_password, initialize_nvs, timeout_ms);
Expand Down
4 changes: 4 additions & 0 deletions examples/espidf/simple/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ void app_main(void) {
gpio_set_level(PIN_LED, 1);
xTaskCreate(blinkAndSerialTask, "blinkAndSerialTask", 2048, NULL, 15, NULL);

// Set log levels.
esp_log_level_set(OtaHelperLog::TAG, ESP_LOG_INFO);
esp_log_level_set(WiFiHelperLog::TAG, ESP_LOG_INFO);

// Connect to WIFI with 10s timeout.
bool initialize_nvs = true;
bool timeout_ms = 10000;
Expand Down
22 changes: 20 additions & 2 deletions src/OtaHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <esp_event.h>
#include <esp_http_client.h>
#include <esp_http_server.h>
#include <esp_log.h>
#include <esp_netif.h>
#include <esp_partition.h>
#include <freertos/FreeRTOS.h>
Expand Down Expand Up @@ -122,7 +123,8 @@ class OtaHelper {
/**
* @brief Construct a new Ota Helper.
*
* To set log level for this object, use: esp_log_level_set(OtaHelperLog::TAG, ESP_LOG_*);
* If not using log callback (see addOnLog()), set log level for this object using:
* esp_log_level_set(OtaHelperLog::TAG, ESP_LOG_*);
*
* @param configuration configuration for the OTA services and rollback strategy.
* @param crt_bundle_attach CRT Bundle Attach for Ardunio or ESP-IDF from MDTLS, to support TLS/HTTPS. See definition
Expand All @@ -141,7 +143,6 @@ class OtaHelper {
* @return true if successfully started.
*/
bool start();
void setup() { start(); }

/**
* @brief If the rollback strategy is MANUAL, call this to confirm that the new firmware is OK. Otherwise the previous
Expand All @@ -166,6 +167,21 @@ class OtaHelper {
*/
bool updateFrom(std::string &url, FlashMode flash_mode, std::string md5_hash = "");

/**
* @brief Callback when this object want to log something.
*
* @param message the log message to log.
* @param log_level the severity of the log.
*/
using OnLog = std::function<void(const std::string message, const esp_log_level_t log_level)>;

/**
* @brief Register log callback. Normally you can use esp_log_level_set(OtaHelperLog::TAG,
* ESP_LOG_*); to set the log level for this object, but if that is not possible or you want more control over
* logging, you can add a log callback. When set, the log level set using esp_log_level_set() is ignored.
*/
void addOnLog(OnLog on_log) { _on_log.push_back(on_log); }

private: // OTA (generic)
bool
writeStreamToPartition(const esp_partition_t *partition, FlashMode flash_mode, size_t content_length,
Expand Down Expand Up @@ -227,8 +243,10 @@ class OtaHelper {
bool reportOnError(esp_err_t err, const char *msg);
void replaceAll(std::string &s, const std::string &search, const std::string &replace);
std::string trim(const std::string &str);
void log(const esp_log_level_t log_level, std::string message);

private:
std::vector<OnLog> _on_log;
Configuration _configuration;
CrtBundleAttach _crt_bundle_attach;
uint8_t _rollback_bits_to_wait_for;
Expand Down
21 changes: 20 additions & 1 deletion src/WiFiHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define __WIFI_HELPER_H__

#include <esp_event.h>
#include <esp_log.h>
#include <esp_netif.h>
#include <freertos/FreeRTOS.h>
#include <freertos/event_groups.h>
Expand All @@ -22,7 +23,8 @@ class WiFiHelper {
/**
* @brief Construct a new WiFi Helper.
*
* To set log level for this object, use: esp_log_level_set(WiFiHelperLog::TAG, ESP_LOG_*);
* If not using log callback (see addOnLog()), set log level for this object using:
* esp_log_level_set(WiFiHelperLog::TAG, ESP_LOG_*);
*
* @param device_hostname the name of this device. Will be used as hostname. From https://www.ietf.org/rfc/rfc1123.txt
* "Each element of the hostname must be from 1 to 63 characters long
Expand Down Expand Up @@ -68,12 +70,28 @@ class WiFiHelper {
*/
bool isConnected() { return _is_connected; }

/**
* @brief Callback when this object want to log something.
*
* @param message the log message to log.
* @param log_level the severity of the log.
*/
using OnLog = std::function<void(const std::string message, const esp_log_level_t log_level)>;

/**
* @brief Register log callback. Normally you can use esp_log_level_set(WiFiHelperLog::TAG,
* ESP_LOG_*); to set the log level for this object, but if that is not possible or you want more control over
* logging, you can add a log callback. When set, the log level set using esp_log_level_set() is ignored.
*/
void addOnLog(OnLog on_log) { _on_log.push_back(on_log); }

private:
bool initializeNVS();
bool reportOnError(esp_err_t err, const char *msg);

private:
static void eventHandler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
void log(const esp_log_level_t log_level, std::string message);

private:
const char *_device_hostname;
Expand All @@ -83,6 +101,7 @@ class WiFiHelper {
bool _is_connected;
esp_ip4_addr_t _ip_addr;
esp_netif_t *_netif_sta;
std::vector<OnLog> _on_log;
EventGroupHandle_t _wifi_event_group;
std::function<void(void)> _on_connected;
std::function<void(void)> _on_disconnected;
Expand Down
32 changes: 32 additions & 0 deletions src/impl/LogHelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#ifndef __LOG_HELPER_H__
#define __LOG_HELPER_H__

#include <cstdint>
#include <esp_log.h>

namespace LogHelper {
static inline void log(const char *tag, const esp_log_level_t log_level, std::string &message) {
va_list args;
switch (log_level) {
case ESP_LOG_ERROR:
ESP_LOGE(tag, "%s", message.c_str());
break;
case ESP_LOG_WARN:
ESP_LOGW(tag, "%s", message.c_str());
break;
case ESP_LOG_INFO:
ESP_LOGI(tag, "%s", message.c_str());
break;
case ESP_LOG_VERBOSE:
ESP_LOGV(tag, "%s", message.c_str());
break;
case ESP_LOG_DEBUG:
ESP_LOGD(tag, "%s", message.c_str());
break;
}
}
} // namespace LogHelper

#endif // __LOG_HELPER_H__
Loading

0 comments on commit 13ac12c

Please sign in to comment.