updated uuid libs

This commit is contained in:
Proddy
2023-01-05 15:05:30 +01:00
parent 6370296c53
commit 3b196fc90d
27 changed files with 2013 additions and 1411 deletions

View File

@@ -18,7 +18,6 @@
#include "uuid/syslog.h"
#include "../../../src/emsesp.h"
#ifndef UUID_SYSLOG_HAVE_GETTIMEOFDAY
@@ -32,12 +31,6 @@
#define UUID_SYSLOG_HAVE_GETTIMEOFDAY 0
#endif
#ifndef UUID_SYSLOG_HAVE_IPADDRESS_TYPE
#if defined(ARDUINO_ARCH_ESP8266)
#define UUID_SYSLOG_HAVE_IPADDRESS_TYPE 1
#endif
#endif
#ifndef UUID_SYSLOG_HAVE_IPADDRESS_TYPE
#define UUID_SYSLOG_HAVE_IPADDRESS_TYPE 0
#endif
@@ -77,11 +70,26 @@
#include <algorithm>
#include <list>
#include <memory>
#if UUID_SYSLOG_THREAD_SAFE
#include <mutex>
#endif
#include <string>
#include <uuid/common.h>
#include <uuid/log.h>
#ifndef UUID_SYSLOG_UDP_BASE_MESSAGE_DELAY
#define UUID_SYSLOG_UDP_BASE_MESSAGE_DELAY 100
#endif
#ifndef UUID_SYSLOG_UDP_IPV4_ARP_MESSAGE_DELAY
#define UUID_SYSLOG_UDP_IPV4_ARP_MESSAGE_DELAY 10
#endif
#ifndef UUID_SYSLOG_UDP_IPV6_NDP_MESSAGE_DELAY
#define UUID_SYSLOG_UDP_IPV6_NDP_MESSAGE_DELAY 10
#endif
static const char __pstr__logger_name[] = "syslog";
namespace uuid {
@@ -91,10 +99,6 @@ namespace syslog {
uuid::log::Logger SyslogService::logger_{__pstr__logger_name, uuid::log::Facility::SYSLOG};
bool SyslogService::QueuedLogMessage::time_good_ = false;
SyslogService::~SyslogService() {
uuid::log::Logger::unregister_handler(this);
}
void SyslogService::start() {
uuid::log::Logger::register_handler(this, uuid::log::Level::ALL);
}
@@ -104,6 +108,9 @@ uuid::log::Level SyslogService::log_level() const {
}
void SyslogService::remove_queued_messages(uuid::log::Level level) {
#if UUID_SYSLOG_THREAD_SAFE
std::lock_guard<std::mutex> lock{mutex_};
#endif
unsigned long offset = 0;
for (auto it = log_messages_.begin(); it != log_messages_.end();) {
@@ -130,17 +137,23 @@ void SyslogService::log_level(uuid::log::Level level) {
level_set = true;
if (level_changed) {
logger_.info(F("Log level set to %S"), uuid::log::format_level_uppercase(level));
logger_.info("Log level set to %S", uuid::log::format_level_uppercase(level));
}
uuid::log::Logger::register_handler(this, level);
}
size_t SyslogService::maximum_log_messages() const {
#if UUID_SYSLOG_THREAD_SAFE
std::lock_guard<std::mutex> lock{mutex_};
#endif
return maximum_log_messages_;
}
void SyslogService::maximum_log_messages(size_t count) {
#if UUID_SYSLOG_THREAD_SAFE
std::lock_guard<std::mutex> lock{mutex_};
#endif
maximum_log_messages_ = std::max((size_t)1, count);
while (log_messages_.size() > maximum_log_messages_) {
@@ -148,8 +161,16 @@ void SyslogService::maximum_log_messages(size_t count) {
}
}
size_t SyslogService::current_log_messages() const {
#if UUID_SYSLOG_THREAD_SAFE
std::lock_guard<std::mutex> lock{mutex_};
#endif
return log_messages_.size();
}
std::pair<IPAddress, uint16_t> SyslogService::destination() const {
return std::make_pair(ip_, port_);
return std::make_pair(host_, port_);
}
void SyslogService::destination(IPAddress host, uint16_t port) {
@@ -159,28 +180,7 @@ void SyslogService::destination(IPAddress host, uint16_t port) {
if ((uint32_t)ip_ == (uint32_t)0) {
started_ = false;
remove_queued_messages(log_level());
host_.clear();
}
}
void SyslogService::destination(const char * host, uint16_t port) {
if (host == nullptr || host[0] == '\0') {
started_ = false;
remove_queued_messages(log_level());
ip_ = (IPAddress)(uint32_t)0;
host_.clear();
return;
}
host_ = host;
port_ = port;
if (ip_.fromString(host)) {
host_.clear();
if ((uint32_t)ip_ == (uint32_t)0) {
started_ = false;
remove_queued_messages(log_level());
}
} else {
ip_ = (IPAddress)(uint32_t)0;
// host_.clear();
}
}
@@ -207,7 +207,8 @@ void SyslogService::mark_interval(unsigned long interval) {
SyslogService::QueuedLogMessage::QueuedLogMessage(unsigned long id, std::shared_ptr<uuid::log::Message> && content)
: id_(id)
, content_(std::move(content)) {
// Added by proddy - check for Ethernet too. This assumes the network has already started.
// Added by proddy for EMS-ESP
// check for Ethernet too. This assumes the network has already started.
if (time_good_ || emsesp::EMSESP::system_.network_connected()) {
#if UUID_SYSLOG_HAVE_GETTIMEOFDAY
if (gettimeofday(&time_, nullptr) != 0) {
@@ -229,9 +230,9 @@ SyslogService::QueuedLogMessage::QueuedLogMessage(unsigned long id, std::shared_
}
}
void SyslogService::operator<<(std::shared_ptr<uuid::log::Message> message) {
/* Mutex already locked by caller. */
void SyslogService::add_message(std::shared_ptr<uuid::log::Message> & message) {
if (log_messages_.size() >= maximum_log_messages_) {
log_messages_overflow_ = true;
log_messages_.pop_front();
log_message_fails_++;
}
@@ -239,89 +240,140 @@ void SyslogService::operator<<(std::shared_ptr<uuid::log::Message> message) {
log_messages_.emplace_back(log_message_id_++, std::move(message));
}
void SyslogService::operator<<(std::shared_ptr<uuid::log::Message> message) {
#if UUID_SYSLOG_THREAD_SAFE
std::lock_guard<std::mutex> lock{mutex_};
#endif
add_message(message);
}
void SyslogService::loop() {
while (!log_messages_.empty() && can_transmit()) {
#if UUID_SYSLOG_THREAD_SAFE
std::unique_lock<std::mutex> lock{mutex_};
#endif
size_t count = std::max((size_t)1, MAX_LOG_MESSAGES);
while (!log_messages_.empty()) {
#if UUID_SYSLOG_THREAD_SAFE
lock.unlock();
#endif
if (!can_transmit())
return;
#if UUID_SYSLOG_THREAD_SAFE
lock.lock();
if (log_messages_.empty())
break;
#endif
auto message = log_messages_.front();
started_ = true;
log_messages_overflow_ = false;
auto ok = transmit(message);
started_ = true;
#if UUID_SYSLOG_THREAD_SAFE
lock.unlock();
#endif
auto ok = transmit(message);
if (ok) {
// The transmit() may have called yield() allowing
// other messages to have been added to the queue.
if (!log_messages_overflow_) {
#if UUID_SYSLOG_THREAD_SAFE
lock.lock();
#endif
last_message_ = last_transmit_;
if (!log_messages_.empty() && log_messages_.front().content_ == message.content_) {
log_messages_.pop_front();
}
last_message_ = uuid::get_uptime_ms();
#if UUID_SYSLOG_THREAD_SAFE
lock.unlock();
#endif
}
::yield();
#if UUID_SYSLOG_THREAD_SAFE
lock.lock();
#endif
if (!ok) {
break;
}
count--;
if (count == 0) {
break;
}
}
if (started_ && mark_interval_ != 0 && log_messages_.empty()) {
if (uuid::get_uptime_ms() - last_message_ >= mark_interval_) {
// This is generated manually because the log level may not
// be high enough to receive INFO messages.
operator<<(std::make_shared<uuid::log::Message>(uuid::get_uptime_ms(),
uuid::log::Level::INFO,
uuid::log::Facility::SYSLOG,
(__pstr__logger_name),
(F("-- MARK --"))));
auto message = std::make_shared<uuid::log::Message>(uuid::get_uptime_ms(),
uuid::log::Level::INFO,
uuid::log::Facility::SYSLOG,
(__pstr__logger_name),
"-- MARK --");
add_message(message);
}
}
}
bool SyslogService::can_transmit() {
// TODO add this like in v3.5?
/*
if (!host_.empty() && (uint32_t)ip_ == 0) {
WiFi.hostByName(host_.c_str(), ip_);
}
*/
#if UUID_SYSLOG_HAVE_IPADDRESS_TYPE
if (ip_.isV4() && (uint32_t)ip_ == (uint32_t)0) {
if (host_.isV4() && (uint32_t)host_ == (uint32_t)0) {
return false;
}
#else
if ((uint32_t)ip_ == (uint32_t)0) {
if ((uint32_t)host_ == (uint32_t)0) {
return false;
}
#endif
if (!emsesp::EMSESP::system_.network_connected()) {
return false; // added by proddy. Check Ethernet
if (WiFi.status() != WL_CONNECTED) {
return false;
}
const uint64_t now = uuid::get_uptime_ms();
uint64_t message_delay = 100;
uint64_t message_delay = UUID_SYSLOG_UDP_BASE_MESSAGE_DELAY;
#if UUID_SYSLOG_ARP_CHECK
#if UUID_SYSLOG_HAVE_IPADDRESS_TYPE
if (ip_.isV4())
if (host_.isV4())
#endif
{
message_delay = 10;
message_delay = UUID_SYSLOG_UDP_IPV4_ARP_MESSAGE_DELAY;
}
#endif
#if UUID_SYSLOG_NDP_CHECK && UUID_SYSLOG_HAVE_IPADDRESS_TYPE
if (ip_.isV6()) {
message_delay = 10;
if (host_.isV6()) {
message_delay = UUID_SYSLOG_UDP_IPV6_NDP_MESSAGE_DELAY;
}
#endif
if (now < last_transmit_ || now - last_transmit_ < message_delay) {
if (started_ && (now < last_transmit_ || now - last_transmit_ < message_delay)) {
return false;
}
#if UUID_SYSLOG_ARP_CHECK
#if UUID_SYSLOG_HAVE_IPADDRESS_TYPE
if (ip_.isV4())
if (host_.isV4())
#endif
{
ip4_addr_t ipaddr;
ip4_addr_set_u32(&ipaddr, (uint32_t)ip_);
ip4_addr_set_u32(&ipaddr, (uint32_t)host_);
if (!ip4_addr_isloopback(&ipaddr) && !ip4_addr_ismulticast(&ipaddr) && !ip4_addr_isbroadcast(&ipaddr, netif_default)) {
struct eth_addr * eth_ret = nullptr;
@@ -348,10 +400,10 @@ bool SyslogService::can_transmit() {
#endif
#if UUID_SYSLOG_NDP_CHECK && UUID_SYSLOG_HAVE_IPADDRESS_TYPE
if (ip_.isV6()) {
if (host_.isV6()) {
ip6_addr_t ip6addr;
IP6_ADDR(&ip6addr, ip_.raw6()[0], ip_.raw6()[1], ip_.raw6()[2], ip_.raw6()[3]);
IP6_ADDR(&ip6addr, host_.raw6()[0], host_.raw6()[1], host_.raw6()[2], host_.raw6()[3]);
ip6_addr_assign_zone(&ip6addr, IP6_UNICAST, netif_default);
if (!ip6_addr_isloopback(&ip6addr) && !ip6_addr_ismulticast(&ip6addr)) {
@@ -361,26 +413,17 @@ bool SyslogService::can_transmit() {
for (size_t i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
if (ip6_addr_isvalid(netif_ip6_addr_state(netif_default, i))) {
if (ip6_addr_isglobal(&ip6addr)) {
if (ip6_addr_isglobal(netif_ip6_addr(netif_default, i))) {
have_address = true;
break;
}
} else if (ip6_addr_issitelocal(&ip6addr)) {
if (ip6_addr_issitelocal(netif_ip6_addr(netif_default, i))) {
have_address = true;
break;
}
} else if (ip6_addr_isuniquelocal(&ip6addr)) {
if (ip6_addr_isuniquelocal(netif_ip6_addr(netif_default, i))) {
have_address = true;
break;
}
} else if (ip6_addr_islinklocal(&ip6addr)) {
if (ip6_addr_islinklocal(&ip6addr)) {
if (ip6_addr_islinklocal(netif_ip6_addr(netif_default, i))) {
have_address = true;
break;
}
} else if (ip6_addr_isglobal(&ip6addr) || ip6_addr_isuniquelocal(&ip6addr) || ip6_addr_issitelocal(&ip6addr)) {
if (ip6_addr_isglobal(netif_ip6_addr(netif_default, i)) || ip6_addr_isuniquelocal(netif_ip6_addr(netif_default, i))
|| ip6_addr_issitelocal(netif_ip6_addr(netif_default, i))) {
have_address = true;
break;
}
} else {
have_address = true;
break;
@@ -408,8 +451,11 @@ bool SyslogService::can_transmit() {
bool SyslogService::transmit(const QueuedLogMessage & message) {
struct tm tm;
int8_t tzh = 0;
int8_t tzm = 0;
// Changes for EMS-ESP by MichaelDvP
// TODO add this like in v3.5?
// int8_t tzh = 0;
// int8_t tzm = 0;
tm.tm_year = 0;
if (message.time_.tv_sec != (time_t)-1) {
@@ -418,8 +464,10 @@ bool SyslogService::transmit(const QueuedLogMessage & message) {
localtime_r(&message.time_.tv_sec, &tm);
int16_t diff = 60 * (tm.tm_hour - utc.tm_hour) + tm.tm_min - utc.tm_min;
diff = diff > 720 ? diff - 1440 : diff < -720 ? diff + 1440 : diff;
tzh = diff / 60;
tzm = diff < 0 ? (0 - diff) % 60 : diff % 60;
// From previous EMS-ESP. Need to check if still needed.
// tzh = diff / 60;
// tzm = diff < 0 ? (0 - diff) % 60 : diff % 60;
}
if (udp_.beginPacket(ip_, port_) != 1) {
@@ -427,26 +475,54 @@ bool SyslogService::transmit(const QueuedLogMessage & message) {
return false;
}
udp_.printf_P(PSTR("<%u>1 "), ((unsigned int)message.content_->facility * 8) + std::min(7U, (unsigned int)message.content_->level));
/*
* The level is constrained to 0-7 by design in RFC 5424 because higher
* severity values would be a different severity in another facility. The
* TRACE level and all other invalid values (because of the conversion to
* unsigned) are converted to DEBUG.
*
* RFC 5424 requires that the facility MUST be 0-23. Values here are allowed
* to go up to 31 because there is no obvious candidate to convert them to
* and because the multiplication by a factor of 256 means that (with the
* cast back to uint8_t) higher values just overflow into other facilities
* (which is easier than doing it by modulo 24). The enum doesn't allow
* values that are out of range of the RFC.
*
* The maximum possible priority value does not exceed the requirement that
* the PRI part MUST be 3-5 characters.
*/
udp_.printf("<%u>1 ", (uint8_t)(message.content_->facility * 8U) + std::min(7U, (unsigned int)message.content_->level));
if (tm.tm_year != 0) {
udp_.printf_P(PSTR("%04u-%02u-%02uT%02u:%02u:%02u.%06u%+02d:%02d"),
udp_.printf_P("%04u-%02u-%02uT%02u:%02u:%02u.%06luZ",
tm.tm_year + 1900,
tm.tm_mon + 1,
tm.tm_mday,
tm.tm_hour,
tm.tm_min,
tm.tm_sec,
(uint32_t)message.time_.tv_usec,
tzh,
tzm);
(unsigned long)message.time_.tv_usec);
// udp_.printf("%04u-%02u-%02uT%02u:%02u:%02u.%06u%+02d:%02d",
// tm.tm_year + 1900,
// tm.tm_mon + 1,
// tm.tm_mday,
// tm.tm_hour,
// tm.tm_min,
// tm.tm_sec,
// (unsigned long)message.time_.tv_usec,
// tzh,
// tzm);
} else {
udp_.print('-');
}
// Changes for EMS-ESP by MichaelDvP
udp_.printf_P(PSTR(" %s %s - - - "), hostname_.c_str(), (message.content_->name));
char id_c_str[15];
snprintf_P(id_c_str, sizeof(id_c_str), PSTR(" %lu: "), message.id_);
snprintf(id_c_str, sizeof(id_c_str), " %lu: ", message.id_);
std::string msgstr = uuid::log::format_timestamp_ms(message.content_->uptime_ms, 3) + ' ' + uuid::log::format_level_char(message.content_->level) + id_c_str
+ message.content_->text;
for (uint16_t i = 0; i < msgstr.length(); i++) {

View File

@@ -1,6 +1,6 @@
/*
* uuid-syslog - Syslog service
* Copyright 2019 Simon Arlott
* uuid-syslog - Microcontroller syslog service
* Copyright 2019,2021-2022 Simon Arlott
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -20,21 +20,34 @@
#define UUID_SYSLOG_H_
#include <Arduino.h>
#ifdef ARDUINO_ARCH_ESP8266
#include <ESP8266WiFi.h>
#else
#include <WiFi.h>
#endif
#include <WiFiUdp.h>
#include <time.h>
#include <atomic>
#include <list>
#include <memory>
#include <string>
#include <uuid/log.h>
#ifndef UUID_LOG_THREAD_SAFE
#define UUID_LOG_THREAD_SAFE 0
#endif
#ifndef UUID_COMMON_STD_MUTEX_AVAILABLE
#define UUID_COMMON_STD_MUTEX_AVAILABLE 0
#endif
#if UUID_COMMON_STD_MUTEX_AVAILABLE
#define UUID_SYSLOG_THREAD_SAFE 1
#else
#define UUID_SYSLOG_THREAD_SAFE 0
#endif
#if UUID_LOG_THREAD_SAFE
#include <mutex>
#endif
namespace uuid {
/**
@@ -45,6 +58,17 @@ namespace uuid {
*/
namespace syslog {
/**
* Thread-safe status of the library.
*
* @since 2.2.0
*/
#if UUID_COMMON_THREAD_SAFE && UUID_LOG_THREAD_SAFE && UUID_SYSLOG_THREAD_SAFE
static constexpr bool thread_safe = true;
#else
static constexpr bool thread_safe = false;
#endif
/**
* Log handler for sending messages to a syslog server.
*
@@ -60,9 +84,8 @@ class SyslogService : public uuid::log::Handler {
*
* @since 1.0.0
*/
SyslogService() = default;
~SyslogService();
SyslogService() = default;
~SyslogService() = default;
/**
* Register the log handler with the logging framework.
@@ -110,6 +133,14 @@ class SyslogService : public uuid::log::Handler {
*/
void maximum_log_messages(size_t count);
/**
* Get the current number of queued log messages.
*
* @return The current number of queued log messages.
* @since 2.1.0
*/
size_t current_log_messages() const;
/**
* Get the server to send messages to.
*
@@ -129,7 +160,6 @@ class SyslogService : public uuid::log::Handler {
* @since 2.0.0
*/
void destination(IPAddress host, uint16_t port = DEFAULT_PORT);
void destination(const char * host, uint16_t port = DEFAULT_PORT);
/**
* Get local hostname.
@@ -185,7 +215,7 @@ class SyslogService : public uuid::log::Handler {
virtual void operator<<(std::shared_ptr<uuid::log::Message> message);
/**
* added MichaelDvP
* added for EMS-ESP
* query status variables
*/
size_t queued() {
@@ -227,14 +257,27 @@ class SyslogService : public uuid::log::Handler {
QueuedLogMessage(unsigned long id, std::shared_ptr<uuid::log::Message> && content);
~QueuedLogMessage() = default;
unsigned long id_; /*!< Sequential identifier for this log message. @since 1.0.0 */
struct timeval time_; /*!< Time message was received. @since 1.0.0 */
const std::shared_ptr<const uuid::log::Message> content_; /*!< Log message content. @since 1.0.0 */
unsigned long id_; /*!< Sequential identifier for this log message. @since 1.0.0 */
struct timeval time_; /*!< Time message was received. @since 1.0.0 */
std::shared_ptr<const uuid::log::Message> content_; /*!< Log message content. @since 1.0.0 */
private:
static bool time_good_; /*!< System time appears to be valid. @since 1.0.0 */
};
/**
* Add a new log message.
*
* This will be put in a queue for output at the next loop()
* process. The queue has a maximum size of
* get_maximum_log_messages() and will discard the oldest message
* first.
*
* @param[in] message New log message, shared by all handlers.
* @since 2.2.0
*/
void add_message(std::shared_ptr<uuid::log::Message> & message);
/**
* Remove messages that were queued before the log level was set.
*
@@ -264,20 +307,25 @@ class SyslogService : public uuid::log::Handler {
static uuid::log::Logger logger_; /*!< uuid::log::Logger instance for syslog services. @since 1.0.0 */
bool started_ = false; /*!< Flag to indicate that messages have started being transmitted. @since 1.0.0 */
WiFiUDP udp_; /*!< UDP client. @since 1.0.0 */
IPAddress ip_; /*!< Host-IP to send messages to. @since 1.0.0 */
std::string host_; /*!< Host to send messages to. */
uint16_t port_ = DEFAULT_PORT; /*!< Port to send messages to. @since 1.0.0 */
uint64_t last_transmit_ = 0; /*!< Last transmit time. @since 1.0.0 */
std::string hostname_{'-'}; /*!< Local hostname. @since 1.0.0 */
bool started_ = false; /*!< Flag to indicate that messages have started being transmitted. @since 1.0.0 */
bool level_set_ = false; /*!< Flag to indicate that the log level has been set at least once. @since 2.2.0 */
WiFiUDP udp_; /*!< UDP client. @since 1.0.0 */
IPAddress host_; /*!< Host to send messages to. @since 1.0.0 */
uint16_t port_ = DEFAULT_PORT; /*!< Port to send messages to. @since 1.0.0 */
uint64_t last_transmit_ = 0; /*!< Last transmit time. @since 1.0.0 */
std::string hostname_{'-'}; /*!< Local hostname. @since 1.0.0 */
#if UUID_SYSLOG_THREAD_SAFE
mutable std::mutex mutex_; /*!< Mutex for queued log messages. @since 2.2.0 */
#endif
size_t maximum_log_messages_ = MAX_LOG_MESSAGES; /*!< Maximum number of log messages to buffer before they are output. @since 1.0.0 */
unsigned long log_message_id_ = 0; /*!< The next identifier to use for queued log messages. @since 1.0.0 */
std::list<QueuedLogMessage> log_messages_; /*!< Queued log messages, in the order they were received. @since 1.0.0 */
std::atomic<bool> log_messages_overflow_{false}; /*!< Check if log messages have overflowed the buffer. @since 1.0.0 */
uint64_t mark_interval_ = 0; /*!< Mark interval in milliseconds. @since 2.0.0 */
uint64_t last_message_ = 0; /*!< Last message/mark time. @since 2.0.0 */
unsigned long log_message_fails_ = 0;
uint64_t mark_interval_ = 0; /*!< Mark interval in milliseconds. @since 2.0.0 */
uint64_t last_message_ = 0; /*!< Last message/mark time. @since 2.0.0 */
// added by MichaelDvP for EMS-ESP
IPAddress ip_; /*!< Host-IP to send messages to. @since 1.0.0 */
unsigned long log_message_fails_ = 0;
};
} // namespace syslog