23 #ifndef ETCPAL_CPP_LOG_H_
24 #define ETCPAL_CPP_LOG_H_
33 #include "etcpal/common.h"
34 #include "etcpal/log.h"
133 unsigned int msec = 0,
164 : timestamp_{year, month, day, hour, minute, second, msec, utc_offset}
244 bool CanLog(
int pri)
const noexcept;
245 void Log(
int pri,
const char* format, ...);
249 void Debug(
const char* format, ...);
250 void Info(
const char* format, ...);
251 void Notice(
const char* format, ...);
252 void Warning(
const char* format, ...);
253 void Error(
const char* format, ...);
254 void Critical(
const char* format, ...);
255 void Alert(
const char* format, ...);
293 void LogInternal(
int pri,
const char* format, std::va_list args);
295 void EmptyLogQueue();
303 std::array<char, ETCPAL_RAW_LOG_MSG_MAX_LEN> buf;
309 std::queue<LogMessage> msg_q_;
310 std::unique_ptr<etcpal::Signal> signal_;
311 std::unique_ptr<etcpal::Mutex> mutex_;
313 bool running_{
false};
318 extern "C" inline void LogCallbackFn(
void* context,
const EtcPalLogStrings* strings)
320 if (context && strings)
328 if (context && timestamp)
330 *timestamp =
static_cast<LogMessageHandler*
>(context)->GetLogTimestamp().get();
336 inline Logger::Logger()
340 log_params_.
log_fn = LogCallbackFn;
342 log_params_.
time_fn = LogTimestampFn;
363 log_params_.
context = &message_handler;
376 if (!thread_.
SetName(
"EtcPalLoggerThread").
Start(&Logger::LogThreadRun,
this))
421 va_start(args, format);
422 LogInternal(pri, format, args);
430 va_start(args, format);
439 va_start(args, format);
448 va_start(args, format);
457 va_start(args, format);
466 va_start(args, format);
475 va_start(args, format);
484 va_start(args, format);
493 va_start(args, format);
501 return dispatch_policy_;
513 return log_params_.
action;
557 dispatch_policy_ = new_policy;
577 log_params_.action = log_action;
584 log_params_.syslog_params.facility = facility;
599 SetSyslogHostname(hostname.c_str());
614 SetSyslogAppName(app_name.c_str());
629 SetSyslogProcId(proc_id.c_str());
638 thread_.SetPriority(priority);
647 thread_.SetStackSize(stack_size);
656 thread_.SetName(name);
665 thread_.SetName(name);
674 thread_.SetPlatformData(platform_data);
681 ETCPAL_MSVC_NO_DEP_WRN sprintf(log_params_.syslog_params.procid,
"%d", proc_id);
687 inline void Logger::LogInternal(
int pri,
const char* format, std::va_list args)
700 msg_q_.back().pri = pri;
708 inline void Logger::LogThreadRun()
719 inline void Logger::EmptyLogQueue()
721 std::queue<LogMessage> to_log;
727 while (!to_log.empty())
729 etcpal_log(&log_params_, to_log.front().pri,
"%s", to_log.front().buf.data());
An interface which handles log messages.
Definition: log.h:199
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:218
An object representing the current local time with millisecond resolution for logging purposes.
Definition: log.h:123
constexpr const EtcPalLogTimestamp & get() const noexcept
Get a const reference to the underlying C type.
Definition: log.h:175
bool IsValid() const noexcept
Whether this timestamp represents a valid time.
Definition: log.h:169
LogTimestamp()=default
Construct an invalid timestamp by default.
static LogTimestamp Invalid()
Construct an invalid timestamp.
Definition: log.h:187
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:582
void Critical(const char *format,...)
Log a message at critical priority.
Definition: log.h:472
void Info(const char *format,...)
Log a message at informational priority.
Definition: log.h:436
Logger & SetThreadPlatformData(void *platform_data) noexcept
Set the platform-specific parameter data of the log dispatch thread.
Definition: log.h:672
bool CanLog(int pri) const noexcept
Determine whether a priority level can be logged using the mask given via SetLogMask().
Definition: log.h:406
LogDispatchPolicy dispatch_policy() const noexcept
Get the current log dispatch policy.
Definition: log.h:499
Logger & SetLogAction(int log_action) noexcept
Set the types of log messages to create and dispatch to the LogMessageHandler.
Definition: log.h:575
void Error(const char *format,...)
Log a message at error priority.
Definition: log.h:463
int log_action() const noexcept
Get the current log action.
Definition: log.h:511
void Emergency(const char *format,...)
Log a message at emergency priority.
Definition: log.h:490
Logger & SetThreadStackSize(unsigned int stack_size) noexcept
Set the stack size of the log dispatch thread.
Definition: log.h:645
void Alert(const char *format,...)
Log a message at alert priority.
Definition: log.h:481
bool Startup(LogMessageHandler &message_handler)
Start logging.
Definition: log.h:352
int syslog_facility() const noexcept
Get the current Syslog facility value.
Definition: log.h:517
Logger & SetSyslogHostname(const char *hostname) noexcept
Set the Syslog HOSTNAME; see RFC 5424 § 6.2.4.
Definition: log.h:589
void Warning(const char *format,...)
Log a message at warning priority.
Definition: log.h:454
Logger & SetSyslogAppName(const char *app_name) noexcept
Set the Syslog APP-NAME; see RFC 5424 § 6.2.5.
Definition: log.h:604
const EtcPalLogParams & log_params() const noexcept
Get the log params used by this logger.
Definition: log.h:545
const char * syslog_hostname() const noexcept
Get the current Syslog HOSTNAME.
Definition: log.h:523
void Log(int pri, const char *format,...)
Log a message.
Definition: log.h:418
const char * syslog_procid() const noexcept
Get the current Syslog PROCID.
Definition: log.h:535
void Shutdown()
Stop logging.
Definition: log.h:388
const char * syslog_app_name() const noexcept
Get the current Syslog APP-NAME.
Definition: log.h:529
int log_mask() const noexcept
Get the current log mask.
Definition: log.h:505
Logger & SetThreadName(const char *name) noexcept
Set the name of the log dispatch thread.
Definition: log.h:654
Logger & SetLogMask(int log_mask) noexcept
Set a new log mask.
Definition: log.h:568
Logger & SetSyslogProcId(const char *proc_id) noexcept
Set the Syslog PROCID; see RFC 5424 § 6.2.6.
Definition: log.h:619
void Debug(const char *format,...)
Log a message at debug priority.
Definition: log.h:427
Logger & SetThreadPriority(unsigned int priority) noexcept
Set the priority of the log dispatch thread.
Definition: log.h:636
Logger & SetDispatchPolicy(LogDispatchPolicy new_policy) noexcept
Change the dispatch policy of this logger.
Definition: log.h:555
void Notice(const char *format,...)
Log a message at notice priority.
Definition: log.h:445
Lock guard around a mutex.
Definition: mutex.h:173
A wrapper class for the EtcPal mutex type.
Definition: mutex.h:88
A wrapper class for the EtcPal signal type.
Definition: signal.h:89
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:425
Thread & SetName(const char *name) noexcept
Set the name of this thread.
Definition: thread.h:355
Error Join(int timeout_ms=ETCPAL_WAIT_FOREVER) noexcept
Wait for the thread to finish execution.
Definition: thread.h:459
Common definitions used by EtcPal C++ wrappers.
C++ wrapper and utilities for etcpal/mutex.h.
C++ wrapper and utilities for etcpal/signal.h.
C++ wrapper and utilities for etcpal/thread.h.
LogDispatchPolicy
Options for the method by which the Logger dispatches log messages.
Definition: log.h:226
@ kQueued
Log messages are queued and dispatched from another thread (recommended)
@ kDirect
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:424
#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:369
bool etcpal_validate_log_params(EtcPalLogParams *params)
Ensure that the given EtcPalLogParams are valid.
Definition: log.c:347
#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:405
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:388
#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:375
EtcPalSyslogParams syslog_params
The syslog header parameters.
Definition: log.h:381
EtcPalLogCallback log_fn
A callback function for the finished log string(s).
Definition: log.h:379
int action
What should be done when etcpal_log() or etcpal_vlog() is called.
Definition: log.h:377
int log_mask
A mask value that determines which priority messages can be logged.
Definition: log.h:383
EtcPalLogTimeFn time_fn
A callback function for the etcpal_log() and etcpal_vlog() functions to obtain the time from the appl...
Definition: log.h:388
void * context
Application context that will be passed back with the log callback function.
Definition: log.h:390
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:350
char app_name[ETCPAL_LOG_APP_NAME_MAX_LEN]
Syslog APP-NAME; see RFC 5424 § 6.2.5.
Definition: log.h:354
char procid[ETCPAL_LOG_PROCID_MAX_LEN]
Syslog PROCID; see RFC 5424 § 6.2.6.
Definition: log.h:356
char hostname[ETCPAL_LOG_HOSTNAME_MAX_LEN]
Syslog HOSTNAME; see RFC 5424 § 6.2.4.
Definition: log.h:352