log: add log query API endpoints and display logs on web interface
All checks were successful
Build ESP32 BMC Firmware / build (push) Successful in 53s
All checks were successful
Build ESP32 BMC Firmware / build (push) Successful in 53s
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include "uart_handler.h"
|
||||
#include "ethernet_manager.h"
|
||||
#include "ota_handler.h"
|
||||
#include "log_handler.h"
|
||||
#include "esp_log.h"
|
||||
#include "cJSON.h"
|
||||
#include <string.h>
|
||||
@@ -490,6 +491,42 @@ static esp_err_t api_system_status_handler(httpd_req_t *req) {
|
||||
return send_json_success(req, status);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Log API Handlers
|
||||
// ============================================================================
|
||||
|
||||
static esp_err_t api_logs_get_handler(httpd_req_t *req) {
|
||||
// Get log buffer size
|
||||
size_t log_size = log_get_size();
|
||||
if (log_size == 0) {
|
||||
httpd_resp_set_type(req, "text/plain");
|
||||
httpd_resp_sendstr(req, "No logs available");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
// Allocate buffer for logs
|
||||
char *log_buf = malloc(log_size + 1);
|
||||
if (!log_buf) {
|
||||
return send_json_error(req, "Failed to allocate memory for logs");
|
||||
}
|
||||
|
||||
// Get logs
|
||||
size_t copied = log_get_buffer(log_buf, log_size);
|
||||
log_buf[copied] = '\0';
|
||||
|
||||
// Send response
|
||||
httpd_resp_set_type(req, "text/plain");
|
||||
httpd_resp_sendstr(req, log_buf);
|
||||
|
||||
free(log_buf);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t api_logs_clear_handler(httpd_req_t *req) {
|
||||
log_clear_buffer();
|
||||
return send_json_success(req, cJSON_CreateObject());
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// OTA API Handlers
|
||||
// ============================================================================
|
||||
@@ -797,6 +834,18 @@ static const httpd_uri_t uri_api_ota_upload = {
|
||||
.handler = api_ota_upload_handler,
|
||||
};
|
||||
|
||||
static const httpd_uri_t uri_api_logs_get = {
|
||||
.uri = "/api/logs",
|
||||
.method = HTTP_GET,
|
||||
.handler = api_logs_get_handler,
|
||||
};
|
||||
|
||||
static const httpd_uri_t uri_api_logs_clear = {
|
||||
.uri = "/api/logs/clear",
|
||||
.method = HTTP_POST,
|
||||
.handler = api_logs_clear_handler,
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// Public Functions
|
||||
// ============================================================================
|
||||
@@ -846,6 +895,8 @@ esp_err_t web_server_start(void) {
|
||||
httpd_register_uri_handler(server, &uri_api_ota_status);
|
||||
httpd_register_uri_handler(server, &uri_api_ota_update);
|
||||
httpd_register_uri_handler(server, &uri_api_ota_upload);
|
||||
httpd_register_uri_handler(server, &uri_api_logs_get);
|
||||
httpd_register_uri_handler(server, &uri_api_logs_clear);
|
||||
|
||||
// Register UART callback for WebSocket broadcast at startup
|
||||
esp_err_t cb_ret = uart_register_rx_callback(uart_to_ws_callback);
|
||||
|
||||
Reference in New Issue
Block a user