From: Laine Stump <[email protected]>

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 <[email protected]>
Reviewed-by: Peter Krempa <[email protected]>
---
V2: fixed formatting of a function definition noticed by Peter

 src/util/virlog.c | 82 +++++++++++++++++++++++------------------------
 1 file changed, 40 insertions(+), 42 deletions(-)

diff --git a/src/util/virlog.c b/src/util/virlog.c
index 8d0d926d58..cab23f613f 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,18 @@ 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;
     }
 
-- 
2.53.0

Reply via email to