Merge branch 'maint' of git://github.com/git-l10n/git-po into maint
[git.git] / Documentation / technical / api-trace.txt
blob097a651d9680ea31c1a5a0e522a77ccec96c6b69
1 trace API
2 =========
4 The trace API can be used to print debug messages to stderr or a file. Trace
5 code is inactive unless explicitly enabled by setting `GIT_TRACE*` environment
6 variables.
8 The trace implementation automatically adds `timestamp file:line ... \n` to
9 all trace messages. E.g.:
11 ------------
12 23:59:59.123456 git.c:312               trace: built-in: git 'foo'
13 00:00:00.000001 builtin/foo.c:99        foo: some message
14 ------------
16 Data Structures
17 ---------------
19 `struct trace_key`::
21         Defines a trace key (or category). The default (for API functions that
22         don't take a key) is `GIT_TRACE`.
24 E.g. to define a trace key controlled by environment variable `GIT_TRACE_FOO`:
26 ------------
27 static struct trace_key trace_foo = TRACE_KEY_INIT(FOO);
29 static void trace_print_foo(const char *message)
31         trace_print_key(&trace_foo, message);
33 ------------
35 Note: don't use `const` as the trace implementation stores internal state in
36 the `trace_key` structure.
38 Functions
39 ---------
41 `int trace_want(struct trace_key *key)`::
43         Checks whether the trace key is enabled. Used to prevent expensive
44         string formatting before calling one of the printing APIs.
46 `void trace_disable(struct trace_key *key)`::
48         Disables tracing for the specified key, even if the environment
49         variable was set.
51 `void trace_printf(const char *format, ...)`::
52 `void trace_printf_key(struct trace_key *key, const char *format, ...)`::
54         Prints a formatted message, similar to printf.
56 `void trace_argv_printf(const char **argv, const char *format, ...)``::
58         Prints a formatted message, followed by a quoted list of arguments.
60 `void trace_strbuf(struct trace_key *key, const struct strbuf *data)`::
62         Prints the strbuf, without additional formatting (i.e. doesn't
63         choke on `%` or even `\0`).
65 `uint64_t getnanotime(void)`::
67         Returns nanoseconds since the epoch (01/01/1970), typically used
68         for performance measurements.
70 Currently there are high precision timer implementations for Linux (using
71 `clock_gettime(CLOCK_MONOTONIC)`) and Windows (`QueryPerformanceCounter`).
72 Other platforms use `gettimeofday` as time source.
74 `void trace_performance(uint64_t nanos, const char *format, ...)`::
75 `void trace_performance_since(uint64_t start, const char *format, ...)`::
77         Prints the elapsed time (in nanoseconds), or elapsed time since
78         `start`, followed by a formatted message. Enabled via environment
79         variable `GIT_TRACE_PERFORMANCE`. Used for manual profiling, e.g.:
81 ------------
82 uint64_t start = getnanotime();
83 /* code section to measure */
84 trace_performance_since(start, "foobar");
85 ------------
87 ------------
88 uint64_t t = 0;
89 for (;;) {
90         /* ignore */
91         t -= getnanotime();
92         /* code section to measure */
93         t += getnanotime();
94         /* ignore */
96 trace_performance(t, "frotz");
97 ------------