diff mbox series

[2/2] qga: map GLib log levels to system levels

Message ID 20221128185403.447817-3-andrey.drobyshev@virtuozzo.com (mailing list archive)
State New, archived
Headers show
Series qga: improve "syslog" domain logging | expand

Commit Message

Andrey Drobyshev Nov. 28, 2022, 6:54 p.m. UTC
This patch translates GLib-specific log levels to system ones, so that
they may be used by both *nix syslog() (as a "priority" argument) and
Windows ReportEvent() (as a "wType" argument).

Currently the only codepath to write to "syslog" domain is slog()
function.  However, this patch allows the interface to be extended.

Note that since slog() is using G_LOG_LEVEL_INFO level, its behaviour
doesn't change.

Originally-by: Yuri Pudgorodskiy <yur@virtuozzo.com>
Signed-off-by: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
---
 qga/main.c | 36 ++++++++++++++++++++++++++++++++++--
 1 file changed, 34 insertions(+), 2 deletions(-)

Comments

Marc-André Lureau Nov. 29, 2022, 7:38 a.m. UTC | #1
On Tue, Nov 29, 2022 at 12:59 AM Andrey Drobyshev via
<qemu-devel@nongnu.org> wrote:
>
> This patch translates GLib-specific log levels to system ones, so that
> they may be used by both *nix syslog() (as a "priority" argument) and
> Windows ReportEvent() (as a "wType" argument).
>
> Currently the only codepath to write to "syslog" domain is slog()
> function.  However, this patch allows the interface to be extended.
>
> Note that since slog() is using G_LOG_LEVEL_INFO level, its behaviour
> doesn't change.
>
> Originally-by: Yuri Pudgorodskiy <yur@virtuozzo.com>
> Signed-off-by: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>

> ---
>  qga/main.c | 36 ++++++++++++++++++++++++++++++++++--
>  1 file changed, 34 insertions(+), 2 deletions(-)
>
> diff --git a/qga/main.c b/qga/main.c
> index 10314dfe5d..0467d5daf8 100644
> --- a/qga/main.c
> +++ b/qga/main.c
> @@ -314,6 +314,38 @@ void ga_enable_logging(GAState *s)
>      s->logging_enabled = true;
>  }
>
> +static int glib_log_level_to_system(int level)
> +{
> +    switch (level) {
> +#ifndef _WIN32
> +        case G_LOG_LEVEL_ERROR:
> +            return LOG_ERR;
> +        case G_LOG_LEVEL_CRITICAL:
> +            return LOG_CRIT;
> +        case G_LOG_LEVEL_WARNING:
> +            return LOG_WARNING;
> +        case G_LOG_LEVEL_MESSAGE:
> +            return LOG_NOTICE;
> +        case G_LOG_LEVEL_DEBUG:
> +            return LOG_DEBUG;
> +        case G_LOG_LEVEL_INFO:
> +        default:
> +            return LOG_INFO;
> +#else
> +        case G_LOG_LEVEL_ERROR:
> +        case G_LOG_LEVEL_CRITICAL:
> +            return EVENTLOG_ERROR_TYPE;
> +        case G_LOG_LEVEL_WARNING:
> +            return EVENTLOG_WARNING_TYPE;
> +        case G_LOG_LEVEL_MESSAGE:
> +        case G_LOG_LEVEL_INFO:
> +        case G_LOG_LEVEL_DEBUG:
> +        default:
> +            return EVENTLOG_INFORMATION_TYPE;
> +#endif
> +    }
> +}
> +
>  static void ga_log(const gchar *domain, GLogLevelFlags level,
>                     const gchar *msg, gpointer opaque)
>  {
> @@ -327,9 +359,9 @@ static void ga_log(const gchar *domain, GLogLevelFlags level,
>      level &= G_LOG_LEVEL_MASK;
>      if (g_strcmp0(domain, "syslog") == 0) {
>  #ifndef _WIN32
> -        syslog(LOG_INFO, "%s: %s", level_str, msg);
> +        syslog(glib_log_level_to_system(level), "%s: %s", level_str, msg);
>  #else
> -        ReportEvent(s->event_log, EVENTLOG_INFORMATION_TYPE,
> +        ReportEvent(s->event_log, glib_log_level_to_system(level),
>                      0, 1, NULL, 1, 0, &msg, NULL);
>  #endif
>      } else if (level & s->log_level) {
> --
> 2.38.1
>
>
diff mbox series

Patch

diff --git a/qga/main.c b/qga/main.c
index 10314dfe5d..0467d5daf8 100644
--- a/qga/main.c
+++ b/qga/main.c
@@ -314,6 +314,38 @@  void ga_enable_logging(GAState *s)
     s->logging_enabled = true;
 }
 
+static int glib_log_level_to_system(int level)
+{
+    switch (level) {
+#ifndef _WIN32
+        case G_LOG_LEVEL_ERROR:
+            return LOG_ERR;
+        case G_LOG_LEVEL_CRITICAL:
+            return LOG_CRIT;
+        case G_LOG_LEVEL_WARNING:
+            return LOG_WARNING;
+        case G_LOG_LEVEL_MESSAGE:
+            return LOG_NOTICE;
+        case G_LOG_LEVEL_DEBUG:
+            return LOG_DEBUG;
+        case G_LOG_LEVEL_INFO:
+        default:
+            return LOG_INFO;
+#else
+        case G_LOG_LEVEL_ERROR:
+        case G_LOG_LEVEL_CRITICAL:
+            return EVENTLOG_ERROR_TYPE;
+        case G_LOG_LEVEL_WARNING:
+            return EVENTLOG_WARNING_TYPE;
+        case G_LOG_LEVEL_MESSAGE:
+        case G_LOG_LEVEL_INFO:
+        case G_LOG_LEVEL_DEBUG:
+        default:
+            return EVENTLOG_INFORMATION_TYPE;
+#endif
+    }
+}
+
 static void ga_log(const gchar *domain, GLogLevelFlags level,
                    const gchar *msg, gpointer opaque)
 {
@@ -327,9 +359,9 @@  static void ga_log(const gchar *domain, GLogLevelFlags level,
     level &= G_LOG_LEVEL_MASK;
     if (g_strcmp0(domain, "syslog") == 0) {
 #ifndef _WIN32
-        syslog(LOG_INFO, "%s: %s", level_str, msg);
+        syslog(glib_log_level_to_system(level), "%s: %s", level_str, msg);
 #else
-        ReportEvent(s->event_log, EVENTLOG_INFORMATION_TYPE,
+        ReportEvent(s->event_log, glib_log_level_to_system(level),
                     0, 1, NULL, 1, 0, &msg, NULL);
 #endif
     } else if (level & s->log_level) {