23 #ifndef ETCPAL_CPP_LOG_H_
24 #define ETCPAL_CPP_LOG_H_
32 #include "etcpal/common.h"
33 #include "etcpal/log.h"
131 unsigned int msec = 0,
162 : timestamp_{year, month, day, hour, minute, second, msec, utc_offset}
242 bool CanLog(
int pri)
const noexcept;
243 void Log(
int pri,
const char* format, ...);
247 void Debug(
const char* format, ...);
248 void Info(
const char* format, ...);
249 void Notice(
const char* format, ...);
250 void Warning(
const char* format, ...);
251 void Error(
const char* format, ...);
252 void Critical(
const char* format, ...);
253 void Alert(
const char* format, ...);
285 void LogInternal(
int pri,
const char* format, std::va_list args);
287 void EmptyLogQueue();
301 std::queue<LogMessage> msg_q_;
302 std::unique_ptr<etcpal::Signal> signal_;
303 std::unique_ptr<etcpal::Mutex> mutex_;
305 bool running_{
false};
310 extern "C" inline void LogCallbackFn(
void* context,
const EtcPalLogStrings* strings)
312 if (context && strings)
320 if (context && timestamp)
322 *timestamp =
static_cast<LogMessageHandler*
>(context)->GetLogTimestamp().get();
328 inline Logger::Logger()
332 log_params_.
log_fn = LogCallbackFn;
334 log_params_.
time_fn = LogTimestampFn;
355 log_params_.
context = &message_handler;
363 if (thread_.
SetName(
"EtcPalLoggerThread").
Start(&Logger::LogThreadRun,
this))
417 va_start(args, format);
418 LogInternal(pri, format, args);
426 va_start(args, format);
435 va_start(args, format);
444 va_start(args, format);
453 va_start(args, format);
462 va_start(args, format);
471 va_start(args, format);
480 va_start(args, format);
489 va_start(args, format);
497 return dispatch_policy_;
509 return log_params_.
action;
553 dispatch_policy_ = new_policy;
573 log_params_.action = log_action;
580 log_params_.syslog_params.facility = facility;
595 SetSyslogHostname(hostname.c_str());
610 SetSyslogAppName(app_name.c_str());
625 SetSyslogProcId(proc_id.c_str());
632 ETCPAL_MSVC_NO_DEP_WRN sprintf(log_params_.syslog_params.procid,
"%d", proc_id);
638 inline void Logger::LogInternal(
int pri,
const char* format, std::va_list args)
651 msg_q_.back().pri = pri;
659 inline void Logger::LogThreadRun()
670 inline void Logger::EmptyLogQueue()
672 std::queue<LogMessage> to_log;
678 while (!to_log.empty())
680 etcpal_log(&log_params_, to_log.front().pri,
"%s", to_log.front().buf);
An interface which handles log messages.
Definition: log.h:197
virtual void HandleLogMessage(const EtcPalLogStrings &strings)=0
Define this function to handle log messages and determine what to do with them.
virtual LogTimestamp GetLogTimestamp()
Return a LogTimestamp representing the current local time.
Definition: log.h:216
An object representing the current local time with millisecond resolution for logging purposes.
Definition: log.h:121
constexpr const EtcPalLogTimestamp & get() const noexcept
Get a const reference to the underlying C type.
Definition: log.h:173
bool IsValid() const noexcept
Whether this timestamp represents a valid time.
Definition: log.h:167
LogTimestamp()=default
Construct an invalid timestamp by default.
static LogTimestamp Invalid()
Construct an invalid timestamp.
Definition: log.h:185
A class for dispatching log messages.
Definition: log.h:235
Logger & SetSyslogFacility(int facility) noexcept
Set the Syslog facility value; see RFC 5424 § 6.2.1.
Definition: log.h:578
void Critical(const char *format,...)
Log a message at critical priority.
Definition: log.h:468
void Info(const char *format,...)
Log a message at informational priority.
Definition: log.h:432
bool CanLog(int pri) const noexcept
Determine whether a priority level can be logged using the mask given via SetLogMask().
Definition: log.h:402
LogDispatchPolicy dispatch_policy() const noexcept
Get the current log dispatch policy.
Definition: log.h:495
Logger & SetLogAction(int log_action) noexcept
Set the types of log messages to create and dispatch to the LogMessageHandler.
Definition: log.h:571
void Error(const char *format,...)
Log a message at error priority.
Definition: log.h:459
int log_action() const noexcept
Get the current log action.
Definition: log.h:507
void Emergency(const char *format,...)
Log a message at emergency priority.
Definition: log.h:486
void Alert(const char *format,...)
Log a message at alert priority.
Definition: log.h:477
bool Startup(LogMessageHandler &message_handler)
Start logging.
Definition: log.h:344
int syslog_facility() const noexcept
Get the current Syslog facility value.
Definition: log.h:513
Logger & SetSyslogHostname(const char *hostname) noexcept
Set the Syslog HOSTNAME; see RFC 5424 § 6.2.4.
Definition: log.h:585
void Warning(const char *format,...)
Log a message at warning priority.
Definition: log.h:450
Logger & SetSyslogAppName(const char *app_name) noexcept
Set the Syslog APP-NAME; see RFC 5424 § 6.2.5.
Definition: log.h:600
const EtcPalLogParams & log_params() const noexcept
Get the log params used by this logger.
Definition: log.h:541
const char * syslog_hostname() const noexcept
Get the current Syslog HOSTNAME.
Definition: log.h:519
void Log(int pri, const char *format,...)
Log a message.
Definition: log.h:414
const char * syslog_procid() const noexcept
Get the current Syslog PROCID.
Definition: log.h:531
void Shutdown()
Stop logging.
Definition: log.h:384
const char * syslog_app_name() const noexcept
Get the current Syslog APP-NAME.
Definition: log.h:525
int log_mask() const noexcept
Get the current log mask.
Definition: log.h:501
Logger & SetLogMask(int log_mask) noexcept
Set a new log mask.
Definition: log.h:564
Logger & SetSyslogProcId(const char *proc_id) noexcept
Set the Syslog PROCID; see RFC 5424 § 6.2.6.
Definition: log.h:615
void Debug(const char *format,...)
Log a message at debug priority.
Definition: log.h:423
Logger & SetDispatchPolicy(LogDispatchPolicy new_policy) noexcept
Change the dispatch policy of this logger.
Definition: log.h:551
void Notice(const char *format,...)
Log a message at notice priority.
Definition: log.h:441
Lock guard around a mutex.
Definition: lock.h:590
A wrapper class for the EtcPal mutex type.
Definition: lock.h:89
A wrapper class for the EtcPal signal type.
Definition: lock.h:207
A thread class, modeled after std::thread.
Definition: thread.h:143
Error Start(Function &&func, Args &&... args)
Associate this thread object with a new thread of execution.
Definition: thread.h:384
Thread & SetName(const char *name) noexcept
Set the name of this thread.
Definition: thread.h:328
Error Join(int timeout_ms=ETCPAL_WAIT_FOREVER) noexcept
Wait for the thread to finish execution.
Definition: thread.h:412
Common definitions used by EtcPal C++ wrappers.
C++ wrapper and utilities for etcpal/lock.h.
C++ wrapper and utilities for etcpal/thread.h.
LogDispatchPolicy
Options for the method by which the Logger dispatches log messages.
Definition: log.h:224
@ Queued
Log messages are queued and dispatched from another thread (recommended)
@ Direct
Log messages propagate directly from Log() calls to output streams (normally only used for testing)
#define ETCPAL_CONSTEXPR_14
Stand-in for "constexpr" on entities that can only be defined "constexpr" in C++14 or later.
Definition: common.h:53
#define ETCPAL_CONSTEXPR_14_OR_INLINE
Defined to "constexpr" in C++14 or later, "inline" earlier.
Definition: common.h:54
@ kEtcPalErrOk
The call was successful, no error occurred.
Definition: error.h:51
#define ETCPAL_LOG_ERR
Error conditions.
Definition: log.h:178
#define ETCPAL_LOG_PROCID_MAX_LEN
Max length of the procid param.
Definition: log.h:198
#define ETCPAL_LOG_CRIT
Critical conditions.
Definition: log.h:177
#define ETCPAL_RAW_LOG_MSG_MAX_LEN
Max length of a log message string passed to etcpal_log() or etcpal_vlog().
Definition: log.h:201
#define ETCPAL_LOG_APP_NAME_MAX_LEN
Max length of the app_name param.
Definition: log.h:197
#define ETCPAL_LOG_NOTICE
Normal but significant condition.
Definition: log.h:180
void etcpal_vlog(const EtcPalLogParams *params, int pri, const char *format, va_list args)
Log a message with the list of format arguments already generated.
Definition: log.c:431
#define ETCPAL_LOG_CREATE_HUMAN_READABLE
Create a log string with a human-readable prefix including timestamp and severity.
Definition: log.h:340
#define ETCPAL_LOG_DEBUG
Debug-level messages.
Definition: log.h:182
bool etcpal_validate_log_timestamp(const EtcPalLogTimestamp *timestamp)
Determine whether the given EtcPalLogTimestamp is valid.
Definition: log.c:372
bool etcpal_validate_log_params(EtcPalLogParams *params)
Ensure that the given EtcPalLogParams are valid.
Definition: log.c:350
#define ETCPAL_LOG_INFO
Informational.
Definition: log.h:181
#define ETCPAL_LOG_EMERG
System is unusable.
Definition: log.h:175
void etcpal_log(const EtcPalLogParams *params, int pri, const char *format,...)
Log a message.
Definition: log.c:412
bool etcpal_can_log(const EtcPalLogParams *params, int pri)
Determine whether a priority level can be logged given the mask present in the log params.
Definition: log.c:395
#define ETCPAL_LOG_WARNING
Warning conditions.
Definition: log.h:179
#define ETCPAL_LOG_UPTO(pri)
Create a priority mask for all priorities through pri.
Definition: log.h:194
#define ETCPAL_LOG_ALERT
Action must be taken immediately.
Definition: log.h:176
#define ETCPAL_LOG_HOSTNAME_MAX_LEN
Max length of the hostname param.
Definition: log.h:196
void etcpal_deinit(etcpal_features_t features)
Deinitialize the EtcPal library.
Definition: common.c:136
etcpal_error_t etcpal_init(etcpal_features_t features)
Initialize the EtcPal library.
Definition: common.c:87
#define ETCPAL_FEATURE_LOGGING
Use the etcpal/log module.
Definition: common.h:130
A set of parameters used for the etcpal_*log() functions.
Definition: log.h:371
EtcPalSyslogParams syslog_params
The syslog header parameters.
Definition: log.h:377
EtcPalLogCallback log_fn
A callback function for the finished log string(s).
Definition: log.h:375
int action
What should be done when etcpal_log() or etcpal_vlog() is called.
Definition: log.h:373
int log_mask
A mask value that determines which priority messages can be logged.
Definition: log.h:379
EtcPalLogTimeFn time_fn
A callback function for the etcpal_log() and etcpal_vlog() functions to obtain the time from the appl...
Definition: log.h:384
void * context
Application context that will be passed back with the log callback function.
Definition: log.h:386
The set of log strings passed with a call to an etcpal_log_callback function.
Definition: log.h:288
A set of parameters which represent the current local time with millisecond resolution.
Definition: log.h:272
int facility
Syslog Facility; see RFC 5424 § 6.2.1.
Definition: log.h:349
char app_name[ETCPAL_LOG_APP_NAME_MAX_LEN]
Syslog APP-NAME; see RFC 5424 § 6.2.5.
Definition: log.h:351
char procid[ETCPAL_LOG_PROCID_MAX_LEN]
Syslog PROCID; see RFC 5424 § 6.2.6.
Definition: log.h:352
char hostname[ETCPAL_LOG_HOSTNAME_MAX_LEN]
Syslog HOSTNAME; see RFC 5424 § 6.2.4.
Definition: log.h:350