]> git.ipfire.org Git - thirdparty/git.git/blame - trace2/tr2_tgt_normal.c
Merge branch 'en/fetch-negotiation-default-fix'
[thirdparty/git.git] / trace2 / tr2_tgt_normal.c
CommitLineData
ee4512ed
JH
1#include "cache.h"
2#include "config.h"
3#include "run-command.h"
4#include "quote.h"
5#include "version.h"
6#include "trace2/tr2_dst.h"
bce9db6d 7#include "trace2/tr2_sysenv.h"
ee4512ed
JH
8#include "trace2/tr2_tbuf.h"
9#include "trace2/tr2_tgt.h"
10#include "trace2/tr2_tls.h"
11
87db61a4 12static struct tr2_dst tr2dst_normal = { TR2_SYSENV_NORMAL, 0, 0, 0, 0 };
ee4512ed
JH
13
14/*
bce9db6d 15 * Use the TR2_SYSENV_NORMAL_BRIEF setting to omit the "<time> <file>:<line>"
ee4512ed
JH
16 * fields from each line written to the builtin normal target.
17 *
18 * Unit tests may want to use this to help with testing.
19 */
bce9db6d 20static int tr2env_normal_be_brief;
ee4512ed
JH
21
22#define TR2FMT_NORMAL_FL_WIDTH (50)
23
24static int fn_init(void)
25{
26 int want = tr2_dst_trace_want(&tr2dst_normal);
27 int want_brief;
bce9db6d 28 const char *brief;
ee4512ed
JH
29
30 if (!want)
31 return want;
32
bce9db6d 33 brief = tr2_sysenv_get(TR2_SYSENV_NORMAL_BRIEF);
ee4512ed
JH
34 if (brief && *brief &&
35 ((want_brief = git_parse_maybe_bool(brief)) != -1))
bce9db6d 36 tr2env_normal_be_brief = want_brief;
ee4512ed
JH
37
38 return want;
39}
40
41static void fn_term(void)
42{
43 tr2_dst_trace_disable(&tr2dst_normal);
44}
45
46static void normal_fmt_prepare(const char *file, int line, struct strbuf *buf)
47{
48 strbuf_setlen(buf, 0);
49
bce9db6d 50 if (!tr2env_normal_be_brief) {
ee4512ed
JH
51 struct tr2_tbuf tb_now;
52
53 tr2_tbuf_local_time(&tb_now);
54 strbuf_addstr(buf, tb_now.buf);
55 strbuf_addch(buf, ' ');
56
57 if (file && *file)
58 strbuf_addf(buf, "%s:%d ", file, line);
59 while (buf->len < TR2FMT_NORMAL_FL_WIDTH)
60 strbuf_addch(buf, ' ');
61 }
62}
63
64static void normal_io_write_fl(const char *file, int line,
65 const struct strbuf *buf_payload)
66{
67 struct strbuf buf_line = STRBUF_INIT;
68
69 normal_fmt_prepare(file, line, &buf_line);
70 strbuf_addbuf(&buf_line, buf_payload);
71 tr2_dst_write_line(&tr2dst_normal, &buf_line);
72 strbuf_release(&buf_line);
73}
74
75static void fn_version_fl(const char *file, int line)
76{
77 struct strbuf buf_payload = STRBUF_INIT;
78
79 strbuf_addf(&buf_payload, "version %s", git_version_string);
80 normal_io_write_fl(file, line, &buf_payload);
81 strbuf_release(&buf_payload);
82}
83
39f43177
JH
84static void fn_start_fl(const char *file, int line,
85 uint64_t us_elapsed_absolute, const char **argv)
ee4512ed
JH
86{
87 struct strbuf buf_payload = STRBUF_INIT;
88
89 strbuf_addstr(&buf_payload, "start ");
e3443055 90 sq_append_quote_argv_pretty(&buf_payload, argv);
ee4512ed
JH
91 normal_io_write_fl(file, line, &buf_payload);
92 strbuf_release(&buf_payload);
93}
94
95static void fn_exit_fl(const char *file, int line, uint64_t us_elapsed_absolute,
96 int code)
97{
98 struct strbuf buf_payload = STRBUF_INIT;
99 double elapsed = (double)us_elapsed_absolute / 1000000.0;
100
101 strbuf_addf(&buf_payload, "exit elapsed:%.6f code:%d", elapsed, code);
102 normal_io_write_fl(file, line, &buf_payload);
103 strbuf_release(&buf_payload);
104}
105
106static void fn_signal(uint64_t us_elapsed_absolute, int signo)
107{
108 struct strbuf buf_payload = STRBUF_INIT;
109 double elapsed = (double)us_elapsed_absolute / 1000000.0;
110
111 strbuf_addf(&buf_payload, "signal elapsed:%.6f code:%d", elapsed,
112 signo);
113 normal_io_write_fl(__FILE__, __LINE__, &buf_payload);
114 strbuf_release(&buf_payload);
115}
116
117static void fn_atexit(uint64_t us_elapsed_absolute, int code)
118{
119 struct strbuf buf_payload = STRBUF_INIT;
120 double elapsed = (double)us_elapsed_absolute / 1000000.0;
121
122 strbuf_addf(&buf_payload, "atexit elapsed:%.6f code:%d", elapsed, code);
123 normal_io_write_fl(__FILE__, __LINE__, &buf_payload);
124 strbuf_release(&buf_payload);
125}
126
127static void maybe_append_string_va(struct strbuf *buf, const char *fmt,
128 va_list ap)
129{
ad006fe4 130 if (fmt && *fmt) {
ee4512ed
JH
131 va_list copy_ap;
132
133 va_copy(copy_ap, ap);
134 strbuf_vaddf(buf, fmt, copy_ap);
135 va_end(copy_ap);
136 return;
137 }
ee4512ed
JH
138}
139
140static void fn_error_va_fl(const char *file, int line, const char *fmt,
141 va_list ap)
142{
143 struct strbuf buf_payload = STRBUF_INIT;
144
ad43e378
JH
145 strbuf_addstr(&buf_payload, "error");
146 if (fmt && *fmt) {
147 strbuf_addch(&buf_payload, ' ');
148 maybe_append_string_va(&buf_payload, fmt, ap);
149 }
ee4512ed
JH
150 normal_io_write_fl(file, line, &buf_payload);
151 strbuf_release(&buf_payload);
152}
153
154static void fn_command_path_fl(const char *file, int line, const char *pathname)
155{
156 struct strbuf buf_payload = STRBUF_INIT;
157
158 strbuf_addf(&buf_payload, "cmd_path %s", pathname);
159 normal_io_write_fl(file, line, &buf_payload);
160 strbuf_release(&buf_payload);
161}
162
2f732bf1
ES
163static void fn_command_ancestry_fl(const char *file, int line, const char **parent_names)
164{
165 const char *parent_name = NULL;
166 struct strbuf buf_payload = STRBUF_INIT;
167
168 /* cmd_ancestry parent <- grandparent <- great-grandparent */
169 strbuf_addstr(&buf_payload, "cmd_ancestry ");
170 while ((parent_name = *parent_names++)) {
171 strbuf_addstr(&buf_payload, parent_name);
172 /* if we'll write another one after this, add a delimiter */
173 if (parent_names && *parent_names)
174 strbuf_addstr(&buf_payload, " <- ");
175 }
176
177 normal_io_write_fl(file, line, &buf_payload);
178 strbuf_release(&buf_payload);
179}
180
ee4512ed
JH
181static void fn_command_name_fl(const char *file, int line, const char *name,
182 const char *hierarchy)
183{
184 struct strbuf buf_payload = STRBUF_INIT;
185
186 strbuf_addf(&buf_payload, "cmd_name %s", name);
187 if (hierarchy && *hierarchy)
188 strbuf_addf(&buf_payload, " (%s)", hierarchy);
189 normal_io_write_fl(file, line, &buf_payload);
190 strbuf_release(&buf_payload);
191}
192
193static void fn_command_mode_fl(const char *file, int line, const char *mode)
194{
195 struct strbuf buf_payload = STRBUF_INIT;
196
197 strbuf_addf(&buf_payload, "cmd_mode %s", mode);
198 normal_io_write_fl(file, line, &buf_payload);
199 strbuf_release(&buf_payload);
200}
201
202static void fn_alias_fl(const char *file, int line, const char *alias,
203 const char **argv)
204{
205 struct strbuf buf_payload = STRBUF_INIT;
206
e3443055
JH
207 strbuf_addf(&buf_payload, "alias %s -> ", alias);
208 sq_append_quote_argv_pretty(&buf_payload, argv);
ee4512ed
JH
209 normal_io_write_fl(file, line, &buf_payload);
210 strbuf_release(&buf_payload);
211}
212
213static void fn_child_start_fl(const char *file, int line,
214 uint64_t us_elapsed_absolute,
215 const struct child_process *cmd)
216{
217 struct strbuf buf_payload = STRBUF_INIT;
218
e3443055 219 strbuf_addf(&buf_payload, "child_start[%d]", cmd->trace2_child_id);
ee4512ed
JH
220
221 if (cmd->dir) {
e3443055 222 strbuf_addstr(&buf_payload, " cd ");
ee4512ed 223 sq_quote_buf_pretty(&buf_payload, cmd->dir);
e3443055 224 strbuf_addstr(&buf_payload, ";");
ee4512ed
JH
225 }
226
227 /*
228 * TODO if (cmd->env) { Consider dumping changes to environment. }
229 * See trace_add_env() in run-command.c as used by original trace.c
230 */
231
e3443055 232 strbuf_addch(&buf_payload, ' ');
ee4512ed 233 if (cmd->git_cmd)
e3443055 234 strbuf_addstr(&buf_payload, "git ");
d3b21597 235 sq_append_quote_argv_pretty(&buf_payload, cmd->args.v);
ee4512ed
JH
236
237 normal_io_write_fl(file, line, &buf_payload);
238 strbuf_release(&buf_payload);
239}
240
241static void fn_child_exit_fl(const char *file, int line,
242 uint64_t us_elapsed_absolute, int cid, int pid,
243 int code, uint64_t us_elapsed_child)
244{
245 struct strbuf buf_payload = STRBUF_INIT;
246 double elapsed = (double)us_elapsed_child / 1000000.0;
247
248 strbuf_addf(&buf_payload, "child_exit[%d] pid:%d code:%d elapsed:%.6f",
249 cid, pid, code, elapsed);
250 normal_io_write_fl(file, line, &buf_payload);
251 strbuf_release(&buf_payload);
252}
253
64bc7524
JH
254static void fn_child_ready_fl(const char *file, int line,
255 uint64_t us_elapsed_absolute, int cid, int pid,
256 const char *ready, uint64_t us_elapsed_child)
257{
258 struct strbuf buf_payload = STRBUF_INIT;
259 double elapsed = (double)us_elapsed_child / 1000000.0;
260
261 strbuf_addf(&buf_payload, "child_ready[%d] pid:%d ready:%s elapsed:%.6f",
262 cid, pid, ready, elapsed);
263 normal_io_write_fl(file, line, &buf_payload);
264 strbuf_release(&buf_payload);
265}
266
ee4512ed
JH
267static void fn_exec_fl(const char *file, int line, uint64_t us_elapsed_absolute,
268 int exec_id, const char *exe, const char **argv)
269{
270 struct strbuf buf_payload = STRBUF_INIT;
271
272 strbuf_addf(&buf_payload, "exec[%d] ", exec_id);
e3443055 273 if (exe) {
ee4512ed 274 strbuf_addstr(&buf_payload, exe);
e3443055
JH
275 strbuf_addch(&buf_payload, ' ');
276 }
277 sq_append_quote_argv_pretty(&buf_payload, argv);
ee4512ed
JH
278 normal_io_write_fl(file, line, &buf_payload);
279 strbuf_release(&buf_payload);
280}
281
282static void fn_exec_result_fl(const char *file, int line,
283 uint64_t us_elapsed_absolute, int exec_id,
284 int code)
285{
286 struct strbuf buf_payload = STRBUF_INIT;
287
288 strbuf_addf(&buf_payload, "exec_result[%d] code:%d", exec_id, code);
289 if (code > 0)
290 strbuf_addf(&buf_payload, " err:%s", strerror(code));
291 normal_io_write_fl(file, line, &buf_payload);
292 strbuf_release(&buf_payload);
293}
294
295static void fn_param_fl(const char *file, int line, const char *param,
296 const char *value)
297{
298 struct strbuf buf_payload = STRBUF_INIT;
299
300 strbuf_addf(&buf_payload, "def_param %s=%s", param, value);
301 normal_io_write_fl(file, line, &buf_payload);
302 strbuf_release(&buf_payload);
303}
304
305static void fn_repo_fl(const char *file, int line,
306 const struct repository *repo)
307{
308 struct strbuf buf_payload = STRBUF_INIT;
309
310 strbuf_addstr(&buf_payload, "worktree ");
311 sq_quote_buf_pretty(&buf_payload, repo->worktree);
312 normal_io_write_fl(file, line, &buf_payload);
313 strbuf_release(&buf_payload);
314}
315
316static void fn_printf_va_fl(const char *file, int line,
317 uint64_t us_elapsed_absolute, const char *fmt,
318 va_list ap)
319{
320 struct strbuf buf_payload = STRBUF_INIT;
321
322 maybe_append_string_va(&buf_payload, fmt, ap);
323 normal_io_write_fl(file, line, &buf_payload);
324 strbuf_release(&buf_payload);
325}
326
327struct tr2_tgt tr2_tgt_normal = {
328 &tr2dst_normal,
329
330 fn_init,
331 fn_term,
332
333 fn_version_fl,
334 fn_start_fl,
335 fn_exit_fl,
336 fn_signal,
337 fn_atexit,
338 fn_error_va_fl,
339 fn_command_path_fl,
2f732bf1 340 fn_command_ancestry_fl,
ee4512ed
JH
341 fn_command_name_fl,
342 fn_command_mode_fl,
343 fn_alias_fl,
344 fn_child_start_fl,
345 fn_child_exit_fl,
64bc7524 346 fn_child_ready_fl,
ee4512ed
JH
347 NULL, /* thread_start */
348 NULL, /* thread_exit */
349 fn_exec_fl,
350 fn_exec_result_fl,
351 fn_param_fl,
352 fn_repo_fl,
353 NULL, /* region_enter */
354 NULL, /* region_leave */
355 NULL, /* data */
356 NULL, /* data_json */
357 fn_printf_va_fl,
358};