From c5bce25afe93e1ad41e48a43f148e6492941d4e8 Mon Sep 17 00:00:00 2001 From: Ollo Date: Sun, 14 Nov 2021 19:49:40 +0100 Subject: [PATCH] Added time into logs --- esp32/src/MQTTUtils.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/esp32/src/MQTTUtils.cpp b/esp32/src/MQTTUtils.cpp index 8dd5827..7cdabfb 100644 --- a/esp32/src/MQTTUtils.cpp +++ b/esp32/src/MQTTUtils.cpp @@ -9,9 +9,19 @@ void log(int level, String message, int statusCode) { String buffer; StaticJsonDocument<200> doc; + // Read the current time + time_t now; // this is the epoch + tm tm; // the structure tm holds time information in a more convient way doc["level"] = level; doc["message"] = message; doc["statusCode"] = statusCode; + time(&now); + localtime_r(&now, &tm); + if (tm.tm_year > (2021 - 1970)) { /* Only add the time, if we have at least 2021 */ + doc["time"] = String(String(1900 + tm.tm_year) + "-" + String(tm.tm_mon + 1) + "-" + String(tm.tm_mday) + + " " + String(tm.tm_hour) + ":" + String(tm.tm_min) + ":" + String(tm.tm_sec)); + } + serializeJson(doc, buffer); if (mAliveWasRead) { @@ -84,4 +94,4 @@ void startMQTTRoundtripTest(){ Homie.getMqttClient() .subscribe(backupTopic, 2); } -} \ No newline at end of file +}