Skip to contentSkip to conversation

qga: replace slog() with standard GLib logging

PATCH New Elizabeth Ashurov 1 participant 2 messages Mar 25

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 qga: replace slog() with standard GLib logging

Replace qga-specific slog() calls with standard GLib logging APIs. Use g_info() for command execution messages and g_warning() for non-fatal failures, and remove the use of "syslog" as a log domain.

Rework ga_log() to route messages to the log file and syslog/Event Viewer, filtering by log level. Debug messages are excluded from syslog even with --debug.

- Default log level includes ERROR, CRITICAL, and WARNING
- -v/--verbose enables MESSAGE and INFO
- --debug enables all levels including DEBUG

---
 qga/commands-linux.c   |  6 ++---
 qga/commands-posix.c   | 26 +++++++++---------
 qga/commands-win32.c   | 60 +++++++++++++++++++++---------------------
 qga/commands.c         | 29 ++++++--------------
 qga/guest-agent-core.h |  1 -
 qga/main.c             | 39 +++++++++++++++++++--------
 6 files changed, 82 insertions(+), 79 deletions(-)
qga/commands-linux.c +3 -3
index 378f4d080c..a722de2e6a 100644
@@ -44,7 +44,7 @@ static int dev_major_minor(const char *devpath,
     *devminor = 0;
 
     if (stat(devpath, &st) < 0) {
-        slog("failed to stat device file '%s': %s", devpath, strerror(errno));
+        g_warning("failed to stat device file '%s': %s", devpath, strerror(errno));
         return -1;
     }
     if (S_ISDIR(st.st_mode)) {
@@ -1158,7 +1158,7 @@ qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp)
     int fd;
     struct fstrim_range r;
 
-    slog("guest-fstrim called");
+    g_info("guest-fstrim called");
 
     QTAILQ_INIT(&mounts);
     if (!build_fs_mount_list(&mounts, errp)) {
@@ -2072,7 +2072,7 @@ GuestCpuStatsList *qmp_guest_get_cpustats(Error **errp)
         }
 
         if (i < 5) {
-            slog("Parsing cpu stat from %s failed, see \"man proc\"", cpustats);
+            g_warning("Parsing cpu stat from %s failed, see \"man proc\"", cpustats);
             break;
         }
 
qga/commands-posix.c +13 -13
index 837be51c40..96939a6f36 100644
@@ -240,7 +240,7 @@ void qmp_guest_shutdown(const char *mode, Error **errp)
     const char *reboot_flag = "-r";
 #endif
 
-    slog("guest-shutdown called, mode: %s", mode);
+    g_info("guest-shutdown called, mode: %s", mode);
     if (!mode || strcmp(mode, "powerdown") == 0) {
         if (access(POWEROFF_CMD_PATH, X_OK) == 0) {
             shutdown_cmd = POWEROFF_CMD_PATH;
@@ -519,7 +519,7 @@ int64_t qmp_guest_file_open(const char *path, const char *mode,
     if (!mode) {
         mode = "r";
     }
-    slog("guest-file-open called, filepath: %s, mode: %s", path, mode);
+    g_info("guest-file-open called, filepath: %s, mode: %s", path, mode);
     fh = safe_open_or_create(path, mode, &local_err);
     if (local_err != NULL) {
         error_propagate(errp, local_err);
@@ -540,7 +540,7 @@ int64_t qmp_guest_file_open(const char *path, const char *mode,
         return -1;
     }
 
-    slog("guest-file-open, handle: %" PRId64, handle);
+    g_info("guest-file-open, handle: %" PRId64, handle);
     return handle;
 }
 
@@ -549,7 +549,7 @@ void qmp_guest_file_close(int64_t handle, Error **errp)
     GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
     int ret;
 
-    slog("guest-file-close called, handle: %" PRId64, handle);
+    g_info("guest-file-close called, handle: %" PRId64, handle);
     if (!gfh) {
         return;
     }
@@ -645,7 +645,7 @@ GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
     write_count = fwrite(buf, 1, count, fh);
     if (ferror(fh)) {
         error_setg_errno(errp, errno, "failed to write to file");
-        slog("guest-file-write failed, handle: %" PRId64, handle);
+        g_warning("guest-file-write failed, handle: %" PRId64, handle);
     } else {
         write_data = g_new0(GuestFileWrite, 1);
         write_data->count = write_count;
@@ -760,7 +760,7 @@ static void execute_fsfreeze_hook(FsfreezeHookArg arg, Error **errp)
 
     const char *argv[] = {hook, arg_str, NULL};
 
-    slog("executing fsfreeze hook with arg '%s'", arg_str);
+    g_info("executing fsfreeze hook with arg '%s'", arg_str);
     ga_run_command(argv, NULL, "execute fsfreeze hook", &local_err);
     if (local_err) {
         error_propagate(errp, local_err);
@@ -793,7 +793,7 @@ int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints,
     FsMountList mounts;
     Error *local_err = NULL;
 
-    slog("guest-fsfreeze called");
+    g_info("guest-fsfreeze called");
 
     execute_fsfreeze_hook(FSFREEZE_HOOK_FREEZE, &local_err);
     if (local_err) {
@@ -833,7 +833,7 @@ int64_t qmp_guest_fsfreeze_thaw(Error **errp)
 
     if (ret >= 0) {
         ga_unset_frozen(ga_state);
-        slog("guest-fsthaw called");
+        g_info("guest-fsthaw called");
         execute_fsfreeze_hook(FSFREEZE_HOOK_THAW, errp);
     } else {
         ret = 0;
@@ -849,8 +849,8 @@ static void guest_fsfreeze_cleanup(void)
     if (ga_is_frozen(ga_state) == GUEST_FSFREEZE_STATUS_FROZEN) {
         qmp_guest_fsfreeze_thaw(&err);
         if (err) {
-            slog("failed to clean up frozen filesystems: %s",
-                 error_get_pretty(err));
+            g_warning("failed to clean up frozen filesystems: %s",
+                      error_get_pretty(err));
             error_free(err);
         }
     }
@@ -1282,19 +1282,19 @@ static GKeyFile *ga_parse_osrelease(const char *fname)
     const char *group = "[os-release]\n";
 
     if (!g_file_get_contents(fname, &content, NULL, &err)) {
-        slog("failed to read '%s', error: %s", fname, err->message);
+        g_warning("failed to read '%s', error: %s", fname, err->message);
         goto fail;
     }
 
     if (!g_utf8_validate(content, -1, NULL)) {
-        slog("file is not utf-8 encoded: %s", fname);
+        g_warning("file is not utf-8 encoded: %s", fname);
         goto fail;
     }
     content2 = g_strdup_printf("%s%s", group, content);
 
     if (!g_key_file_load_from_data(keys, content2, -1, G_KEY_FILE_NONE,
                                    &err)) {
-        slog("failed to parse file '%s', error: %s", fname, err->message);
+        g_warning("failed to parse file '%s', error: %s", fname, err->message);
         goto fail;
     }
 
qga/commands-win32.c +30 -30

197 lines not on this page · raw message

qga/commands.c +8 -21
index 5f20af25d3..55edd9fd4c 100644
@@ -30,19 +30,6 @@
  */
 #define GUEST_FILE_READ_COUNT_MAX (48 * MiB)
 
-/* Note: in some situations, like with the fsfreeze, logging may be
- * temporarily disabled. if it is necessary that a command be able
- * to log for accounting purposes, check ga_logging_enabled() beforehand.
- */
-void slog(const gchar *fmt, ...)
-{
-    va_list ap;
-
-    va_start(ap, fmt);
-    g_logv("syslog", G_LOG_LEVEL_INFO, fmt, ap);
-    va_end(ap);
-}
-
 int64_t qmp_guest_sync_delimited(int64_t id, Error **errp)
 {
     ga_set_response_delimited(ga_state);
@@ -56,7 +43,7 @@ int64_t qmp_guest_sync(int64_t id, Error **errp)
 
 void qmp_guest_ping(Error **errp)
 {
-    slog("guest-ping called");
+    g_info("guest-ping called");
 }
 
 static void qmp_command_info(const QmpCommand *cmd, void *opaque)
@@ -149,7 +136,7 @@ GuestExecStatus *qmp_guest_exec_status(int64_t pid, Error **errp)
     GuestExecInfo *gei;
     GuestExecStatus *ges;
 
-    slog("guest-exec-status called, pid: %u", (uint32_t)pid);
+    g_info("guest-exec-status called, pid: %u", (uint32_t)pid);
 
     gei = guest_exec_info_find(pid);
     if (gei == NULL) {
@@ -251,7 +238,7 @@ static char **guest_exec_get_args(const strList *entry, bool log)
     args[i] = NULL;
 
     if (log) {
-        slog("guest-exec called: \"%s\"", str);
+        g_info("guest-exec called: \"%s\"", str);
     }
     g_free(str);
 
@@ -285,8 +272,8 @@ static void guest_exec_task_setup(gpointer data)
          * inside the parent, not the child.
          */
         if (dup2(STDOUT_FILENO, STDERR_FILENO) != 0) {
-            slog("dup2() failed to merge stderr into stdout: %s",
-                 strerror(errno));
+            g_warning("dup2() failed to merge stderr into stdout: %s",
+                      strerror(errno));
         }
     }
 
@@ -295,8 +282,8 @@ static void guest_exec_task_setup(gpointer data)
     sigact.sa_handler = SIG_DFL;
 
     if (sigaction(SIGPIPE, &sigact, NULL) != 0) {
-        slog("sigaction() failed to reset child process's SIGPIPE: %s",
-             strerror(errno));
+        g_warning("sigaction() failed to reset child process's SIGPIPE: %s",
+                  strerror(errno));
     }
 #endif
 }
@@ -626,7 +613,7 @@ GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
 
     read_data = guest_file_read_unsafe(gfh, count, errp);
     if (!read_data) {
-        slog("guest-file-write failed, handle: %" PRId64, handle);
+        g_warning("guest-file-read failed, handle: %" PRId64, handle);
     }
 
     return read_data;
qga/guest-agent-core.h +0 -1
index d9f3922adf..0f2d1b3415 100644
@@ -40,7 +40,6 @@ void ga_command_state_free(GACommandState *cs);
 bool ga_logging_enabled(GAState *s);
 void ga_disable_logging(GAState *s);
 void ga_enable_logging(GAState *s);
-void G_GNUC_PRINTF(1, 2) slog(const gchar *fmt, ...);
 void ga_set_response_delimited(GAState *s);
 bool ga_is_frozen(GAState *s);
 void ga_set_frozen(GAState *s);
qga/main.c +28 -11
index fd19c7037d..40b92ec686 100644
@@ -286,7 +286,8 @@ QEMU_COPYRIGHT "\n"
 #endif
 "  -t, --statedir    specify dir to store state information (absolute paths\n"
 "                    only, default is %s)\n"
-"  -v, --verbose     log extra debugging information\n"
+"  -v, --verbose     enable verbose logging (info and above)\n"
+"  --debug           enable debug logging (all messages)\n"
 "  -V, --version     print version information and exit\n"
 "  -d, --daemonize   become a daemon\n"
 #ifdef _WIN32
@@ -391,18 +392,24 @@ static void ga_log(const gchar *domain, GLogLevelFlags level,
     }
 
     level &= G_LOG_LEVEL_MASK;
-    if (g_strcmp0(domain, "syslog") == 0) {
+    if (!(level & s->log_level)) {
+        return;
+    }
+
+    if (s->log_file) {
+        g_autoptr(GDateTime) now = g_date_time_new_now_utc();
+        g_autofree char *nowstr = g_date_time_format(now, "%s.%f");
+        fprintf(s->log_file, "%s: %s: %s\n", nowstr, level_str, msg);
+        fflush(s->log_file);
+    }
+
+    if (level & ~G_LOG_LEVEL_DEBUG) {
 #ifndef _WIN32
         syslog(glib_log_level_to_system(level), "%s: %s", level_str, msg);
 #else
         ReportEvent(s->event_log, glib_log_level_to_system(level),
                     0, 1, NULL, 1, 0, &msg, NULL);
 #endif
-    } else if (level & s->log_level) {
-        g_autoptr(GDateTime) now = g_date_time_new_now_utc();
-        g_autofree char *nowstr = g_date_time_format(now, "%s.%f");
-        fprintf(s->log_file, "%s: %s: %s\n", nowstr, level_str, msg);
-        fflush(s->log_file);
     }
 }
 
@@ -1140,7 +1147,10 @@ static void config_load(GAConfig *config, const char *confpath, bool required)
     }
     if (g_key_file_has_key(keyfile, "general", "verbose", NULL) &&
         g_key_file_get_boolean(keyfile, "general", "verbose", &gerr)) {
-        /* enable all log levels */
+        config->log_level |= G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO;
+    }
+    if (g_key_file_has_key(keyfile, "general", "debug", NULL) &&
+        g_key_file_get_boolean(keyfile, "general", "debug", &gerr)) {
         config->log_level = G_LOG_LEVEL_MASK;
     }
     if (g_key_file_has_key(keyfile, "general", "retry-path", NULL)) {
@@ -1214,7 +1224,9 @@ static void config_dump(GAConfig *config)
 #endif
     g_key_file_set_string(keyfile, "general", "statedir", config->state_dir);
     g_key_file_set_boolean(keyfile, "general", "verbose",
-                           config->log_level == G_LOG_LEVEL_MASK);
+                           config->log_level & G_LOG_LEVEL_INFO);
+    g_key_file_set_boolean(keyfile, "general", "debug",
+                           config->log_level & G_LOG_LEVEL_DEBUG);
     g_key_file_set_boolean(keyfile, "general", "retry-path",
                            config->retry_path);
     tmp = list_join(config->blockedrpcs, ',');
@@ -1238,6 +1250,7 @@ static void config_dump(GAConfig *config)
 
 static void config_parse(GAConfig *config, int argc, char **argv)
 {
+    enum { OPT_DEBUG = 256 };
     const char *sopt = "hVvdc:m:p:l:f:F::b:a:s:t:Dr";
     int opt_ind = 0, ch;
     const struct option lopt[] = {
@@ -1251,6 +1264,7 @@ static void config_parse(GAConfig *config, int argc, char **argv)
         { "fsfreeze-hook", 2, NULL, 'F' },
 #endif
         { "verbose", 0, NULL, 'v' },
+        { "debug", 0, NULL, OPT_DEBUG },
         { "method", 1, NULL, 'm' },
         { "path", 1, NULL, 'p' },
         { "daemonize", 0, NULL, 'd' },
@@ -1313,7 +1327,9 @@ static void config_parse(GAConfig *config, int argc, char **argv)
             config->state_dir = g_strdup(optarg);
             break;
         case 'v':
-            /* enable all log levels */
+            config->log_level |= G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO;
+            break;
+        case OPT_DEBUG:
             config->log_level = G_LOG_LEVEL_MASK;
             break;
         case 'V':
@@ -1673,7 +1689,8 @@ int main(int argc, char **argv)
     GAConfig *config = g_new0(GAConfig, 1);
     int socket_activation;
 
-    config->log_level = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL;
+    config->log_level = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL
+                      | G_LOG_LEVEL_WARNING;
 
     qemu_init_exec_dir(argv[0]);
     qga_qmp_init_marshal(&ga_commands);
Signature · 1 line
2.51.0
#1 lore raw

PATCH v3 2/2 qga: add --audit option for command logging control

Add -A/--audit=LIST option to control which guest agent commands are logged at info level (visible with --verbose) and which at debug level (visible only with --debug).

Patterns are comma-separated and checked in order; the first match wins. Patterns starting with '!' log the command at debug level instead of info.
For example: --audit=!guest-ping,* logs all commands
at info level except guest-ping.

Move command logging from individual handlers into process_event() so all commands are logged in one place. Keep g_debug() calls in handlers for useful details like file paths, handles, and PIDs.

The default pattern is '*', so all commands are logged at info level unless configured otherwise.

---
 qga/commands-linux.c |  2 --
 qga/commands-posix.c | 11 +++---
 qga/commands-win32.c | 14 +++-----
 qga/commands.c       |  5 ++-
 qga/main.c           | 82 ++++++++++++++++++++++++++++++++++++++++++--
 5 files changed, 90 insertions(+), 24 deletions(-)
qga/commands-linux.c +0 -2
index a722de2e6a..8df83963fa 100644
@@ -1158,8 +1158,6 @@ qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp)
     int fd;
     struct fstrim_range r;
 
-    g_info("guest-fstrim called");
-
     QTAILQ_INIT(&mounts);
     if (!build_fs_mount_list(&mounts, errp)) {
         return NULL;
qga/commands-posix.c +4 -7
index 96939a6f36..6a3e6c78e3 100644
@@ -240,7 +240,7 @@ void qmp_guest_shutdown(const char *mode, Error **errp)
     const char *reboot_flag = "-r";
 #endif
 
-    g_info("guest-shutdown called, mode: %s", mode);
+    g_debug("guest-shutdown mode: %s", mode);
     if (!mode || strcmp(mode, "powerdown") == 0) {
         if (access(POWEROFF_CMD_PATH, X_OK) == 0) {
             shutdown_cmd = POWEROFF_CMD_PATH;
@@ -519,7 +519,7 @@ int64_t qmp_guest_file_open(const char *path, const char *mode,
     if (!mode) {
         mode = "r";
     }
-    g_info("guest-file-open called, filepath: %s, mode: %s", path, mode);
+    g_debug("guest-file-open filepath: %s, mode: %s", path, mode);
     fh = safe_open_or_create(path, mode, &local_err);
     if (local_err != NULL) {
         error_propagate(errp, local_err);
@@ -540,7 +540,7 @@ int64_t qmp_guest_file_open(const char *path, const char *mode,
         return -1;
     }
 
-    g_info("guest-file-open, handle: %" PRId64, handle);
+    g_debug("guest-file-open handle: %" PRId64, handle);
     return handle;
 }
 
@@ -549,7 +549,7 @@ void qmp_guest_file_close(int64_t handle, Error **errp)
     GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
     int ret;
 
-    g_info("guest-file-close called, handle: %" PRId64, handle);
+    g_debug("guest-file-close handle: %" PRId64, handle);
     if (!gfh) {
         return;
     }
@@ -793,8 +793,6 @@ int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints,
     FsMountList mounts;
     Error *local_err = NULL;
 
-    g_info("guest-fsfreeze called");
-
     execute_fsfreeze_hook(FSFREEZE_HOOK_FREEZE, &local_err);
     if (local_err) {
         error_propagate(errp, local_err);
@@ -833,7 +831,6 @@ int64_t qmp_guest_fsfreeze_thaw(Error **errp)
 
     if (ret >= 0) {
         ga_unset_frozen(ga_state);
-        g_info("guest-fsthaw called");
         execute_fsfreeze_hook(FSFREEZE_HOOK_THAW, errp);
     } else {
         ret = 0;
qga/commands-win32.c +4 -10
index d26b0041ce..e916d081f5 100644
@@ -231,7 +231,7 @@ int64_t qmp_guest_file_open(const char *path, const char *mode, Error **errp)
     if (!mode) {
         mode = "r";
     }
-    g_info("guest-file-open called, filepath: %s, mode: %s", path, mode);
+    g_debug("guest-file-open filepath: %s, mode: %s", path, mode);
     guest_flags = find_open_flag(mode);
     if (guest_flags == NULL) {
         error_setg(errp, "invalid file open mode");
@@ -267,8 +267,7 @@ int64_t qmp_guest_file_open(const char *path, const char *mode, Error **errp)
         goto done;
     }
 
-    g_info("guest-file-open, handle: % " PRId64, fd);
-
+    g_debug("guest-file-open handle: %" PRId64, fd);
 done:
     g_free(w_path);
     return fd;
@@ -278,7 +277,7 @@ void qmp_guest_file_close(int64_t handle, Error **errp)
 {
     bool ret;
     GuestFileHandle *gfh = guest_file_handle_find(handle, errp);
-    g_info("guest-file-close called, handle: %" PRId64, handle);
+    g_debug("guest-file-close handle: %" PRId64, handle);
     if (gfh == NULL) {
         return;
     }
@@ -337,8 +336,7 @@ void qmp_guest_shutdown(const char *mode, Error **errp)
     Error *local_err = NULL;
     UINT shutdown_flag = EWX_FORCE;
 
-    g_info("guest-shutdown called, mode: %s", mode);
-
+    g_debug("guest-shutdown mode: %s", mode);
     if (!mode || strcmp(mode, "powerdown") == 0) {
         shutdown_flag |= EWX_POWEROFF;
     } else if (strcmp(mode, "halt") == 0) {
@@ -1255,8 +1253,6 @@ int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints,
         return 0;
     }
 
-    g_info("guest-fsfreeze called");
-
     /* cannot risk guest agent blocking itself on a write in this state */
     ga_set_frozen(ga_state);
 
@@ -1294,8 +1290,6 @@ int64_t qmp_guest_fsfreeze_thaw(Error **errp)
 
     ga_unset_frozen(ga_state);
 
-    g_info("guest-fsthaw called");
-
     return i;
 }
 
qga/commands.c +2 -3
index 55edd9fd4c..36b99819af 100644
@@ -43,7 +43,6 @@ int64_t qmp_guest_sync(int64_t id, Error **errp)
 
 void qmp_guest_ping(Error **errp)
 {
-    g_info("guest-ping called");
 }
 
 static void qmp_command_info(const QmpCommand *cmd, void *opaque)
@@ -136,7 +135,7 @@ GuestExecStatus *qmp_guest_exec_status(int64_t pid, Error **errp)
     GuestExecInfo *gei;
     GuestExecStatus *ges;
 
-    g_info("guest-exec-status called, pid: %u", (uint32_t)pid);
+    g_debug("guest-exec-status pid: %u", (uint32_t)pid);
 
     gei = guest_exec_info_find(pid);
     if (gei == NULL) {
@@ -238,7 +237,7 @@ static char **guest_exec_get_args(const strList *entry, bool log)
     args[i] = NULL;
 
     if (log) {
-        g_info("guest-exec called: \"%s\"", str);
+        g_debug("guest-exec called: \"%s\"", str);
     }
     g_free(str);
 
qga/main.c +80 -2

162 lines not on this page · raw message

Signature · 1 line
2.51.0