diff mbox series

[2/5] v4l2-tracer: remove trailing comma from JSON trace

Message ID abcc11027eb2a9364c7ba8f6b7ea2d3688ef8f09.1675920064.git.deborah.brouwer@collabora.com (mailing list archive)
State New, archived
Headers show
Series v4l2-tracer: misc fixes | expand

Commit Message

Deborah Brouwer Feb. 9, 2023, 6:06 a.m. UTC
Put the trace file into valid JSON format by removing the trailing comma
from the trace file array.

Signed-off-by: Deborah Brouwer <deborah.brouwer@collabora.com>
---
 utils/v4l2-tracer/v4l2-tracer.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Hans Verkuil Feb. 22, 2023, 7:26 p.m. UTC | #1
On 09/02/2023 07:06, Deborah Brouwer wrote:
> Put the trace file into valid JSON format by removing the trailing comma
> from the trace file array.
> 
> Signed-off-by: Deborah Brouwer <deborah.brouwer@collabora.com>
> ---
>  utils/v4l2-tracer/v4l2-tracer.cpp | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/utils/v4l2-tracer/v4l2-tracer.cpp b/utils/v4l2-tracer/v4l2-tracer.cpp
> index ae6f68e4..7adbe04e 100644
> --- a/utils/v4l2-tracer/v4l2-tracer.cpp
> +++ b/utils/v4l2-tracer/v4l2-tracer.cpp
> @@ -346,7 +346,7 @@ int tracer(int argc, char *argv[], bool retrace)
>  
>  	/* Close the json-array and the trace file. */
>  	trace_file = fopen(trace_filename.c_str(), "a");
> -	fseek(trace_file, 0L, SEEK_END);
> +	ftruncate(fileno(trace_file), lseek(fileno(trace_file), 0, SEEK_END) - 2);
>  	fputs("\n]\n", trace_file);

ftruncate seems overkill to me. Wouldn't it be enough to just do

fseek(trace_file, -2, SEEK_END);

since the fputs afterwards will overwrite those last two characters anyway?

(I think it is -2 for SEEK_END, I'm not 100% certain though, it could be 2)

Regards,

	Hans

>  	fclose(trace_file);
>
Deborah Brouwer Feb. 22, 2023, 8:43 p.m. UTC | #2
On Wed, Feb 22, 2023 at 08:26:33PM +0100, Hans Verkuil wrote:
> On 09/02/2023 07:06, Deborah Brouwer wrote:
> > Put the trace file into valid JSON format by removing the trailing comma
> > from the trace file array.
> > 
> > Signed-off-by: Deborah Brouwer <deborah.brouwer@collabora.com>
> > ---
> >  utils/v4l2-tracer/v4l2-tracer.cpp | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/utils/v4l2-tracer/v4l2-tracer.cpp b/utils/v4l2-tracer/v4l2-tracer.cpp
> > index ae6f68e4..7adbe04e 100644
> > --- a/utils/v4l2-tracer/v4l2-tracer.cpp
> > +++ b/utils/v4l2-tracer/v4l2-tracer.cpp
> > @@ -346,7 +346,7 @@ int tracer(int argc, char *argv[], bool retrace)
> >  
> >  	/* Close the json-array and the trace file. */
> >  	trace_file = fopen(trace_filename.c_str(), "a");
> > -	fseek(trace_file, 0L, SEEK_END);
> > +	ftruncate(fileno(trace_file), lseek(fileno(trace_file), 0, SEEK_END) - 2);
> >  	fputs("\n]\n", trace_file);
> 
> ftruncate seems overkill to me. Wouldn't it be enough to just do
> 
> fseek(trace_file, -2, SEEK_END);
> 
> since the fputs afterwards will overwrite those last two characters anyway?
> 
> (I think it is -2 for SEEK_END, I'm not 100% certain though, it could be 2)

It took me a while to remember why I didn't use this simpler method,
but it's because the trace file is opened in append mode "a" in
write_json_object_to_json_file() and so fputs() stubbornly ignores the
file position indicator set by fseek.

> 
> Regards,
> 
> 	Hans
> 
> >  	fclose(trace_file);
> >  
>
diff mbox series

Patch

diff --git a/utils/v4l2-tracer/v4l2-tracer.cpp b/utils/v4l2-tracer/v4l2-tracer.cpp
index ae6f68e4..7adbe04e 100644
--- a/utils/v4l2-tracer/v4l2-tracer.cpp
+++ b/utils/v4l2-tracer/v4l2-tracer.cpp
@@ -346,7 +346,7 @@  int tracer(int argc, char *argv[], bool retrace)
 
 	/* Close the json-array and the trace file. */
 	trace_file = fopen(trace_filename.c_str(), "a");
-	fseek(trace_file, 0L, SEEK_END);
+	ftruncate(fileno(trace_file), lseek(fileno(trace_file), 0, SEEK_END) - 2);
 	fputs("\n]\n", trace_file);
 	fclose(trace_file);