From 01504729d874009ca55a4c60ac256c12cbcc3f61 Mon Sep 17 00:00:00 2001 From: Paul Date: Wed, 20 Nov 2019 22:40:04 +0100 Subject: [PATCH] syslog replacing event log - https://github.com/proddy/EMS-ESP/issues/242 --- src/MyESP.cpp | 31 ++++++++++++++++--------------- src/MyESP.h | 2 +- src/ems.cpp | 22 ++++++++++++++++------ src/websrc/myesp.htm | 2 +- 4 files changed, 34 insertions(+), 23 deletions(-) diff --git a/src/MyESP.cpp b/src/MyESP.cpp index 7907ad10e..6c3f97e7b 100644 --- a/src/MyESP.cpp +++ b/src/MyESP.cpp @@ -44,9 +44,9 @@ MyESP::MyESP() { _suspendOutput = false; _ota_pre_callback_f = nullptr; _ota_post_callback_f = nullptr; - _load_average = 100; // calculated load average - _general_serial = true; // serial is set to on as default - _general_log_events = true; // all logs are sent to syslog + _load_average = 100; // calculated load average + _general_serial = true; // serial is set to on as default + _general_log_events = false; // all logs are not sent to syslog by default _general_log_ip = nullptr; _have_ntp_time = false; @@ -1729,7 +1729,7 @@ bool MyESP::_fs_loadConfig() { _general_password = strdup(general["password"] | MYESP_HTTP_PASSWORD); _ws->setAuthentication("admin", _general_password); _general_hostname = strdup(general["hostname"]); - _general_log_events = general["log_events"]; + _general_log_events = general["log_events"] | false; _general_log_ip = strdup(general["log_ip"] | ""); // serial is only on when booting @@ -1880,7 +1880,7 @@ bool MyESP::fs_saveCustomConfig(JsonObject root) { configFile.close(); if (n) { - _writeLogEvent(MYESP_SYSLOG_INFO, "Custom config stored in the SPIFFS"); + writeLogEvent(MYESP_SYSLOG_INFO, "Custom config stored in the SPIFFS"); ok = true; } } @@ -1913,7 +1913,7 @@ bool MyESP::fs_saveConfig(JsonObject root) { configFile.close(); if (n) { - _writeLogEvent(MYESP_SYSLOG_INFO, "System config stored in the SPIFFS"); + writeLogEvent(MYESP_SYSLOG_INFO, "System config stored in the SPIFFS"); ok = true; } @@ -2257,7 +2257,7 @@ void MyESP::crashInfo() { #endif // write a log entry to SysLog via UDP -void MyESP::_writeLogEvent(const uint8_t type, const char * msg) { +void MyESP::writeLogEvent(const uint8_t type, const char * msg) { if (!_general_log_events) { return; } @@ -2781,7 +2781,7 @@ void MyESP::_bootupSequence() { if (boot_status == MYESP_BOOTSTATUS_BOOTED) { if ((_ntp_enabled) && (now() > 10000) && !_have_ntp_time) { _have_ntp_time = true; - _writeLogEvent(MYESP_SYSLOG_INFO, "System booted"); + writeLogEvent(MYESP_SYSLOG_INFO, "System booted"); } return; } @@ -2811,7 +2811,7 @@ void MyESP::_bootupSequence() { // write a log message if we're not using NTP, otherwise wait for the internet time to arrive if (!_ntp_enabled) { - _writeLogEvent(MYESP_SYSLOG_INFO, "System booted"); + writeLogEvent(MYESP_SYSLOG_INFO, "System booted"); } } } @@ -2819,7 +2819,7 @@ void MyESP::_bootupSequence() { // set up SysLog void MyESP::_syslog_setup() { // if not enabled or IP is empty, don't bother - if ( (!_hasValue(_general_log_ip)) || (!_general_log_events) ) { + if ((!_hasValue(_general_log_ip)) || (!_general_log_events)) { return; } @@ -2831,10 +2831,12 @@ void MyESP::_syslog_setup() { syslog.mark_interval(3600); IPAddress syslog_ip; - syslog.destination(syslog_ip.fromString(_general_log_ip)); + syslog_ip.fromString("192.168.1.4"); + syslog.destination(syslog_ip); + + logger.info(F("Application started")); myDebug_P(PSTR("[SYSLOG] System event logging enabled")); - } // setup MyESP @@ -2857,8 +2859,6 @@ void MyESP::begin(const char * app_hostname, const char * app_name, const char * // print a welcome message myDebug_P(PSTR("\n\n* %s version %s"), _app_name, _app_version); - _syslog_setup(); // SysLog - // set up onboard LED pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, HIGH); @@ -2870,6 +2870,7 @@ void MyESP::begin(const char * app_hostname, const char * app_name, const char * _wifi_setup(); // WIFI setup _mqtt_setup(); // MQTT setup _ota_setup(); // init OTA + _syslog_setup(); // SysLog _webserver_setup(); // init web server _setSystemCheck(false); // reset system check @@ -2918,7 +2919,7 @@ void MyESP::loop() { } if (_shouldRestart) { - _writeLogEvent(MYESP_SYSLOG_INFO, "System is restarting"); + writeLogEvent(MYESP_SYSLOG_INFO, "System is restarting"); myDebug("[SYSTEM] Restarting..."); _deferredReset(500, CUSTOM_RESET_TERMINAL); ESP.restart(); diff --git a/src/MyESP.h b/src/MyESP.h index d6a72ba4a..851eb433c 100644 --- a/src/MyESP.h +++ b/src/MyESP.h @@ -298,7 +298,7 @@ class MyESP { void setUseSerial(bool toggle); // syslog - void _writeLogEvent(const uint8_t type, const char * msg); + void writeLogEvent(const uint8_t type, const char * msg); // FS void setSettings(fs_loadsave_callback_f loadsave, fs_setlist_callback_f setlist, bool useSerial = true); diff --git a/src/ems.cpp b/src/ems.cpp index afc1db953..f3d093b3b 100644 --- a/src/ems.cpp +++ b/src/ems.cpp @@ -531,7 +531,9 @@ void _debugPrintTelegram(const char * prefix, _EMS_RxTelegram * EMS_RxTelegram, } strlcpy(output_str, "(", sizeof(output_str)); - strlcat(output_str, COLOR_CYAN, sizeof(output_str)); + + if (!raw) + strlcat(output_str, COLOR_CYAN, sizeof(output_str)); strlcat(output_str, _smallitoa(t_hour, buffer), sizeof(output_str)); strlcat(output_str, ":", sizeof(output_str)); @@ -545,9 +547,14 @@ void _debugPrintTelegram(const char * prefix, _EMS_RxTelegram * EMS_RxTelegram, strlcat(output_str, _smallitoa3(t_msec, buffer), sizeof(output_str)); } - strlcat(output_str, COLOR_RESET, sizeof(output_str)); + if (!raw) + strlcat(output_str, COLOR_RESET, sizeof(output_str)); + strlcat(output_str, ") ", sizeof(output_str)); - strlcat(output_str, color, sizeof(output_str)); + + if (!raw) + strlcat(output_str, color, sizeof(output_str)); + strlcat(output_str, prefix, sizeof(output_str)); if (!raw) { @@ -569,9 +576,12 @@ void _debugPrintTelegram(const char * prefix, _EMS_RxTelegram * EMS_RxTelegram, strlcat(output_str, " #data=", sizeof(output_str)); strlcat(output_str, itoa(data_len, buffer, 10), sizeof(output_str)); } - } - strlcat(output_str, COLOR_RESET, sizeof(output_str)); + strlcat(output_str, COLOR_RESET, sizeof(output_str)); + } else { + // send it the SysLog + myESP.writeLogEvent(MYESP_SYSLOG_INFO, output_str); + } myDebug(output_str); } @@ -910,7 +920,7 @@ void ems_parseTelegram(uint8_t * telegram, uint8_t length) { } // if we are in raw logging mode then just print out the telegram as it is - // or if we're watching a specific type ID show it + // else if we're watching a specific type ID show it and also log an event to the SysLog // but still continue to process it if ((EMS_Sys_Status.emsLogging == EMS_SYS_LOGGING_RAW)) { _debugPrintTelegram("", &EMS_RxTelegram, COLOR_WHITE, true); diff --git a/src/websrc/myesp.htm b/src/websrc/myesp.htm index a14e1e422..2ada3ff59 100644 --- a/src/websrc/myesp.htm +++ b/src/websrc/myesp.htm @@ -130,7 +130,7 @@
+ data-content="IP address of the SysLog server running on UDP port 514">