From: Laine Stump Date: Fri, 3 Apr 2026 05:58:13 +0000 (-0400) Subject: util: make it easier to add lines to the log "init banner" X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=481a832ef03e8a823dee4df0bf14c86552d17024;p=thirdparty%2Flibvirt.git util: make it easier to add lines to the log "init banner" The same thing happens for each line of the log banner: 1) A helper function is called that a) creates a "raw" string (just the desired info, e.g. version string) and b) calls virLogFormatString() to create a "cooked" version of the string (containing thread-id and log priority) 2) the outputFunc for the target is called with strings (a) and (b) By making a helper that does (1b) & (2), we can further reduce the amount of redundant code that needs to be written to add another line to the banner - now all we need to do is: 1) create the raw string 2) call the helper, sending it the raw string Signed-off-by: Laine Stump Reviewed-by: Peter Krempa --- diff --git a/src/util/virlog.c b/src/util/virlog.c index 8d0d926d58..3d5bfb529e 100644 --- a/src/util/virlog.c +++ b/src/util/virlog.c @@ -433,30 +433,6 @@ virLogFormatString(char **msg, } -static void -virLogVersionString(const char **rawmsg, - char **msg) -{ - *rawmsg = VIR_LOG_VERSION_STRING; - virLogFormatString(msg, 0, NULL, VIR_LOG_INFO, VIR_LOG_VERSION_STRING); -} - -/* Similar to virGetHostname() but avoids use of error - * reporting APIs or logging APIs, to prevent recursion - */ -static void -virLogHostnameString(char **rawmsg, - char **msg) -{ - char *hoststr; - - hoststr = g_strdup_printf("hostname: %s", g_get_host_name()); - - virLogFormatString(msg, 0, NULL, VIR_LOG_INFO, hoststr); - *rawmsg = hoststr; -} - - static void virLogSourceUpdate(virLogSource *source) { @@ -479,6 +455,34 @@ virLogSourceUpdate(virLogSource *source) } +/** + * virLogOneInitMsg: + * + * @str: the "raw" form of the string that's going to be logged + * + * (the args are all described in the caller - virLogToOneTarget() + * @timestamp,@outputFunc, @data + * + * send one "init message" (the lines that are at the beginning of the + * log when a new daemon starts) to one target. This just creates the + * "fancy" version of the string with thread-id and priority, and + * sends that, along with the "raw" version of the string, to the log + * target at INFO level. + */ +static void +virLogOneInitMsg(const char *timestamp, + const char *str, + virLogOutputFunc outputFunc, + void *data) +{ + g_autofree char *msg = NULL; + + virLogFormatString(&msg, 0, NULL, VIR_LOG_INFO, str); + outputFunc(&virLogSelf, VIR_LOG_INFO, __FILE__, __LINE__, __func__, + timestamp, NULL, str, msg, data); +} + + /** * virLogToOneTarget: * @@ -522,24 +526,17 @@ virLogToOneTarget(virLogSource *source, bool *needInit) { if (*needInit) { - const char *rawinitmsg; - char *hoststr = NULL; - char *initmsg = NULL; - - virLogVersionString(&rawinitmsg, &initmsg); - outputFunc(&virLogSelf, VIR_LOG_INFO, - __FILE__, __LINE__, __func__, - timestamp, NULL, rawinitmsg, initmsg, - data); - VIR_FREE(initmsg); - - virLogHostnameString(&hoststr, &initmsg); - outputFunc(&virLogSelf, VIR_LOG_INFO, - __FILE__, __LINE__, __func__, - timestamp, NULL, hoststr, initmsg, - data); - VIR_FREE(hoststr); - VIR_FREE(initmsg); + g_autofree char *hoststr = NULL; + + /* put some useful info at the top of the log. Avoid calling + * any function that might end up reporting an error or + * otherwise logging something, to prevent recursion. + */ + virLogOneInitMsg(timestamp, VIR_LOG_VERSION_STRING, outputFunc, data); + + hoststr = g_strdup_printf("hostname: %s", g_get_host_name()); + virLogOneInitMsg(timestamp, hoststr, outputFunc, data); + *needInit = false; }