[prev in list] [next in list] [prev in thread] [next in thread]
List: libvir-list
Subject: Re: [libvirt] [PATCH 2/4] util: virrotatingfile: Introduce virRotatingFileWriterAppendTimestamp
From: Michal Prívozník <mprivozn () redhat ! com>
Date: 2024-02-29 15:03:53
Message-ID: 28bdcc52-aff2-4922-9d09-fb4401bfcba2 () redhat ! com
[Download RAW message or body]
On 2/29/24 07:46, Shaleen Bathla wrote:
> virRotatingFileWriterAppendTimestamp function appends timestamp to a
> file.
>
> Signed-off-by: Shaleen Bathla <shaleen.bathla@oracle.com>
> ---
> src/util/virrotatingfile.c | 35 +++++++++++++++++++++++++++++++++++
> 1 file changed, 35 insertions(+)
>
> diff --git a/src/util/virrotatingfile.c b/src/util/virrotatingfile.c
> index b02a88c4b08d..2a44c9bf93e3 100644
> --- a/src/util/virrotatingfile.c
> +++ b/src/util/virrotatingfile.c
> @@ -30,6 +30,7 @@
> #include "virerror.h"
> #include "virfile.h"
> #include "virlog.h"
> +#include "virtime.h"
>
> VIR_LOG_INIT("util.rotatingfile");
>
> @@ -406,6 +407,40 @@ virRotatingFileWriterRollover(virRotatingFileWriter *file)
> }
>
>
> +/**
> + * virRotatingFileWriterAppendTimestamp:
> + * @file: the file context
> + *
> + * Append current timestamp with a trailing ' ' char to @file
> + *
> + * Returns 0 on success and -1 on error
> + */
> +static ssize_t
> +virRotatingFileWriterAppendTimestamp(virRotatingFileWriter* file)
> +{
> + char* timestamp = NULL;
> + size_t len = 0;
> +
> + if (!(timestamp = virTimeStringNow())) {
> + g_free(timestamp);
> + return -1;
> + }
> +
> + len = VIR_TIME_STRING_BUFLEN;
> + timestamp = g_realloc(timestamp, len+1);
> + timestamp[len-1] = ' ';
> + timestamp[len] = '\0';
Firstly, to avoid having to call g_free() everywhere you can declare the
variable like this:
g_autofree char *timestamp = NULL;
but in this specific case, when we know how long the string is going to
be (and that it's relatively short), might as well allocate the buffer
on the stack:
char timestamp[VIR_TIME_STRING_BUFLEN + 1] = { 0 };
and then call virTimeStringNowRaw() instead. This avoid necessary g_realloc.
> +
> + if (virRotatingFileWriterAppend(file, timestamp, len) != len) {
So eventually, virRotatingFileWriterAppend() would call
virRotatingFileWriterAppendTimestamp() which would then call
virRotatingFileWriterAppend() again?
I wonder whether the timestamp can't be prepended to the string caller
wants to write in virRotatingFileWriterAppend().
Michal
_______________________________________________
Devel mailing list -- devel@lists.libvirt.org
To unsubscribe send an email to devel-leave@lists.libvirt.org
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic