EtcPal  HEAD (unstable)
ETC Platform Abstraction Layer (EtcPal)
View other versions:
log (Logging)

Overview

C++ utilities for the log (Logging) module.

This module contains a class Logger which is used to gather log messages dispatched from different portions of an application into a single stream with a unified format. It wraps the C logging module, which provides the core of this functionality, in a convenience layer which can also spawn a background thread to handle large log volumes. See the documentation for the C module for more detailed information on the log format.

Generally a single instance per application is sufficient:

// This class handles the log messages gathered by the Logger class.
class MyLogHandler : public etcpal::LogMessageHandler
{
etcpal::LogTimestamp GetLogTimestamp() override
{
// Grab a timestamp from the local system and return it
// This function can also be omitted if you can't easily obtain system time. No timestamps
// will be prepended to log messages in that case.
}
void HandleLogMessage(const EtcPalLogStrings& strings) override
{
// "strings" will contain strings pertaining to what action was requested by
// Logger::SetLogAction().
// Dispatch log messages in an application-defined way.
// If human-readable logging is enabled, maybe...
printf("%s\n", strings.human_readable);
// If syslog is enabled, maybe...
etcpal_sendto(my_syslog_socket, strings.syslog, strlen(strings.syslog), 0, &my_syslog_server);
}
};
int main()
{
MyLogHandler log_handler;
// Set parameters and start logging using the method-chaining setters:
.SetLogAction(kEtcPalLogCreateBoth)
.SetSyslogHostname(my_hostname)
.SetSyslogAppName("My App")
.Startup(log_handler);
// By default, a background thread is started to dispatch the log messages.
logger.Log(ETCPAL_LOG_INFO, "Starting up!");
// Use the shortcuts to log at a specific priority level:
logger.Warning("Uh oh...");
logger.Debug("We've reached line 250");
// All log functions accept printf-style format specifiers and arguments
logger.Info("This module has gone %d days without an accident", -1);
// Use the log_params() getter to add logging output from other libraries with C APIs that
// support it.
somelib_init(&logger.log_params());
// Shut down the logger when done to join the thread, if applicable.
logger.Shutdown();
return 0;
}
An interface which handles log messages.
Definition: log.h:199
An object representing the current local time with millisecond resolution for logging purposes.
Definition: log.h:123
A class for dispatching log messages.
Definition: log.h:237
Logger & SetSyslogFacility(int facility) noexcept
Set the Syslog facility value; see RFC 5424 § 6.2.1.
Definition: log.h:590
void Info(const char *format,...)
Log a message at informational priority.
Definition: log.h:444
Logger & SetLogAction(int log_action) noexcept
Set the types of log messages to create and dispatch to the LogMessageHandler.
Definition: log.h:583
bool Startup(LogMessageHandler &message_handler)
Start logging.
Definition: log.h:353
Logger & SetSyslogHostname(const char *hostname) noexcept
Set the Syslog HOSTNAME; see RFC 5424 § 6.2.4.
Definition: log.h:597
void Warning(const char *format,...)
Log a message at warning priority.
Definition: log.h:462
Logger & SetSyslogAppName(const char *app_name) noexcept
Set the Syslog APP-NAME; see RFC 5424 § 6.2.5.
Definition: log.h:612
const EtcPalLogParams & log_params() const noexcept
Get the log params used by this logger.
Definition: log.h:553
void Log(int pri, const char *format,...)
Log a message.
Definition: log.h:426
void Shutdown()
Stop logging.
Definition: log.h:395
Logger & SetLogMask(int log_mask) noexcept
Set a new log mask.
Definition: log.h:576
Logger & SetSyslogProcId(const char *proc_id) noexcept
Set the Syslog PROCID; see RFC 5424 § 6.2.6.
Definition: log.h:627
void Debug(const char *format,...)
Log a message at debug priority.
Definition: log.h:435
#define ETCPAL_LOG_LOCAL1
Reserved for local use.
Definition: log.h:165
#define ETCPAL_LOG_INFO
Informational.
Definition: log.h:192
#define ETCPAL_LOG_UPTO(pri)
Create a priority mask for all priorities through pri.
Definition: log.h:205
int etcpal_sendto(etcpal_socket_t id, const void *message, size_t length, int flags, const EtcPalSockAddr *dest_addr)
Send data on a socket.
void etcpal_deinit(etcpal_features_t features)
Deinitialize the EtcPal library.
Definition: common.c:193
etcpal_error_t etcpal_init(etcpal_features_t features)
Initialize the EtcPal library.
Definition: common.c:112
#define ETCPAL_FEATURE_LOGGING
Use the etcpal/log module.
Definition: common.h:140
The set of log strings passed with a call to an etcpal_log_callback function.
Definition: log.h:299
const char * human_readable
Log string formatted for readability per ETC convention.
Definition: log.h:305
const char * syslog
Log string formatted compliant to RFC 5424.
Definition: log.h:301

Data Structures

class  LogTimestamp
 An object representing the current local time with millisecond resolution for logging purposes. More...
 
class  LogMessageHandler
 An interface which handles log messages. More...
 
class  Logger
 A class for dispatching log messages. More...
 

Enumerations

enum class  LogDispatchPolicy { kDirect , kQueued }
 Options for the method by which the Logger dispatches log messages. More...
 

Enumeration Type Documentation

◆ LogDispatchPolicy

enum LogDispatchPolicy
strong

Options for the method by which the Logger dispatches log messages.

Enumerator
kDirect 

Log messages propagate directly from Log() calls to output streams (normally only used for testing)

kQueued 

Log messages are queued and dispatched from another thread (recommended)