Use ESP Logging Macros for webapi

This commit is contained in:
Thomas Basler
2025-04-18 16:42:33 +02:00
parent 6bbea37526
commit dd646aebff
3 changed files with 19 additions and 13 deletions

View File

@@ -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 <AsyncJson.h>
#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;
}

View File

@@ -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 <Hoymiles.h>
#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);
}

View File

@@ -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 <AsyncJson.h>
#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);
}
}