Make use of timestamp option in virtlogd config and append timestamp to
domain event logs after every '\n' character.
Signed-off-by: Shaleen Bathla <shaleen.bathla(a)oracle.com>
---
src/logging/log_handler.c | 10 +++++++---
src/util/virrotatingfile.c | 36 ++++++++++++++++++++++++++++++++++--
src/util/virrotatingfile.h | 3 ++-
3 files changed, 43 insertions(+), 6 deletions(-)
diff --git a/src/logging/log_handler.c b/src/logging/log_handler.c
index 71517bbbe5ff..9be338d92ca1 100644
--- a/src/logging/log_handler.c
+++ b/src/logging/log_handler.c
@@ -116,6 +116,7 @@ virLogHandlerDomainLogFileEvent(int watch,
virLogHandlerLogFile *logfile;
char buf[1024];
ssize_t len;
+ bool timestamp = false;
virObjectLock(handler);
logfile = virLogHandlerGetLogFileFromWatch(handler, watch);
@@ -130,6 +131,9 @@ virLogHandlerDomainLogFileEvent(int watch,
goto cleanup;
}
+ if (handler->config && handler->config->timestamp)
+ timestamp = true;
+
reread:
len = read(fd, buf, sizeof(buf));
if (len < 0) {
@@ -143,7 +147,7 @@ virLogHandlerDomainLogFileEvent(int watch,
goto error;
}
- if (virRotatingFileWriterAppend(logfile->file, buf, len) != len)
+ if (virRotatingFileWriterAppend(logfile->file, buf, len, timestamp) != len)
goto error;
cleanup:
@@ -440,7 +444,7 @@ virLogHandlerDomainLogFileDrain(virLogHandlerLogFile *file)
return;
}
- if (virRotatingFileWriterAppend(file->file, buf, len) != len)
+ if (virRotatingFileWriterAppend(file->file, buf, len, 0) != len)
return;
}
}
@@ -568,7 +572,7 @@ virLogHandlerDomainAppendLogFile(virLogHandler *handler,
writer = newwriter;
}
- if (virRotatingFileWriterAppend(writer, message, strlen(message)) < 0)
+ if (virRotatingFileWriterAppend(writer, message, strlen(message), 0) < 0)
goto cleanup;
ret = 0;
diff --git a/src/util/virrotatingfile.c b/src/util/virrotatingfile.c
index 2a44c9bf93e3..5443e850721b 100644
--- a/src/util/virrotatingfile.c
+++ b/src/util/virrotatingfile.c
@@ -431,7 +431,7 @@ virRotatingFileWriterAppendTimestamp(virRotatingFileWriter* file)
timestamp[len-1] = ' ';
timestamp[len] = '\0';
- if (virRotatingFileWriterAppend(file, timestamp, len) != len) {
+ if (virRotatingFileWriterAppend(file, timestamp, len, 0) != len) {
g_free(timestamp);
return -1;
}
@@ -446,6 +446,7 @@ virRotatingFileWriterAppendTimestamp(virRotatingFileWriter* file)
* @file: the file context
* @buf: the data buffer
* @len: the number of bytes in @buf
+ * @timestamp: append timestamp config option
*
* Append the data in @buf to the file, performing rollover
* of the files if their size would exceed the limit
@@ -455,10 +456,12 @@ virRotatingFileWriterAppendTimestamp(virRotatingFileWriter* file)
ssize_t
virRotatingFileWriterAppend(virRotatingFileWriter *file,
const char *buf,
- size_t len)
+ size_t len,
+ bool timestamp)
{
ssize_t ret = 0;
size_t i;
+
while (len) {
size_t towrite = len;
bool forceRollover = false;
@@ -488,6 +491,35 @@ virRotatingFileWriterAppend(virRotatingFileWriter *file,
}
}
+ if (timestamp && towrite) {
+ int last_nl = -1;
+ for (i = 0; i < towrite; i++) {
+ if (buf[i] == '\n') {
+ if (safewrite(file->entry->fd, buf+last_nl+1, i-last_nl) !=
i-last_nl) {
+ virReportSystemError(errno,
+ _("Unable to write to file
%1$s"),
+ file->basepath);
+ return -1;
+ }
+ if (virRotatingFileWriterAppendTimestamp(file) != 0) {
+ virReportSystemError(errno,
+ _("Unable to write to file
%1$s"),
+ file->basepath);
+ return -1;
+ }
+ last_nl = i;
+ }
+ }
+
+ last_nl++;
+ buf += last_nl;
+ len -= last_nl;
+ ret += last_nl;
+ file->entry->pos += last_nl;
+ file->entry->len += last_nl;
+ towrite -= last_nl;
+ }
+
if (towrite) {
if (safewrite(file->entry->fd, buf, towrite) != towrite) {
virReportSystemError(errno,
diff --git a/src/util/virrotatingfile.h b/src/util/virrotatingfile.h
index 1ed8838e7df2..388774bc3913 100644
--- a/src/util/virrotatingfile.h
+++ b/src/util/virrotatingfile.h
@@ -43,7 +43,8 @@ off_t virRotatingFileWriterGetOffset(virRotatingFileWriter *file);
ssize_t virRotatingFileWriterAppend(virRotatingFileWriter *file,
const char *buf,
- size_t len);
+ size_t len,
+ bool timestamp);
int virRotatingFileReaderSeek(virRotatingFileReader *file,
ino_t inode,
--
2.39.3