From 11dbea0302e8fff7c2a2fcacf44544c371cac937 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Sat, 16 Jan 2021 13:41:46 +0100 Subject: [PATCH] LOGln now logs to a central syslog server, if available. --- include/config.sample.h | 3 +++ include/syslog.h | 8 ++++++++ include/tools.h | 11 +++++++++-- src/main.cpp | 3 +++ src/syslog.cpp | 17 +++++++++++++++++ src/wifi.cpp | 7 ++++--- 6 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 include/syslog.h create mode 100644 src/syslog.cpp diff --git a/include/config.sample.h b/include/config.sample.h index 5075afb..41332c1 100644 --- a/include/config.sample.h +++ b/include/config.sample.h @@ -39,4 +39,7 @@ #define MQTT_TOPIC_STATE_LONG "espleaf/state_long" #define MQTT_TOPIC_COMMANDS "esplead/cmnd" +#define SYSLOG_HOST "..." +#define SYSLOG_PORT 514 + //#define TEST_MODE \ No newline at end of file diff --git a/include/syslog.h b/include/syslog.h new file mode 100644 index 0000000..3b5ab9c --- /dev/null +++ b/include/syslog.h @@ -0,0 +1,8 @@ +#pragma once + +#include +#include + +#ifdef SYSLOG_HOST + void syslog(String msg, uint8_t severity=7); +#endif \ No newline at end of file diff --git a/include/tools.h b/include/tools.h index d0b247d..14def8f 100644 --- a/include/tools.h +++ b/include/tools.h @@ -1,8 +1,15 @@ #pragma once #include "my_fastled.h" +#include "syslog.h" -#define LOG(...) Serial.printf(__VA_ARGS__) -#define LOGln(...) Serial.printf(__VA_ARGS__); Serial.println() +//#define LOG(...) Serial.printf(__VA_ARGS__) +//#define LOGln(...) Serial.printf(__VA_ARGS__); Serial.println() + +#ifdef SYSLOG_HOST + #define LOGln(...) { char buffer[512]; snprintf(buffer, 512, __VA_ARGS__); syslog(buffer); Serial.println(buffer);} +#else + #define LOGln(...) Serial.printf(__VA_ARGS__); Serial.println() +#endif void clear_leds(); diff --git a/src/main.cpp b/src/main.cpp index 6f8961d..6054cd1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,7 @@ #include "prototypes.h" #include "mqtt.h" #include "state.h" +#include "syslog.h" std::vector nodes; std::list edges; @@ -173,6 +174,8 @@ void setup() { delay(250); show_status(0); #endif + + syslog("Setup done."); } void loop() { diff --git a/src/syslog.cpp b/src/syslog.cpp new file mode 100644 index 0000000..0152c02 --- /dev/null +++ b/src/syslog.cpp @@ -0,0 +1,17 @@ +#include "syslog.h" +#include "config.h" + +#ifdef SYSLOG_HOST + void syslog(String msg, uint8_t severity) { + uint8_t facility = 16; // local0 + if (severity > 7) severity = 7; + WiFiUDP udp; + + udp.beginPacket(SYSLOG_HOST, SYSLOG_PORT); + udp.write("<"); + udp.write(String(facility * 8 + severity).c_str()); + udp.write(">1 - " OTA_HOSTNAME " espleaf 1 1 - "); + udp.write(msg.c_str()); + udp.endPacket(); + } +#endif \ No newline at end of file diff --git a/src/wifi.cpp b/src/wifi.cpp index 5f50473..1fca7e5 100644 --- a/src/wifi.cpp +++ b/src/wifi.cpp @@ -2,13 +2,14 @@ #include "tools.h" void wifi_setup() { - LOG("Connecting to WiFi %s...", WIFI_SSID); + Serial.printf("Connecting to WiFi %s...", WIFI_SSID); WiFi.mode(WIFI_STA); WiFi.begin(WIFI_SSID, WIFI_PASS); while (WiFi.status() != WL_CONNECTED) { - LOG("."); + Serial.print("."); delay(300); } - LOGln(" Connected as %s", WiFi.localIP().toString().c_str()); + Serial.println(); + LOGln("Connected as %s", WiFi.localIP().toString().c_str()); random16_add_entropy(micros()); } \ No newline at end of file