From b31d104fc09a7241856ff8d226be11ec562beac3 Mon Sep 17 00:00:00 2001 From: Daniel Kurtz Date: Mon, 8 Aug 2011 15:09:46 +0800 Subject: [PATCH] os/log: Add LogVHdrMessageVerb and friends LogVHdrMessageVerb allows a custom header to be inserted in a log message, between the Log system's MessageType string, and a formatted variable message body. The custom header can itself be a formatted variable string. These functions can be used, for example, by driver abstraction layers to format specific driver messages in a standard format, but do it in a way that is efficient, obeys the log-layers verbosity settings, and is safe to use in signal handlers (because they don't call malloc), even for types besides X_NONE. Signed-off-by: Daniel Kurtz Reviewed-by: Peter Hutterer Signed-off-by: Peter Hutterer --- include/os.h | 13 +++++++++++++ os/log.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/include/os.h b/include/os.h index a553f5783..5401ea478 100644 --- a/include/os.h +++ b/include/os.h @@ -525,6 +525,19 @@ extern _X_EXPORT void LogMessageVerb(MessageType type, int verb, const char *for ...) _X_ATTRIBUTE_PRINTF(3,4); extern _X_EXPORT void LogMessage(MessageType type, const char *format, ...) _X_ATTRIBUTE_PRINTF(2,3); + +extern _X_EXPORT void LogVHdrMessageVerb(MessageType type, int verb, + const char *msg_format, va_list msg_args, + const char *hdr_format, va_list hdr_args) + _X_ATTRIBUTE_PRINTF(3,0) _X_ATTRIBUTE_PRINTF(5,0); +extern _X_EXPORT void LogHdrMessageVerb(MessageType type, int verb, + const char *msg_format, va_list msg_args, + const char *hdr_format, ...) + _X_ATTRIBUTE_PRINTF(3,0) _X_ATTRIBUTE_PRINTF(5,6); +extern _X_EXPORT void LogHdrMessage(MessageType type, const char *msg_format, + va_list msg_args, const char *hdr_format, ...) + _X_ATTRIBUTE_PRINTF(2,0) _X_ATTRIBUTE_PRINTF(4,5); + extern _X_EXPORT void FreeAuditTimer(void); extern _X_EXPORT void AuditF(const char *f, ...) _X_ATTRIBUTE_PRINTF(1,2); extern _X_EXPORT void VAuditF(const char *f, va_list args) _X_ATTRIBUTE_PRINTF(1,0); diff --git a/os/log.c b/os/log.c index 4ff018078..2eddf066a 100644 --- a/os/log.c +++ b/os/log.c @@ -411,6 +411,61 @@ LogMessage(MessageType type, const char *format, ...) va_end(ap); } + +void +LogVHdrMessageVerb(MessageType type, int verb, const char *msg_format, + va_list msg_args, const char *hdr_format, va_list hdr_args) +{ + const char *type_str; + char tmpFormat[1024]; + char *tmpFormat_end = &tmpFormat[sizeof(tmpFormat)]; + char *p; + int left; + + type_str = LogMessageTypeVerbString(type, verb); + if (!type_str) + return; + + /* if type_str != "", copy it and ' ' to tmpFormat; set p after ' ' */ + p = tmpFormat; + if (type_str[0] != '\0') + p += snprintf(tmpFormat, sizeof(tmpFormat), "%s ", type_str); + + /* append as much of hdr as fits after type_str (if there was one) */ + left = tmpFormat_end - p; + if (left > 1) + p += vsnprintf(p, left, hdr_format, hdr_args); + + /* append as much of msg_format as will fit after hdr */ + left = tmpFormat_end - p; + if (left > 1) + snprintf(p, left, "%s", msg_format); + + LogVWrite(verb, tmpFormat, msg_args); +} + +void +LogHdrMessageVerb(MessageType type, int verb, const char *msg_format, + va_list msg_args, const char *hdr_format, ...) +{ + va_list hdr_args; + + va_start(hdr_args, hdr_format); + LogVHdrMessageVerb(type, verb, msg_format, msg_args, hdr_format, hdr_args); + va_end(hdr_args); +} + +void +LogHdrMessage(MessageType type, const char *msg_format, va_list msg_args, + const char *hdr_format, ...) +{ + va_list hdr_args; + + va_start(hdr_args, hdr_format); + LogVHdrMessageVerb(type, 1, msg_format, msg_args, hdr_format, hdr_args); + va_end(hdr_args); +} + void AbortServer(void) _X_NORETURN;