From f9f0e6173e1d570847930abfe2b4560c7b6a964a Mon Sep 17 00:00:00 2001 From: Andrey Drobyshev Date: Tue, 29 Nov 2022 19:38:08 +0200 Subject: qga-win: add logging to Windows event log MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit allows QGA to write to Windows event log using Win32 API's ReportEvent() [1], much like syslog() under *nix guests. In order to generate log message definitions we use a very basic message text file [2], so that every QGA's message gets ID 1. The tools "windmc" and "windres" respectively are used to generate ".rc" file and COFF object file, and then the COFF file is linked into qemu-ga.exe. [1] https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-reporteventa [2] https://learn.microsoft.com/en-us/windows/win32/eventlog/message-text-files Originally-by: Yuri Pudgorodskiy Signed-off-by: Andrey Drobyshev Reviewed-by: Marc-André Lureau Reviewed-by: Konstantin Kostiuk Tested-by: Konstantin Kostiuk Signed-off-by: Konstantin Kostiuk --- qga/installer/qemu-ga.wxs | 5 +++++ qga/main.c | 16 +++++++++++++--- qga/meson.build | 19 ++++++++++++++++++- qga/messages-win32.mc | 9 +++++++++ 4 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 qga/messages-win32.mc (limited to 'qga') diff --git a/qga/installer/qemu-ga.wxs b/qga/installer/qemu-ga.wxs index 813d1c6..e344c38 100644 --- a/qga/installer/qemu-ga.wxs +++ b/qga/installer/qemu-ga.wxs @@ -110,6 +110,11 @@ + + + + diff --git a/qga/main.c b/qga/main.c index 0865c99..1463a1c 100644 --- a/qga/main.c +++ b/qga/main.c @@ -83,6 +83,7 @@ struct GAState { #ifdef _WIN32 GAService service; HANDLE wakeup_event; + HANDLE event_log; #endif bool delimit_response; bool frozen; @@ -324,13 +325,14 @@ static void ga_log(const gchar *domain, GLogLevelFlags level, } level &= G_LOG_LEVEL_MASK; -#ifndef _WIN32 if (g_strcmp0(domain, "syslog") == 0) { +#ifndef _WIN32 syslog(LOG_INFO, "%s: %s", level_str, msg); - } else if (level & s->log_level) { #else - if (level & s->log_level) { + ReportEvent(s->event_log, EVENTLOG_INFORMATION_TYPE, + 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); @@ -1286,6 +1288,13 @@ static GAState *initialize_agent(GAConfig *config, int socket_activation) g_debug("Guest agent version %s started", QEMU_FULL_VERSION); #ifdef _WIN32 + s->event_log = RegisterEventSource(NULL, "qemu-ga"); + if (!s->event_log) { + g_autofree gchar *errmsg = g_win32_error_message(GetLastError()); + g_critical("unable to register event source: %s", errmsg); + return NULL; + } + /* On win32 the state directory is application specific (be it the default * or a user override). We got past the command line parsing; let's create * the directory (with any intermediate directories). If we run into an @@ -1377,6 +1386,7 @@ static void cleanup_agent(GAState *s) { #ifdef _WIN32 CloseHandle(s->wakeup_event); + CloseHandle(s->event_log); #endif if (s->command_state) { ga_command_state_cleanup_all(s->command_state); diff --git a/qga/meson.build b/qga/meson.build index 3cfb916..1ff159e 100644 --- a/qga/meson.build +++ b/qga/meson.build @@ -98,7 +98,24 @@ if targetos == 'windows' endif endif -qga = executable('qemu-ga', qga_ss.sources(), +qga_objs = [] +if targetos == 'windows' + windmc = find_program('windmc', required: true) + windres = find_program('windres', required: true) + + msgrc = custom_target('messages-win32.rc', + input: 'messages-win32.mc', + output: ['messages-win32.rc', 'MSG00409.bin', 'messages-win32.h'], + command: [windmc, '-h', '@OUTDIR@', '-r', '@OUTDIR@', '@INPUT@']) + msgobj = custom_target('messages-win32.o', + input: msgrc[0], + output: 'messages-win32.o', + command: [windres, '-I', '@OUTDIR@', '-o', '@OUTPUT@', '@INPUT@']) + + qga_objs = [msgobj] +endif + +qga = executable('qemu-ga', qga_ss.sources() + qga_objs, link_args: qga_libs, dependencies: [qemuutil, libudev], install: true) diff --git a/qga/messages-win32.mc b/qga/messages-win32.mc new file mode 100644 index 0000000..e21019c --- /dev/null +++ b/qga/messages-win32.mc @@ -0,0 +1,9 @@ +LanguageNames=( + English=0x409:MSG00409 +) + +MessageId=1 +SymbolicName=QEMU_GA_EVENTLOG_GENERAL +Language=English +%1 +. -- cgit v1.1