From 904ca0752141e616676441c76edfa0a9b4728b82 Mon Sep 17 00:00:00 2001
From: Zoe Pfister <zoe.pfister@uibk.ac.at>
Date: Tue, 24 Jan 2023 14:29:00 +0100
Subject: [PATCH] WIP: move logging to sd function to separate file

---
 .../lib/Utilities/Definitions.h               |  4 +-
 .../lib/Utilities/SDCardLogger.cpp            | 46 ++++++++++++++++
 .../lib/Utilities/SDCardLogger.h              | 16 ++++++
 host/host_central_mast/src/main.cpp           | 53 ++-----------------
 4 files changed, 69 insertions(+), 50 deletions(-)
 create mode 100644 host/host_central_mast/lib/Utilities/SDCardLogger.cpp
 create mode 100644 host/host_central_mast/lib/Utilities/SDCardLogger.h

diff --git a/host/host_central_mast/lib/Utilities/Definitions.h b/host/host_central_mast/lib/Utilities/Definitions.h
index ed28c03..a5e7f2d 100644
--- a/host/host_central_mast/lib/Utilities/Definitions.h
+++ b/host/host_central_mast/lib/Utilities/Definitions.h
@@ -6,6 +6,7 @@
 #define HOST_CENTRAL_MAST_DEFINITIONS_H
 
 #include "Arduino.h"
+#include "ESP32Time.h"
 
 #define uS_TO_S_FACTOR 1000000ULL // Conversion factor for micro seconds to seconds
 #define TIME_TO_SLEEP 5           // Time ESP32 will go to sleep (in seconds)
@@ -22,7 +23,6 @@
 #define SD_CS 13
 #define LED_PIN 12
 
-const String INFLUXDB_TOKEN =
-    "dUh2gbVLv7e3egqocxriDsJQNUacA9qZ5YXsYtdnVAglnHgy4nx-jDVO7nGlSF34BosfnuwnUDaviC7dQeC5RQ==";
+extern ESP32Time rtc;
 
 #endif // HOST_CENTRAL_MAST_DEFINITIONS_H
diff --git a/host/host_central_mast/lib/Utilities/SDCardLogger.cpp b/host/host_central_mast/lib/Utilities/SDCardLogger.cpp
new file mode 100644
index 0000000..3decfc2
--- /dev/null
+++ b/host/host_central_mast/lib/Utilities/SDCardLogger.cpp
@@ -0,0 +1,46 @@
+//
+// Created by zoe on 1/24/23.
+//
+
+#include "SDCardLogger.h"
+
+namespace SDCardLogger {
+char log_print_buffer[512];
+bool printToSerial = false;
+void printDebugToSerial(bool printToSerialAsWell) {
+    printToSerial = printToSerialAsWell;
+}
+int vprintf_into_sd(const char *szFormat, va_list args) {
+    String logstring = "[" + rtc.getDateTime() + "] ";
+    logstring += szFormat;
+    // write evaluated format string into buffer
+    int ret = vsnprintf(log_print_buffer, sizeof(log_print_buffer), logstring.c_str(), args);
+
+    String date = rtc.getDate();
+    String filename = "/log_" + date + ".txt";
+
+    // output is now in buffer. write to file.
+    if (ret >= 0) {
+        if (!SD.exists(filename)) {
+            File writeLog = SD.open(filename, FILE_WRITE);
+            if (!writeLog)
+                Serial.println("Couldn't open " + filename + " for writing");
+            delay(50);
+            writeLog.close();
+        }
+
+        File logFile = SD.open(filename, FILE_APPEND);
+
+        // debug output
+        if (printToSerial) {
+            vprintf(logstring.c_str(), args);
+        }
+        logFile.write((uint8_t *)log_print_buffer, (size_t)ret);
+        // to be safe in case of crashes: flush the output
+        logFile.flush();
+        logFile.close();
+    }
+    return ret;
+}
+
+} // namespace SDCardLogger
diff --git a/host/host_central_mast/lib/Utilities/SDCardLogger.h b/host/host_central_mast/lib/Utilities/SDCardLogger.h
new file mode 100644
index 0000000..bf6615c
--- /dev/null
+++ b/host/host_central_mast/lib/Utilities/SDCardLogger.h
@@ -0,0 +1,16 @@
+//
+// Created by zoe on 1/24/23.
+//
+
+#ifndef HOST_CENTRAL_MAST_SDCARDLOGGER_H
+#define HOST_CENTRAL_MAST_SDCARDLOGGER_H
+
+#include "Definitions.h"
+#include "SD.h"
+
+namespace SDCardLogger {
+void printDebugToSerial(bool printToSerialAsWell);
+int vprintf_into_sd(const char *szFormat, va_list args);
+}; // namespace SDCardLogger
+
+#endif // HOST_CENTRAL_MAST_SDCARDLOGGER_H
diff --git a/host/host_central_mast/src/main.cpp b/host/host_central_mast/src/main.cpp
index cb62f77..5394036 100644
--- a/host/host_central_mast/src/main.cpp
+++ b/host/host_central_mast/src/main.cpp
@@ -3,10 +3,12 @@
 #include "ConnectionManager.h"
 #include "FS.h"
 #include "SD.h"
+#include "SDCardLogger.h"
 #include "SPI.h"
 #include "time.h"
 #include <Arduino.h>
 #include <ArduinoJson.h>
+#include <Definitions.h>
 #include <ESP32Time.h>
 #include <WiFi.h>
 #include <esp_log.h>
@@ -38,20 +40,6 @@ const char apn[] = "m2m.public.at"; // SET TO YOUR APN
 const char gprsUser[] = "";
 const char gprsPass[] = "";
 
-#define uS_TO_S_FACTOR 1000000ULL // Conversion factor for micro seconds to seconds
-#define TIME_TO_SLEEP 5           // Time ESP32 will go to sleep (in seconds)
-
-#define UART_BAUD 115200
-#define PIN_DTR 25
-#define PIN_TX 27
-#define PIN_RX 26
-#define PWR_PIN 4
-
-#define SD_MISO 2
-#define SD_MOSI 15
-#define SD_SCLK 14
-#define SD_CS 13
-#define LED_PIN 12
 
 const GPRSCredentials gprsCredentials = GPRSCredentials("m2m.public.at", "", "");
 
@@ -97,39 +85,6 @@ void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status) {
     // go to sleep
 }
 
-static char log_print_buffer[512];
-
-int vprintf_into_sd(const char *szFormat, va_list args) {
-    String logstring = "[" + rtc.getDateTime() + "] ";
-    logstring += szFormat;
-    // write evaluated format string into buffer
-    int ret = vsnprintf(log_print_buffer, sizeof(log_print_buffer), logstring.c_str(), args);
-
-    String date = rtc.getDate();
-    String filename = "/log_" + date + ".txt";
-
-    // output is now in buffer. write to file.
-    if (ret >= 0) {
-        if (!SD.exists(filename)) {
-            File writeLog = SD.open(filename, FILE_WRITE);
-            if (!writeLog)
-                Serial.println("Couldn't open " + filename + " for writing");
-            delay(50);
-            writeLog.close();
-        }
-
-        File logFile = SD.open(filename, FILE_APPEND);
-
-        // debug output
-        vprintf(logstring.c_str(), args);
-        logFile.write((uint8_t *)log_print_buffer, (size_t)ret);
-        // to be safe in case of crashes: flush the output
-        logFile.flush();
-        logFile.close();
-    }
-    return ret;
-}
-
 String getMacAddressAsString(const uint8_t *mac);
 
 DynamicJsonDocument parseReceivedJsonData(char *data);
@@ -336,8 +291,10 @@ void setup() {
     delay(10);
     setupSDCard();
 
+    SDCardLogger::printDebugToSerial(true);
+
     //	https://stackoverflow.com/questions/60442350/arduinos-esp-log-set-vprintf-does-not-work-on-esp32
-    //    esp_log_set_vprintf(&vprintf_into_sd);
+    esp_log_set_vprintf(&SDCardLogger::vprintf_into_sd);
     esp_log_level_set("*", ESP_LOG_VERBOSE);
     esp_log_write(ESP_LOG_DEBUG, TAG.c_str(), "%s", WiFi.macAddress().c_str());
 
-- 
GitLab