My Project
Classes | Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
uuid::syslog::SyslogService Class Reference

Log handler for sending messages to a syslog server. More...

#include <uuid/syslog.h>

Inheritance diagram for uuid::syslog::SyslogService:
Inheritance graph
[legend]
Collaboration diagram for uuid::syslog::SyslogService:
Collaboration graph
[legend]

Classes

class  QueuedLogMessage
 Log message that has been queued. More...
 

Public Member Functions

 SyslogService ()=default
 Create a new syslog service log handler. More...
 
void start ()
 Register the log handler with the logging framework. More...
 
uuid::log::Level log_level () const
 Get the current log level. More...
 
void log_level (uuid::log::Level level)
 Set the current log level. More...
 
size_t maximum_log_messages () const
 Get the maximum number of queued log messages. More...
 
void maximum_log_messages (size_t count)
 Set the maximum number of queued log messages. More...
 
size_t current_log_messages () const
 Get the current number of queued log messages. More...
 
std::pair< IPAddress, uint16_t > destination () const
 Get the server to send messages to. More...
 
void destination (IPAddress host, uint16_t port=DEFAULT_PORT)
 Set the server to send messages to. More...
 
std::string hostname () const
 Get local hostname. More...
 
void hostname (std::string hostname)
 Set local hostname. More...
 
unsigned long mark_interval () const
 Get mark interval. More...
 
void mark_interval (unsigned long interval)
 Set mark interval. More...
 
void loop ()
 Dispatch queued log messages. More...
 
void operator<< (std::shared_ptr< uuid::log::Message > message) override
 Add a new log message. More...
 

Static Public Attributes

static constexpr size_t MAX_LOG_MESSAGES = 50
 
static constexpr uint16_t DEFAULT_PORT = 514
 

Private Member Functions

void add_message (std::shared_ptr< uuid::log::Message > &message)
 Add a new log message. More...
 
void remove_queued_messages (uuid::log::Level level)
 Remove messages that were queued before the log level was set. More...
 
bool can_transmit ()
 Check if it is possible to transmit to the server. More...
 
bool transmit (const QueuedLogMessage &message)
 Attempt to transmit one message to the server. More...
 

Private Attributes

bool started_ = false
 
bool level_set_ = false
 
WiFiUDP udp_
 
IPAddress host_
 
uint16_t port_ = DEFAULT_PORT
 
uint64_t last_transmit_ = 0
 
std::string hostname_ {'-'}
 
std::mutex mutex_
 
size_t maximum_log_messages_ = MAX_LOG_MESSAGES
 
unsigned long log_message_id_ = 0
 
std::list< QueuedLogMessagelog_messages_
 
uint64_t mark_interval_ = 0
 
uint64_t last_message_ = 0
 

Static Private Attributes

static uuid::log::Logger logger_ {FPSTR(__pstr__logger_name), uuid::log::Facility::SYSLOG}
 

Detailed Description

Log handler for sending messages to a syslog server.

Since
1.0.0

Definition at line 81 of file syslog.h.

Constructor & Destructor Documentation

◆ SyslogService()

uuid::syslog::SyslogService::SyslogService ( )
default

Create a new syslog service log handler.

Since
1.0.0

Member Function Documentation

◆ add_message()

void uuid::syslog::SyslogService::add_message ( std::shared_ptr< uuid::log::Message > &  message)
private

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.

Parameters
[in]messageNew log message, shared by all handlers.
Since
2.2.0

Definition at line 255 of file syslog.cpp.

◆ can_transmit()

bool uuid::syslog::SyslogService::can_transmit ( )
private

Check if it is possible to transmit to the server.

Returns
True if it is safe to transmit a message to the server, otherwise false.
Since
1.0.0

Definition at line 347 of file syslog.cpp.

◆ current_log_messages()

size_t uuid::syslog::SyslogService::current_log_messages ( ) const

Get the current number of queued log messages.

Returns
The current number of queued log messages.
Since
2.1.0

Definition at line 188 of file syslog.cpp.

◆ destination() [1/2]

std::pair< IPAddress, uint16_t > uuid::syslog::SyslogService::destination ( ) const

Get the server to send messages to.

Since
2.0.0
Returns
IP address and UDP port of the syslog server.

Definition at line 196 of file syslog.cpp.

◆ destination() [2/2]

void uuid::syslog::SyslogService::destination ( IPAddress  host,
uint16_t  port = DEFAULT_PORT 
)

Set the server to send messages to.

To disable sending messages, set the host to 0.0.0.0 and the log level to uuid::log::Level::OFF (otherwise they will be queued but not sent).

Parameters
[in]hostIP address of the syslog server.
[in]portUDP port to send messages to.
Since
2.0.0

Definition at line 200 of file syslog.cpp.

◆ hostname() [1/2]

std::string uuid::syslog::SyslogService::hostname ( ) const

Get local hostname.

Since
2.0.0
Returns
Hostname of this device.

Definition at line 210 of file syslog.cpp.

◆ hostname() [2/2]

void uuid::syslog::SyslogService::hostname ( std::string  hostname)

Set local hostname.

Parameters
[in]hostnameHostname of this device.
Since
2.0.0

Definition at line 214 of file syslog.cpp.

◆ log_level() [1/2]

uuid::log::Level uuid::syslog::SyslogService::log_level ( ) const

Get the current log level.

This only affects newly received log messages, not messages that have already been queued.

Returns
The current log level.
Since
2.0.0

Definition at line 128 of file syslog.cpp.

◆ log_level() [2/2]

void uuid::syslog::SyslogService::log_level ( uuid::log::Level  level)

Set the current log level.

Unless this is the first time the log level is being set, this only affects newly received log messages, not messages that have already been queued.

Parameters
[in]levelMinimum log level that will be sent to the syslog server.
Since
2.0.0

Definition at line 151 of file syslog.cpp.

◆ loop()

void uuid::syslog::SyslogService::loop ( )

Dispatch queued log messages.

Since
1.0.0

Definition at line 271 of file syslog.cpp.

◆ mark_interval() [1/2]

unsigned long uuid::syslog::SyslogService::mark_interval ( ) const

Get mark interval.

Since
2.0.0
Returns
Mark interval in seconds (0 = disable).

Definition at line 222 of file syslog.cpp.

◆ mark_interval() [2/2]

void uuid::syslog::SyslogService::mark_interval ( unsigned long  interval)

Set mark interval.

When no messages have been sent for this period of time, a -- MARK -- message will be generated automatically.

Parameters
[in]intervalMark interval in seconds (0 = disable).
Since
2.0.0

Definition at line 226 of file syslog.cpp.

◆ maximum_log_messages() [1/2]

size_t uuid::syslog::SyslogService::maximum_log_messages ( ) const

Get the maximum number of queued log messages.

Returns
The maximum number of queued log messages.
Since
2.0.0

Definition at line 168 of file syslog.cpp.

◆ maximum_log_messages() [2/2]

void uuid::syslog::SyslogService::maximum_log_messages ( size_t  count)

Set the maximum number of queued log messages.

Defaults to SyslogService::MAX_LOG_MESSAGES.

Since
2.0.0

Definition at line 176 of file syslog.cpp.

◆ operator<<()

void uuid::syslog::SyslogService::operator<< ( std::shared_ptr< uuid::log::Message message)
overridevirtual

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.

Parameters
[in]messageNew log message, shared by all handlers.
Since
1.0.0

Implements uuid::log::Handler.

Definition at line 263 of file syslog.cpp.

◆ remove_queued_messages()

void uuid::syslog::SyslogService::remove_queued_messages ( uuid::log::Level  level)
private

Remove messages that were queued before the log level was set.

Parameters
[in]levelNew log level
Since
1.0.0

Definition at line 132 of file syslog.cpp.

◆ start()

void uuid::syslog::SyslogService::start ( )

Register the log handler with the logging framework.

Since
1.0.0

Definition at line 124 of file syslog.cpp.

◆ transmit()

bool uuid::syslog::SyslogService::transmit ( const QueuedLogMessage message)
private

Attempt to transmit one message to the server.

Parameters
[in]messageLog message to be sent.
Returns
True if the message was successfully set, otherwise false.
Since
1.0.0

Definition at line 472 of file syslog.cpp.

Member Data Documentation

◆ DEFAULT_PORT

constexpr uint16_t uuid::syslog::SyslogService::DEFAULT_PORT = 514
staticconstexpr

Default UDP port to send messages to.

Since
1.0.0

Definition at line 84 of file syslog.h.

◆ host_

IPAddress uuid::syslog::SyslogService::host_
private

Host to send messages to.

Since
1.0.0

Definition at line 296 of file syslog.h.

◆ hostname_

std::string uuid::syslog::SyslogService::hostname_ {'-'}
private

Local hostname.

Since
1.0.0

Definition at line 299 of file syslog.h.

◆ last_message_

uint64_t uuid::syslog::SyslogService::last_message_ = 0
private

Last message/mark time.

Since
2.0.0

Definition at line 307 of file syslog.h.

◆ last_transmit_

uint64_t uuid::syslog::SyslogService::last_transmit_ = 0
private

Last transmit time.

Since
1.0.0

Definition at line 298 of file syslog.h.

◆ level_set_

bool uuid::syslog::SyslogService::level_set_ = false
private

Flag to indicate that the log level has been set at least once.

Since
2.2.0

Definition at line 294 of file syslog.h.

◆ log_message_id_

unsigned long uuid::syslog::SyslogService::log_message_id_ = 0
private

The next identifier to use for queued log messages.

Since
1.0.0

Definition at line 304 of file syslog.h.

◆ log_messages_

std::list<QueuedLogMessage> uuid::syslog::SyslogService::log_messages_
private

Queued log messages, in the order they were received.

Since
1.0.0

Definition at line 305 of file syslog.h.

◆ logger_

uuid::log::Logger uuid::syslog::SyslogService::logger_ {FPSTR(__pstr__logger_name), uuid::log::Facility::SYSLOG}
staticprivate

uuid::log::Logger instance for syslog services.

Since
1.0.0

Definition at line 291 of file syslog.h.

◆ mark_interval_

uint64_t uuid::syslog::SyslogService::mark_interval_ = 0
private

Mark interval in milliseconds.

Since
2.0.0

Definition at line 306 of file syslog.h.

◆ MAX_LOG_MESSAGES

constexpr size_t uuid::syslog::SyslogService::MAX_LOG_MESSAGES = 50
staticconstexpr

Maximum number of log messages to buffer before they are output.

Since
1.0.0

Definition at line 83 of file syslog.h.

◆ maximum_log_messages_

size_t uuid::syslog::SyslogService::maximum_log_messages_ = MAX_LOG_MESSAGES
private

Maximum number of log messages to buffer before they are output.

Since
1.0.0

Definition at line 303 of file syslog.h.

◆ mutex_

std::mutex uuid::syslog::SyslogService::mutex_
mutableprivate

Mutex for queued log messages.

Since
2.2.0

Definition at line 301 of file syslog.h.

◆ port_

uint16_t uuid::syslog::SyslogService::port_ = DEFAULT_PORT
private

Port to send messages to.

Since
1.0.0

Definition at line 297 of file syslog.h.

◆ started_

bool uuid::syslog::SyslogService::started_ = false
private

Flag to indicate that messages have started being transmitted.

Since
1.0.0

Definition at line 293 of file syslog.h.

◆ udp_

WiFiUDP uuid::syslog::SyslogService::udp_
private

UDP client.

Since
1.0.0

Definition at line 295 of file syslog.h.


The documentation for this class was generated from the following files: