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::Signal> stop_signal_;
312 std::unique_ptr<etcpal::Mutex> mutex_;
314 bool initialized_{
false};
319 extern "C" inline void LogCallbackFn(
void* context,
const EtcPalLogStrings* strings)
321 if (context && strings)
329 if (context && timestamp)
331 *timestamp =
static_cast<LogMessageHandler*
>(context)->GetLogTimestamp().get();
337 inline Logger::Logger()
341 log_params_.
log_fn = LogCallbackFn;
343 log_params_.
time_fn = LogTimestampFn;
364 signal_ = std::make_unique<etcpal::Signal>();
365 stop_signal_ = std::make_unique<etcpal::Signal>();
366 mutex_ = std::make_unique<etcpal::Mutex>();
368 if (!signal_ || !stop_signal_ || !mutex_)
374 log_params_.
context = &message_handler;
382 if (!thread_.
SetName(
"EtcPalLoggerThread").
Start(&Logger::LogThreadRun,
this))
384 initialized_ =
false;
399 initialized_ =
false;
400 stop_signal_->Notify();
429 va_start(args, format);
430 LogInternal(pri, format, args);
438 va_start(args, format);
447 va_start(args, format);
456 va_start(args, format);
465 va_start(args, format);
474 va_start(args, format);
483 va_start(args, format);
492 va_start(args, format);
501 va_start(args, format);
509 return dispatch_policy_;
521 return log_params_.
action;
565 dispatch_policy_ = new_policy;
585 log_params_.action = log_action;
592 log_params_.syslog_params.facility = facility;
607 SetSyslogHostname(hostname.c_str());
622 SetSyslogAppName(app_name.c_str());
637 SetSyslogProcId(proc_id.c_str());
646 thread_.SetPriority(priority);
655 thread_.SetStackSize(stack_size);
664 thread_.SetName(name);
673 thread_.SetName(name);
682 thread_.SetPlatformData(platform_data);
689 ETCPAL_MSVC_NO_DEP_WRN sprintf(log_params_.syslog_params.procid,
"%d", proc_id);
695 inline void Logger::LogInternal(
int pri,
const char* format, std::va_list args)
708 msg_q_.back().pri = pri;
716 inline void Logger::LogThreadRun()
718 while (!stop_signal_->TryWait())
727 inline void Logger::EmptyLogQueue()
729 std::queue<LogMessage> to_log;
735 while (!to_log.empty())
737 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:590
void Critical(const char *format,...)
Log a message at critical priority.
Definition: log.h:480
void Info(const char *format,...)
Log a message at informational priority.
Definition: log.h:444
Logger & SetThreadPlatformData(void *platform_data) noexcept
Set the platform-specific parameter data of the log dispatch thread.
Definition: log.h:680
bool CanLog(int pri) const noexcept
Determine whether a priority level can be logged using the mask given via SetLogMask().
Definition: log.h:414
LogDispatchPolicy dispatch_policy() const noexcept
Get the current log dispatch policy.
Definition: log.h:507
Logger & SetLogAction(int log_action) noexcept
Set the types of log messages to create and dispatch to the LogMessageHandler.
Definition: log.h:583
void Error(const char *format,...)
Log a message at error priority.
Definition: log.h:471
int log_action() const noexcept
Get the current log action.
Definition: log.h:519
void Emergency(const char *format,...)
Log a message at emergency priority.
Definition: log.h:498
Logger & SetThreadStackSize(unsigned int stack_size) noexcept
Set the stack size of the log dispatch thread.
Definition: log.h:653
void Alert(const char *format,...)
Log a message at alert priority.
Definition: log.h:489
bool Startup(LogMessageHandler &message_handler)
Start logging.
Definition: log.h:353
int syslog_facility() const noexcept
Get the current Syslog facility value.
Definition: log.h:525
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
const char * syslog_hostname() const noexcept
Get the current Syslog HOSTNAME.
Definition: log.h:531
void Log(int pri, const char *format,...)
Log a message.
Definition: log.h:426
const char * syslog_procid() const noexcept
Get the current Syslog PROCID.
Definition: log.h:543
void Shutdown()
Stop logging.
Definition: log.h:395
const char * syslog_app_name() const noexcept
Get the current Syslog APP-NAME.
Definition: log.h:537
int log_mask() const noexcept
Get the current log mask.
Definition: log.h:513
Logger & SetThreadName(const char *name) noexcept
Set the name of the log dispatch thread.
Definition: log.h:662
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
Logger & SetThreadPriority(unsigned int priority) noexcept
Set the priority of the log dispatch thread.
Definition: log.h:644
Logger & SetDispatchPolicy(LogDispatchPolicy new_policy) noexcept
Change the dispatch policy of this logger.
Definition: log.h:563
void Notice(const char *format,...)
Log a message at notice priority.
Definition: log.h:453
Lock guard around a mutex.
Definition: mutex.h:173
A thread class, modeled after std::thread.
Definition: thread.h:146
Error Start(Function &&func, Args &&... args)
Associate this thread object with a new thread of execution.
Definition: thread.h:428
Thread & SetName(const char *name) noexcept
Set the name of this thread.
Definition: thread.h:358
Error Join(int timeout_ms=ETCPAL_WAIT_FOREVER) noexcept
Wait for the thread to finish execution.
Definition: thread.h:462
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:189
#define ETCPAL_LOG_PROCID_MAX_LEN
Max length of the procid param.
Definition: log.h:209
#define ETCPAL_LOG_CRIT
Critical conditions.
Definition: log.h:188
#define ETCPAL_RAW_LOG_MSG_MAX_LEN
Max length of a log message string passed to etcpal_log() or etcpal_vlog().
Definition: log.h:212
#define ETCPAL_LOG_APP_NAME_MAX_LEN
Max length of the app_name param.
Definition: log.h:208
#define ETCPAL_LOG_NOTICE
Normal but significant condition.
Definition: log.h:191
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:478
#define ETCPAL_LOG_CREATE_HUMAN_READABLE
Create a log string with a human-readable prefix including timestamp and severity.
Definition: log.h:351
#define ETCPAL_LOG_DEBUG
Debug-level messages.
Definition: log.h:193
bool etcpal_validate_log_timestamp(const EtcPalLogTimestamp *timestamp)
Determine whether the given EtcPalLogTimestamp is valid.
Definition: log.c:417
bool etcpal_validate_log_params(EtcPalLogParams *params)
Ensure that the given EtcPalLogParams are valid.
Definition: log.c:395
#define ETCPAL_LOG_INFO
Informational.
Definition: log.h:192
#define ETCPAL_LOG_EMERG
System is unusable.
Definition: log.h:186
void etcpal_log(const EtcPalLogParams *params, int pri, const char *format,...)
Log a message.
Definition: log.c:456
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:439
#define ETCPAL_LOG_WARNING
Warning conditions.
Definition: log.h:190
#define ETCPAL_LOG_UPTO(pri)
Create a priority mask for all priorities through pri.
Definition: log.h:205
#define ETCPAL_LOG_ALERT
Action must be taken immediately.
Definition: log.h:187
#define ETCPAL_LOG_HOSTNAME_MAX_LEN
Max length of the hostname param.
Definition: log.h:207
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
A set of parameters used for the etcpal_*log() functions.
Definition: log.h:386
EtcPalSyslogParams syslog_params
The syslog header parameters.
Definition: log.h:392
EtcPalLogCallback log_fn
A callback function for the finished log string(s).
Definition: log.h:390
int action
What should be done when etcpal_log() or etcpal_vlog() is called.
Definition: log.h:388
int log_mask
A mask value that determines which priority messages can be logged.
Definition: log.h:394
EtcPalLogTimeFn time_fn
A callback function for the etcpal_log() and etcpal_vlog() functions to obtain the time from the appl...
Definition: log.h:399
void * context
Application context that will be passed back with the log callback function.
Definition: log.h:401
The set of log strings passed with a call to an etcpal_log_callback function.
Definition: log.h:299
A set of parameters which represent the current local time with millisecond resolution.
Definition: log.h:283
int facility
Syslog Facility; see RFC 5424 § 6.2.1.
Definition: log.h:361
char app_name[ETCPAL_LOG_APP_NAME_MAX_LEN]
Syslog APP-NAME; see RFC 5424 § 6.2.5.
Definition: log.h:365
char procid[ETCPAL_LOG_PROCID_MAX_LEN]
Syslog PROCID; see RFC 5424 § 6.2.6.
Definition: log.h:367
char hostname[ETCPAL_LOG_HOSTNAME_MAX_LEN]
Syslog HOSTNAME; see RFC 5424 § 6.2.4.
Definition: log.h:363