Skip to content

Commit b72be02

Browse files
kbleesgitster
authored andcommitted
trace: add current timestamp to all trace output
This is useful to tell apart trace output of separate test runs. It can also be used for basic, coarse-grained performance analysis. Note that the accuracy is tainted by writing to the trace file, and you have to calculate the deltas yourself (which is next to impossible if multiple threads or processes are involved). Signed-off-by: Karsten Blees <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 124647c commit b72be02

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

trace.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ static const char err_msg[] = "Could not trace into fd given by "
8888
static int prepare_trace_line(struct trace_key *key, struct strbuf *buf)
8989
{
9090
static struct trace_key trace_bare = TRACE_KEY_INIT(BARE);
91+
struct timeval tv;
92+
struct tm tm;
93+
time_t secs;
9194

9295
if (!trace_want(key))
9396
return 0;
@@ -98,7 +101,12 @@ static int prepare_trace_line(struct trace_key *key, struct strbuf *buf)
98101
if (trace_want(&trace_bare))
99102
return 1;
100103

101-
/* add line prefix here */
104+
/* print current timestamp */
105+
gettimeofday(&tv, NULL);
106+
secs = tv.tv_sec;
107+
localtime_r(&secs, &tm);
108+
strbuf_addf(buf, "%02d:%02d:%02d.%06ld ", tm.tm_hour, tm.tm_min,
109+
tm.tm_sec, (long) tv.tv_usec);
102110

103111
return 1;
104112
}

0 commit comments

Comments
 (0)