summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Kurtz <djkurtz@chromium.org>2011-08-08 15:09:46 +0800
committerPeter Hutterer <peter.hutterer@who-t.net>2011-08-22 15:49:18 +1000
commitb31d104fc09a7241856ff8d226be11ec562beac3 (patch)
tree9ed507b828ec8d8dc7c813b256846c8a85612135
parentb82f934db661c59d7397ce180d08cf8f8f7118b1 (diff)
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 <djkurtz@chromium.org> Reviewed-by: Peter Hutterer <peter.hutterer@who-t.net> Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
-rw-r--r--include/os.h13
-rw-r--r--os/log.c55
2 files changed, 68 insertions, 0 deletions
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;