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.
This commit is contained in:
Peter Krempa 2016-06-07 16:09:09 +02:00
parent 456ccc14d5
commit 5e6143fbcc
2 changed files with 58 additions and 0 deletions

View File

@ -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)
{

View File

@ -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__ */