From dd646aebffcc2136ef772926c5df1c540f12fb85 Mon Sep 17 00:00:00 2001 From: Thomas Basler Date: Fri, 18 Apr 2025 16:42:33 +0200 Subject: [PATCH] Use ESP Logging Macros for webapi --- src/WebApi.cpp | 8 +++++--- src/WebApi_prometheus.cpp | 8 +++++--- src/WebApi_ws_live.cpp | 16 +++++++++------- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/WebApi.cpp b/src/WebApi.cpp index c124b16f..11c3e2ac 100644 --- a/src/WebApi.cpp +++ b/src/WebApi.cpp @@ -1,13 +1,15 @@ // SPDX-License-Identifier: GPL-2.0-or-later /* - * Copyright (C) 2022-2024 Thomas Basler and others + * Copyright (C) 2022-2025 Thomas Basler and others */ #include "WebApi.h" #include "Configuration.h" -#include "MessageOutput.h" #include "defaults.h" #include +#undef TAG +static const char* TAG = "webapi"; + WebApiClass::WebApiClass() : _server(HTTP_PORT) { @@ -138,7 +140,7 @@ bool WebApiClass::sendJsonResponse(AsyncWebServerRequest* request, AsyncJsonResp root["code"] = WebApiError::GenericInternalServerError; root["type"] = "danger"; response->setCode(500); - MessageOutput.printf("WebResponse failed: %s, %" PRIu16 "\n", function, line); + ESP_LOGE(TAG, "WebResponse failed: %s, %" PRIu16 "", function, line); ret_val = false; } diff --git a/src/WebApi_prometheus.cpp b/src/WebApi_prometheus.cpp index 826e77b9..51db9631 100644 --- a/src/WebApi_prometheus.cpp +++ b/src/WebApi_prometheus.cpp @@ -5,12 +5,14 @@ */ #include "WebApi_prometheus.h" #include "Configuration.h" -#include "MessageOutput.h" #include "NetworkSettings.h" #include "WebApi.h" #include "__compiled_constants.h" #include +#undef TAG +static const char* TAG = "webapi"; + void WebApiPrometheusClass::init(AsyncWebServer& server, Scheduler& scheduler) { using std::placeholders::_1; @@ -113,12 +115,12 @@ void WebApiPrometheusClass::onPrometheusMetricsGet(AsyncWebServerRequest* reques stream->addHeader("Cache-Control", "no-cache"); if (stream->available() > initialResponseBufferSize) { initialResponseBufferSize = stream->available(); - MessageOutput.printf("Increased /api/prometheus/metrics initialResponseBufferSize to %" PRIu32 " bytes\n", initialResponseBufferSize); + ESP_LOGI(TAG, "Increased /api/prometheus/metrics initialResponseBufferSize to %" PRIu32 " bytes", initialResponseBufferSize); } request->send(stream); } catch (std::bad_alloc& bad_alloc) { - MessageOutput.printf("Call to /api/prometheus/metrics temporarely out of resources. Reason: \"%s\".\n", bad_alloc.what()); + ESP_LOGE(TAG, "Call to /api/prometheus/metrics temporarely out of resources. Reason: \"%s\".", bad_alloc.what()); WebApi.sendTooManyRequests(request); } diff --git a/src/WebApi_ws_live.cpp b/src/WebApi_ws_live.cpp index 1e08258c..c69fede1 100644 --- a/src/WebApi_ws_live.cpp +++ b/src/WebApi_ws_live.cpp @@ -4,12 +4,14 @@ */ #include "WebApi_ws_live.h" #include "Datastore.h" -#include "MessageOutput.h" #include "Utils.h" #include "WebApi.h" #include "defaults.h" #include +#undef TAG +static const char* TAG = "webapi"; + #ifndef PIN_MAPPING_REQUIRED #define PIN_MAPPING_REQUIRED 0 #endif @@ -112,9 +114,9 @@ void WebApiWsLiveClass::sendDataTaskCb() _ws.textAll(buffer); } catch (const std::bad_alloc& bad_alloc) { - MessageOutput.printf("Call to /api/livedata/status temporarely out of resources. Reason: \"%s\".\n", bad_alloc.what()); + ESP_LOGE(TAG, "Call to /api/livedata/status temporarely out of resources. Reason: \"%s\".", bad_alloc.what()); } catch (const std::exception& exc) { - MessageOutput.printf("Unknown exception in /api/livedata/status. Reason: \"%s\".\n", exc.what()); + ESP_LOGE(TAG, "Unknown exception in /api/livedata/status. Reason: \"%s\".", exc.what()); } } } @@ -237,9 +239,9 @@ void WebApiWsLiveClass::addTotalField(JsonObject& root, const String& name, cons void WebApiWsLiveClass::onWebsocketEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len) { if (type == WS_EVT_CONNECT) { - MessageOutput.printf("Websocket: [%s][%" PRIu32 "] connect\n", server->url(), client->id()); + ESP_LOGD(TAG, "Websocket: [%s][%" PRIu32 "] connect", server->url(), client->id()); } else if (type == WS_EVT_DISCONNECT) { - MessageOutput.printf("Websocket: [%s][%" PRIu32 "] disconnect\n", server->url(), client->id()); + ESP_LOGD(TAG, "Websocket: [%s][%" PRIu32 "] disconnect", server->url(), client->id()); } } @@ -281,10 +283,10 @@ void WebApiWsLiveClass::onLivedataStatus(AsyncWebServerRequest* request) WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__); } catch (const std::bad_alloc& bad_alloc) { - MessageOutput.printf("Call to /api/livedata/status temporarely out of resources. Reason: \"%s\".\n", bad_alloc.what()); + ESP_LOGE(TAG, "Call to /api/livedata/status temporarely out of resources. Reason: \"%s\".", bad_alloc.what()); WebApi.sendTooManyRequests(request); } catch (const std::exception& exc) { - MessageOutput.printf("Unknown exception in /api/livedata/status. Reason: \"%s\".\n", exc.what()); + ESP_LOGE(TAG, "Unknown exception in /api/livedata/status. Reason: \"%s\".", exc.what()); WebApi.sendTooManyRequests(request); } }