From 5e6143fbccf2e6afb73c3f872ccdafd02fed5d95 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Tue, 7 Jun 2016 16:09:09 +0200 Subject: [PATCH] log: handler: Add new API to append to logging files For logging one-shot entries to the VM log file it's quite a waste to hold open the file descriptor for logging that is provided by the current API. This new API will be ideal for logging one-shot entries to the file e.g. at the point when we shut the VM down rather than having to add the whole file-descriptor infrastructure. Additionally this will allow to add the messages even after restart of libvirtd since virtlogd doesn't allow to obtain a regular context with filedescriptors while the VM is still active. --- src/logging/log_handler.c | 50 +++++++++++++++++++++++++++++++++++++++ src/logging/log_handler.h | 8 +++++++ 2 files changed, 58 insertions(+) diff --git a/src/logging/log_handler.c b/src/logging/log_handler.c index 4c08223820..a8cb6cf878 100644 --- a/src/logging/log_handler.c +++ b/src/logging/log_handler.c @@ -513,6 +513,56 @@ virLogHandlerDomainReadLogFile(virLogHandlerPtr handler, } +int +virLogHandlerDomainAppendLogFile(virLogHandlerPtr handler, + const char *driver ATTRIBUTE_UNUSED, + const unsigned char *domuuid ATTRIBUTE_UNUSED, + const char *domname ATTRIBUTE_UNUSED, + const char *path, + const char *message, + unsigned int flags) +{ + size_t i; + virRotatingFileWriterPtr writer = NULL; + virRotatingFileWriterPtr newwriter = NULL; + int ret = -1; + + virCheckFlags(0, -1); + + VIR_DEBUG("Appending to log '%s' message: '%s'", path, message); + + virObjectLock(handler); + + for (i = 0; i < handler->nfiles; i++) { + if (STREQ(virRotatingFileWriterGetPath(handler->files[i]->file), path)) { + writer = handler->files[i]->file; + break; + } + } + + if (!writer) { + if (!(newwriter = virRotatingFileWriterNew(path, + DEFAULT_FILE_SIZE, + DEFAULT_MAX_BACKUP, + false, + DEFAULT_MODE))) + goto cleanup; + + writer = newwriter; + } + + if (virRotatingFileWriterAppend(writer, message, strlen(message)) < 0) + goto cleanup; + + ret = 0; + + cleanup: + virRotatingFileWriterFree(newwriter); + virObjectUnlock(handler); + return ret; +} + + virJSONValuePtr virLogHandlerPreExecRestart(virLogHandlerPtr handler) { diff --git a/src/logging/log_handler.h b/src/logging/log_handler.h index 54a9cd9288..4607e4556d 100644 --- a/src/logging/log_handler.h +++ b/src/logging/log_handler.h @@ -65,6 +65,14 @@ char *virLogHandlerDomainReadLogFile(virLogHandlerPtr handler, size_t maxlen, unsigned int flags); +int virLogHandlerDomainAppendLogFile(virLogHandlerPtr handler, + const char *driver, + const unsigned char *domuuid, + const char *domname, + const char *path, + const char *message, + unsigned int flags); + virJSONValuePtr virLogHandlerPreExecRestart(virLogHandlerPtr handler); #endif /** __VIR_LOG_HANDLER_H__ */ -- GitLab