My Project
syslog.h
1 /*
2  * uuid-syslog - Microcontroller syslog service
3  * Copyright 2019,2021-2022 Simon Arlott
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef UUID_SYSLOG_H_
20 #define UUID_SYSLOG_H_
21 
22 #include <Arduino.h>
23 #ifdef ARDUINO_ARCH_ESP8266
24 # include <ESP8266WiFi.h>
25 #else
26 # include <WiFi.h>
27 #endif
28 #include <WiFiUdp.h>
29 #include <time.h>
30 
31 #include <list>
32 #include <memory>
33 #include <string>
34 
35 #include <uuid/log.h>
36 
37 #ifndef UUID_LOG_THREAD_SAFE
38 # define UUID_LOG_THREAD_SAFE 0
39 #endif
40 
41 #ifndef UUID_COMMON_STD_MUTEX_AVAILABLE
42 # define UUID_COMMON_STD_MUTEX_AVAILABLE 0
43 #endif
44 
45 #if defined(DOXYGEN) || UUID_COMMON_STD_MUTEX_AVAILABLE
46 # define UUID_SYSLOG_THREAD_SAFE 1
47 #else
48 # define UUID_SYSLOG_THREAD_SAFE 0
49 #endif
50 
51 #if UUID_LOG_THREAD_SAFE
52 # include <mutex>
53 #endif
54 
55 namespace uuid {
56 
63 namespace syslog {
64 
70 #if UUID_COMMON_THREAD_SAFE && UUID_LOG_THREAD_SAFE && UUID_SYSLOG_THREAD_SAFE
71 static constexpr bool thread_safe = true;
72 #else
73 static constexpr bool thread_safe = false;
74 #endif
75 
82 public:
83  static constexpr size_t MAX_LOG_MESSAGES = 50;
84  static constexpr uint16_t DEFAULT_PORT = 514;
91  SyslogService() = default;
92  ~SyslogService() = default;
93 
99  void start();
100 
110  uuid::log::Level log_level() const;
122  void log_level(uuid::log::Level level);
123 
130  size_t maximum_log_messages() const;
138  void maximum_log_messages(size_t count);
139 
146  size_t current_log_messages() const;
147 
154  std::pair<IPAddress,uint16_t> destination() const;
166  void destination(IPAddress host, uint16_t port = DEFAULT_PORT);
167 
174  std::string hostname() const;
181  void hostname(std::string hostname);
182 
189  unsigned long mark_interval() const;
199  void mark_interval(unsigned long interval);
200 
206  void loop();
207 
219  void operator<<(std::shared_ptr<uuid::log::Message> message) override;
220 
221 private:
232  public:
240  QueuedLogMessage(unsigned long id, std::shared_ptr<uuid::log::Message> &&content);
241  ~QueuedLogMessage() = default;
242 
243  unsigned long id_;
244  struct timeval time_;
245  std::shared_ptr<const uuid::log::Message> content_;
247  private:
248  static bool time_good_;
249  };
250 
262  void add_message(std::shared_ptr<uuid::log::Message> &message);
263 
271 
279  bool can_transmit();
280 
289  bool transmit(const QueuedLogMessage &message);
290 
293  bool started_ = false;
294  bool level_set_ = false;
295  WiFiUDP udp_;
296  IPAddress host_;
297  uint16_t port_ = DEFAULT_PORT;
298  uint64_t last_transmit_ = 0;
299  std::string hostname_{'-'};
300 #if UUID_SYSLOG_THREAD_SAFE
301  mutable std::mutex mutex_;
302 #endif
304  unsigned long log_message_id_ = 0;
305  std::list<QueuedLogMessage> log_messages_;
306  uint64_t mark_interval_ = 0;
307  uint64_t last_message_ = 0;
308 };
309 
310 } // namespace syslog
311 
312 } // namespace uuid
313 
314 #endif
uuid::syslog::SyslogService::QueuedLogMessage::time_
struct timeval time_
Definition: syslog.h:244
uuid::syslog::SyslogService::current_log_messages
size_t current_log_messages() const
Get the current number of queued log messages.
Definition: syslog.cpp:188
uuid::log::Logger
Logger instance used to make log messages.
Definition: log.h:347
uuid::syslog::SyslogService::level_set_
bool level_set_
Definition: syslog.h:294
uuid::syslog::SyslogService::remove_queued_messages
void remove_queued_messages(uuid::log::Level level)
Remove messages that were queued before the log level was set.
Definition: syslog.cpp:132
uuid::syslog::SyslogService::log_message_id_
unsigned long log_message_id_
Definition: syslog.h:304
uuid::syslog::SyslogService::QueuedLogMessage
Log message that has been queued.
Definition: syslog.h:231
uuid::syslog::SyslogService::MAX_LOG_MESSAGES
static constexpr size_t MAX_LOG_MESSAGES
Definition: syslog.h:83
uuid::syslog::SyslogService
Log handler for sending messages to a syslog server.
Definition: syslog.h:81
uuid::syslog::SyslogService::port_
uint16_t port_
Definition: syslog.h:297
uuid::log::Level
Level
Severity level of log messages.
Definition: log.h:84
uuid::syslog::SyslogService::hostname
std::string hostname() const
Get local hostname.
Definition: syslog.cpp:210
uuid::syslog::SyslogService::host_
IPAddress host_
Definition: syslog.h:296
uuid::log::Handler
Logger handler used to process log messages.
Definition: log.h:290
uuid::syslog::SyslogService::DEFAULT_PORT
static constexpr uint16_t DEFAULT_PORT
Definition: syslog.h:84
uuid::syslog::SyslogService::log_messages_
std::list< QueuedLogMessage > log_messages_
Definition: syslog.h:305
uuid::syslog::SyslogService::last_message_
uint64_t last_message_
Definition: syslog.h:307
uuid::syslog::SyslogService::destination
std::pair< IPAddress, uint16_t > destination() const
Get the server to send messages to.
Definition: syslog.cpp:196
uuid::syslog::SyslogService::start
void start()
Register the log handler with the logging framework.
Definition: syslog.cpp:124
uuid::syslog::SyslogService::last_transmit_
uint64_t last_transmit_
Definition: syslog.h:298
uuid::syslog::SyslogService::add_message
void add_message(std::shared_ptr< uuid::log::Message > &message)
Add a new log message.
Definition: syslog.cpp:255
uuid::syslog::SyslogService::QueuedLogMessage::QueuedLogMessage
QueuedLogMessage(unsigned long id, std::shared_ptr< uuid::log::Message > &&content)
Create a queued log message.
Definition: syslog.cpp:230
uuid::syslog::SyslogService::QueuedLogMessage::id_
unsigned long id_
Definition: syslog.h:243
uuid::syslog::SyslogService::udp_
WiFiUDP udp_
Definition: syslog.h:295
uuid::syslog::SyslogService::logger_
static uuid::log::Logger logger_
Definition: syslog.h:291
uuid
Common utilities.
Definition: get_uptime_ms.cpp:28
uuid::syslog::SyslogService::hostname_
std::string hostname_
Definition: syslog.h:299
uuid::syslog::SyslogService::transmit
bool transmit(const QueuedLogMessage &message)
Attempt to transmit one message to the server.
Definition: syslog.cpp:472
uuid::syslog::SyslogService::QueuedLogMessage::content_
std::shared_ptr< const uuid::log::Message > content_
Definition: syslog.h:245
uuid::syslog::SyslogService::operator<<
void operator<<(std::shared_ptr< uuid::log::Message > message) override
Add a new log message.
Definition: syslog.cpp:263
uuid::syslog::SyslogService::mark_interval
unsigned long mark_interval() const
Get mark interval.
Definition: syslog.cpp:222
uuid::syslog::SyslogService::log_level
uuid::log::Level log_level() const
Get the current log level.
Definition: syslog.cpp:128
uuid::syslog::SyslogService::maximum_log_messages_
size_t maximum_log_messages_
Definition: syslog.h:303
uuid::syslog::SyslogService::maximum_log_messages
size_t maximum_log_messages() const
Get the maximum number of queued log messages.
Definition: syslog.cpp:168
uuid::syslog::SyslogService::started_
bool started_
Definition: syslog.h:293
uuid::syslog::SyslogService::can_transmit
bool can_transmit()
Check if it is possible to transmit to the server.
Definition: syslog.cpp:347
uuid::syslog::SyslogService::QueuedLogMessage::time_good_
static bool time_good_
Definition: syslog.h:248
uuid::syslog::SyslogService::mutex_
std::mutex mutex_
Definition: syslog.h:301
uuid::syslog::thread_safe
static constexpr bool thread_safe
Thread-safe status of the library.
Definition: syslog.h:73
uuid::syslog::SyslogService::SyslogService
SyslogService()=default
Create a new syslog service log handler.
uuid::syslog::SyslogService::loop
void loop()
Dispatch queued log messages.
Definition: syslog.cpp:271
uuid::syslog::SyslogService::mark_interval_
uint64_t mark_interval_
Definition: syslog.h:306