Skip to content

Commit b8faa5f

Browse files
committed
trace: implement %p placeholder for filenames
This lets you trace output on a per-process basis by naming the trace file after the PID. This is probably not all that useful for GIT_TRACE itself (which is about a global view of the process hierarchy anyway), but will be useful for traces which produce large amounts of data (e.g., whole packfiles). Signed-off-by: Jeff King <peff@peff.net>
1 parent 94f0577 commit b8faa5f

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

Documentation/git.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,8 @@ trace messages into this file descriptor.
804804
Alternatively, if the variable is set to an absolute path
805805
(starting with a '/' character), Git will interpret this
806806
as a file path and will try to append the trace messages
807-
to it.
807+
to it. If the filename contains the string `%p`, that string
808+
will be replaced with the PID of the traced process.
808809
+
809810
Unsetting the variable, or setting it to empty, "0" or
810811
"false" (case insensitive) disables trace messages.

trace.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,28 @@ static int get_trace_fd(struct trace_key *key, const char *override_envvar)
5353
else if (strlen(trace) == 1 && isdigit(*trace))
5454
key->fd = atoi(trace);
5555
else if (is_absolute_path(trace)) {
56-
int fd = open(trace, O_WRONLY | O_APPEND | O_CREAT, 0666);
56+
struct strbuf name = STRBUF_INIT;
57+
int fd;
58+
59+
while (strbuf_expand_step(&name, &trace)) {
60+
if (skip_prefix(trace, "%", &trace))
61+
strbuf_addch(&name, '%');
62+
else if (skip_prefix(trace, "p", &trace))
63+
strbuf_addf(&name, "%lu", (unsigned long)getpid());
64+
else
65+
strbuf_addch(&name, '%');
66+
}
67+
68+
fd = open(name.buf, O_WRONLY | O_APPEND | O_CREAT, 0666);
5769
if (fd == -1) {
5870
warning("could not open '%s' for tracing: %s",
59-
trace, strerror(errno));
71+
name.buf, strerror(errno));
6072
trace_disable(key);
6173
} else {
6274
key->fd = fd;
6375
key->need_close = 1;
6476
}
77+
strbuf_release(&name);
6578
} else {
6679
warning("unknown trace value for '%s': %s\n"
6780
" If you want to trace into a file, then please set %s\n"

0 commit comments

Comments
 (0)