]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: make it easier to add lines to the log "init banner"
authorLaine Stump <laine@redhat.com>
Fri, 3 Apr 2026 05:58:13 +0000 (01:58 -0400)
committerLaine Stump <laine@redhat.com>
Thu, 9 Apr 2026 05:12:21 +0000 (01:12 -0400)
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 <laine@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
src/util/virlog.c

index 8d0d926d587245d8b80a582f920b3d964c70f376..3d5bfb529e7e4c5ed7bffba8890d0e49f5d00d45 100644 (file)
@@ -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;
     }