Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trace: avoid leading comma on second trace output #647

Closed
wants to merge 1 commit into from

Conversation

cspiel1
Copy link
Collaborator

@cspiel1 cspiel1 commented Jan 17, 2023

No description provided.

@sreimers
Copy link
Member

sreimers commented Jan 17, 2023

re_trace_flush() can be called multiple times for same json file. Maybe better to move first to trace.first and set to true in re_trace_init(), like so (untested):

diff --git a/src/trace/trace.c b/src/trace/trace.c
index fa3571a..5723698 100644
--- a/src/trace/trace.c
+++ b/src/trace/trace.c
@@ -58,6 +58,7 @@ static struct {
        mtx_t lock;
        bool init;
        uint64_t start_time;
+       bool first;
 } trace = {
        .init = false
 };
@@ -130,6 +131,7 @@ int re_trace_init(const char *json_file)
 
        trace.start_time = tmr_jiffies_usec();
        trace.init = true;
+       trace.first = true;
 
 out:
        if (err) {
@@ -177,7 +179,6 @@ int re_trace_flush(void)
        struct trace_event *e;
        char json_arg[256];
        char name[128];
-       static bool first = true;
 
 #ifndef RE_TRACE_ENABLED
        return 0;
@@ -227,10 +228,10 @@ int re_trace_flush(void)
                (void)re_fprintf(trace.f,
                        "%s{\"cat\":\"%s\",\"pid\":%i,\"tid\":%lu,\"ts\":%llu,"
                        "\"ph\":\"%c\",%s%s}",
-                       first ? "" : ",\n",
+                       trace.first ? "" : ",\n",
                        e->cat, e->pid, e->tid, e->ts - trace.start_time,
                        e->ph, name, str_isset(json_arg) ? json_arg : "");
-               first = false;
+               trace.first = false;
        }
 
        (void)fflush(trace.f);

@sreimers
Copy link
Member

Can you try this PR #648

@cspiel1
Copy link
Collaborator Author

cspiel1 commented Jan 19, 2023

It is fixed now in main.

@cspiel1 cspiel1 closed this Jan 19, 2023
@cspiel1 cspiel1 deleted the trace_first_line branch January 19, 2023 07:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants