]>
Commit | Line | Data |
---|---|---|
15db4e7f | 1 | #include "git-compat-util.h" |
ee4512ed | 2 | #include "config.h" |
d1cbe1e6 | 3 | #include "repository.h" |
ee4512ed | 4 | #include "run-command.h" |
0a4d5b97 | 5 | #include "strbuf.h" |
ee4512ed JH |
6 | #include "quote.h" |
7 | #include "version.h" | |
8 | #include "trace2/tr2_dst.h" | |
bce9db6d | 9 | #include "trace2/tr2_sysenv.h" |
ee4512ed JH |
10 | #include "trace2/tr2_tbuf.h" |
11 | #include "trace2/tr2_tgt.h" | |
12 | #include "trace2/tr2_tls.h" | |
8ad57564 | 13 | #include "trace2/tr2_tmr.h" |
ee4512ed | 14 | |
4996e0b0 ÆAB |
15 | static struct tr2_dst tr2dst_normal = { |
16 | .sysenv_var = TR2_SYSENV_NORMAL, | |
17 | }; | |
ee4512ed JH |
18 | |
19 | /* | |
bce9db6d | 20 | * Use the TR2_SYSENV_NORMAL_BRIEF setting to omit the "<time> <file>:<line>" |
ee4512ed JH |
21 | * fields from each line written to the builtin normal target. |
22 | * | |
23 | * Unit tests may want to use this to help with testing. | |
24 | */ | |
bce9db6d | 25 | static int tr2env_normal_be_brief; |
ee4512ed JH |
26 | |
27 | #define TR2FMT_NORMAL_FL_WIDTH (50) | |
28 | ||
29 | static int fn_init(void) | |
30 | { | |
31 | int want = tr2_dst_trace_want(&tr2dst_normal); | |
32 | int want_brief; | |
bce9db6d | 33 | const char *brief; |
ee4512ed JH |
34 | |
35 | if (!want) | |
36 | return want; | |
37 | ||
bce9db6d | 38 | brief = tr2_sysenv_get(TR2_SYSENV_NORMAL_BRIEF); |
ee4512ed JH |
39 | if (brief && *brief && |
40 | ((want_brief = git_parse_maybe_bool(brief)) != -1)) | |
bce9db6d | 41 | tr2env_normal_be_brief = want_brief; |
ee4512ed JH |
42 | |
43 | return want; | |
44 | } | |
45 | ||
46 | static void fn_term(void) | |
47 | { | |
48 | tr2_dst_trace_disable(&tr2dst_normal); | |
49 | } | |
50 | ||
51 | static void normal_fmt_prepare(const char *file, int line, struct strbuf *buf) | |
52 | { | |
53 | strbuf_setlen(buf, 0); | |
54 | ||
bce9db6d | 55 | if (!tr2env_normal_be_brief) { |
ee4512ed JH |
56 | struct tr2_tbuf tb_now; |
57 | ||
58 | tr2_tbuf_local_time(&tb_now); | |
59 | strbuf_addstr(buf, tb_now.buf); | |
60 | strbuf_addch(buf, ' '); | |
61 | ||
62 | if (file && *file) | |
63 | strbuf_addf(buf, "%s:%d ", file, line); | |
64 | while (buf->len < TR2FMT_NORMAL_FL_WIDTH) | |
65 | strbuf_addch(buf, ' '); | |
66 | } | |
67 | } | |
68 | ||
69 | static void normal_io_write_fl(const char *file, int line, | |
70 | const struct strbuf *buf_payload) | |
71 | { | |
72 | struct strbuf buf_line = STRBUF_INIT; | |
73 | ||
74 | normal_fmt_prepare(file, line, &buf_line); | |
75 | strbuf_addbuf(&buf_line, buf_payload); | |
76 | tr2_dst_write_line(&tr2dst_normal, &buf_line); | |
77 | strbuf_release(&buf_line); | |
78 | } | |
79 | ||
80 | static void fn_version_fl(const char *file, int line) | |
81 | { | |
82 | struct strbuf buf_payload = STRBUF_INIT; | |
83 | ||
84 | strbuf_addf(&buf_payload, "version %s", git_version_string); | |
85 | normal_io_write_fl(file, line, &buf_payload); | |
86 | strbuf_release(&buf_payload); | |
87 | } | |
88 | ||
39f43177 | 89 | static void fn_start_fl(const char *file, int line, |
e46a25b0 | 90 | uint64_t us_elapsed_absolute UNUSED, const char **argv) |
ee4512ed JH |
91 | { |
92 | struct strbuf buf_payload = STRBUF_INIT; | |
93 | ||
94 | strbuf_addstr(&buf_payload, "start "); | |
e3443055 | 95 | sq_append_quote_argv_pretty(&buf_payload, argv); |
ee4512ed JH |
96 | normal_io_write_fl(file, line, &buf_payload); |
97 | strbuf_release(&buf_payload); | |
98 | } | |
99 | ||
100 | static void fn_exit_fl(const char *file, int line, uint64_t us_elapsed_absolute, | |
101 | int code) | |
102 | { | |
103 | struct strbuf buf_payload = STRBUF_INIT; | |
104 | double elapsed = (double)us_elapsed_absolute / 1000000.0; | |
105 | ||
106 | strbuf_addf(&buf_payload, "exit elapsed:%.6f code:%d", elapsed, code); | |
107 | normal_io_write_fl(file, line, &buf_payload); | |
108 | strbuf_release(&buf_payload); | |
109 | } | |
110 | ||
111 | static void fn_signal(uint64_t us_elapsed_absolute, int signo) | |
112 | { | |
113 | struct strbuf buf_payload = STRBUF_INIT; | |
114 | double elapsed = (double)us_elapsed_absolute / 1000000.0; | |
115 | ||
116 | strbuf_addf(&buf_payload, "signal elapsed:%.6f code:%d", elapsed, | |
117 | signo); | |
118 | normal_io_write_fl(__FILE__, __LINE__, &buf_payload); | |
119 | strbuf_release(&buf_payload); | |
120 | } | |
121 | ||
122 | static void fn_atexit(uint64_t us_elapsed_absolute, int code) | |
123 | { | |
124 | struct strbuf buf_payload = STRBUF_INIT; | |
125 | double elapsed = (double)us_elapsed_absolute / 1000000.0; | |
126 | ||
127 | strbuf_addf(&buf_payload, "atexit elapsed:%.6f code:%d", elapsed, code); | |
128 | normal_io_write_fl(__FILE__, __LINE__, &buf_payload); | |
129 | strbuf_release(&buf_payload); | |
130 | } | |
131 | ||
132 | static void maybe_append_string_va(struct strbuf *buf, const char *fmt, | |
133 | va_list ap) | |
134 | { | |
ad006fe4 | 135 | if (fmt && *fmt) { |
ee4512ed JH |
136 | va_list copy_ap; |
137 | ||
138 | va_copy(copy_ap, ap); | |
139 | strbuf_vaddf(buf, fmt, copy_ap); | |
140 | va_end(copy_ap); | |
141 | return; | |
142 | } | |
ee4512ed JH |
143 | } |
144 | ||
145 | static void fn_error_va_fl(const char *file, int line, const char *fmt, | |
146 | va_list ap) | |
147 | { | |
148 | struct strbuf buf_payload = STRBUF_INIT; | |
149 | ||
ad43e378 JH |
150 | strbuf_addstr(&buf_payload, "error"); |
151 | if (fmt && *fmt) { | |
152 | strbuf_addch(&buf_payload, ' '); | |
153 | maybe_append_string_va(&buf_payload, fmt, ap); | |
154 | } | |
ee4512ed JH |
155 | normal_io_write_fl(file, line, &buf_payload); |
156 | strbuf_release(&buf_payload); | |
157 | } | |
158 | ||
159 | static void fn_command_path_fl(const char *file, int line, const char *pathname) | |
160 | { | |
161 | struct strbuf buf_payload = STRBUF_INIT; | |
162 | ||
163 | strbuf_addf(&buf_payload, "cmd_path %s", pathname); | |
164 | normal_io_write_fl(file, line, &buf_payload); | |
165 | strbuf_release(&buf_payload); | |
166 | } | |
167 | ||
2f732bf1 ES |
168 | static void fn_command_ancestry_fl(const char *file, int line, const char **parent_names) |
169 | { | |
170 | const char *parent_name = NULL; | |
171 | struct strbuf buf_payload = STRBUF_INIT; | |
172 | ||
173 | /* cmd_ancestry parent <- grandparent <- great-grandparent */ | |
174 | strbuf_addstr(&buf_payload, "cmd_ancestry "); | |
175 | while ((parent_name = *parent_names++)) { | |
176 | strbuf_addstr(&buf_payload, parent_name); | |
177 | /* if we'll write another one after this, add a delimiter */ | |
178 | if (parent_names && *parent_names) | |
179 | strbuf_addstr(&buf_payload, " <- "); | |
180 | } | |
181 | ||
182 | normal_io_write_fl(file, line, &buf_payload); | |
183 | strbuf_release(&buf_payload); | |
184 | } | |
185 | ||
ee4512ed JH |
186 | static void fn_command_name_fl(const char *file, int line, const char *name, |
187 | const char *hierarchy) | |
188 | { | |
189 | struct strbuf buf_payload = STRBUF_INIT; | |
190 | ||
191 | strbuf_addf(&buf_payload, "cmd_name %s", name); | |
192 | if (hierarchy && *hierarchy) | |
193 | strbuf_addf(&buf_payload, " (%s)", hierarchy); | |
194 | normal_io_write_fl(file, line, &buf_payload); | |
195 | strbuf_release(&buf_payload); | |
196 | } | |
197 | ||
198 | static void fn_command_mode_fl(const char *file, int line, const char *mode) | |
199 | { | |
200 | struct strbuf buf_payload = STRBUF_INIT; | |
201 | ||
202 | strbuf_addf(&buf_payload, "cmd_mode %s", mode); | |
203 | normal_io_write_fl(file, line, &buf_payload); | |
204 | strbuf_release(&buf_payload); | |
205 | } | |
206 | ||
207 | static void fn_alias_fl(const char *file, int line, const char *alias, | |
208 | const char **argv) | |
209 | { | |
210 | struct strbuf buf_payload = STRBUF_INIT; | |
211 | ||
e3443055 JH |
212 | strbuf_addf(&buf_payload, "alias %s -> ", alias); |
213 | sq_append_quote_argv_pretty(&buf_payload, argv); | |
ee4512ed JH |
214 | normal_io_write_fl(file, line, &buf_payload); |
215 | strbuf_release(&buf_payload); | |
216 | } | |
217 | ||
218 | static void fn_child_start_fl(const char *file, int line, | |
e46a25b0 | 219 | uint64_t us_elapsed_absolute UNUSED, |
ee4512ed JH |
220 | const struct child_process *cmd) |
221 | { | |
222 | struct strbuf buf_payload = STRBUF_INIT; | |
223 | ||
e3443055 | 224 | strbuf_addf(&buf_payload, "child_start[%d]", cmd->trace2_child_id); |
ee4512ed JH |
225 | |
226 | if (cmd->dir) { | |
e3443055 | 227 | strbuf_addstr(&buf_payload, " cd "); |
ee4512ed | 228 | sq_quote_buf_pretty(&buf_payload, cmd->dir); |
e3443055 | 229 | strbuf_addstr(&buf_payload, ";"); |
ee4512ed JH |
230 | } |
231 | ||
232 | /* | |
233 | * TODO if (cmd->env) { Consider dumping changes to environment. } | |
234 | * See trace_add_env() in run-command.c as used by original trace.c | |
235 | */ | |
236 | ||
e3443055 | 237 | strbuf_addch(&buf_payload, ' '); |
ee4512ed | 238 | if (cmd->git_cmd) |
e3443055 | 239 | strbuf_addstr(&buf_payload, "git "); |
d3b21597 | 240 | sq_append_quote_argv_pretty(&buf_payload, cmd->args.v); |
ee4512ed JH |
241 | |
242 | normal_io_write_fl(file, line, &buf_payload); | |
243 | strbuf_release(&buf_payload); | |
244 | } | |
245 | ||
246 | static void fn_child_exit_fl(const char *file, int line, | |
e46a25b0 JK |
247 | uint64_t us_elapsed_absolute UNUSED, |
248 | int cid, int pid, | |
ee4512ed JH |
249 | int code, uint64_t us_elapsed_child) |
250 | { | |
251 | struct strbuf buf_payload = STRBUF_INIT; | |
252 | double elapsed = (double)us_elapsed_child / 1000000.0; | |
253 | ||
254 | strbuf_addf(&buf_payload, "child_exit[%d] pid:%d code:%d elapsed:%.6f", | |
255 | cid, pid, code, elapsed); | |
256 | normal_io_write_fl(file, line, &buf_payload); | |
257 | strbuf_release(&buf_payload); | |
258 | } | |
259 | ||
64bc7524 | 260 | static void fn_child_ready_fl(const char *file, int line, |
e46a25b0 JK |
261 | uint64_t us_elapsed_absolute UNUSED, |
262 | int cid, int pid, | |
64bc7524 JH |
263 | const char *ready, uint64_t us_elapsed_child) |
264 | { | |
265 | struct strbuf buf_payload = STRBUF_INIT; | |
266 | double elapsed = (double)us_elapsed_child / 1000000.0; | |
267 | ||
268 | strbuf_addf(&buf_payload, "child_ready[%d] pid:%d ready:%s elapsed:%.6f", | |
269 | cid, pid, ready, elapsed); | |
270 | normal_io_write_fl(file, line, &buf_payload); | |
271 | strbuf_release(&buf_payload); | |
272 | } | |
273 | ||
e46a25b0 JK |
274 | static void fn_exec_fl(const char *file, int line, |
275 | uint64_t us_elapsed_absolute UNUSED, | |
ee4512ed JH |
276 | int exec_id, const char *exe, const char **argv) |
277 | { | |
278 | struct strbuf buf_payload = STRBUF_INIT; | |
279 | ||
280 | strbuf_addf(&buf_payload, "exec[%d] ", exec_id); | |
e3443055 | 281 | if (exe) { |
ee4512ed | 282 | strbuf_addstr(&buf_payload, exe); |
e3443055 JH |
283 | strbuf_addch(&buf_payload, ' '); |
284 | } | |
285 | sq_append_quote_argv_pretty(&buf_payload, argv); | |
ee4512ed JH |
286 | normal_io_write_fl(file, line, &buf_payload); |
287 | strbuf_release(&buf_payload); | |
288 | } | |
289 | ||
290 | static void fn_exec_result_fl(const char *file, int line, | |
e46a25b0 JK |
291 | uint64_t us_elapsed_absolute UNUSED, |
292 | int exec_id, int code) | |
ee4512ed JH |
293 | { |
294 | struct strbuf buf_payload = STRBUF_INIT; | |
295 | ||
296 | strbuf_addf(&buf_payload, "exec_result[%d] code:%d", exec_id, code); | |
297 | if (code > 0) | |
298 | strbuf_addf(&buf_payload, " err:%s", strerror(code)); | |
299 | normal_io_write_fl(file, line, &buf_payload); | |
300 | strbuf_release(&buf_payload); | |
301 | } | |
302 | ||
303 | static void fn_param_fl(const char *file, int line, const char *param, | |
dc902084 | 304 | const char *value, const struct key_value_info *kvi) |
ee4512ed JH |
305 | { |
306 | struct strbuf buf_payload = STRBUF_INIT; | |
dc902084 | 307 | enum config_scope scope = kvi->scope; |
35ae40ea | 308 | const char *scope_name = config_scope_name(scope); |
ee4512ed | 309 | |
35ae40ea TL |
310 | strbuf_addf(&buf_payload, "def_param scope:%s %s=%s", scope_name, param, |
311 | value); | |
ee4512ed JH |
312 | normal_io_write_fl(file, line, &buf_payload); |
313 | strbuf_release(&buf_payload); | |
314 | } | |
315 | ||
316 | static void fn_repo_fl(const char *file, int line, | |
317 | const struct repository *repo) | |
318 | { | |
319 | struct strbuf buf_payload = STRBUF_INIT; | |
320 | ||
321 | strbuf_addstr(&buf_payload, "worktree "); | |
322 | sq_quote_buf_pretty(&buf_payload, repo->worktree); | |
323 | normal_io_write_fl(file, line, &buf_payload); | |
324 | strbuf_release(&buf_payload); | |
325 | } | |
326 | ||
327 | static void fn_printf_va_fl(const char *file, int line, | |
e46a25b0 JK |
328 | uint64_t us_elapsed_absolute UNUSED, |
329 | const char *fmt, | |
ee4512ed JH |
330 | va_list ap) |
331 | { | |
332 | struct strbuf buf_payload = STRBUF_INIT; | |
333 | ||
334 | maybe_append_string_va(&buf_payload, fmt, ap); | |
335 | normal_io_write_fl(file, line, &buf_payload); | |
336 | strbuf_release(&buf_payload); | |
337 | } | |
338 | ||
8ad57564 JH |
339 | static void fn_timer(const struct tr2_timer_metadata *meta, |
340 | const struct tr2_timer *timer, | |
341 | int is_final_data) | |
342 | { | |
343 | const char *event_name = is_final_data ? "timer" : "th_timer"; | |
344 | struct strbuf buf_payload = STRBUF_INIT; | |
345 | double t_total = NS_TO_SEC(timer->total_ns); | |
346 | double t_min = NS_TO_SEC(timer->min_ns); | |
347 | double t_max = NS_TO_SEC(timer->max_ns); | |
348 | ||
349 | strbuf_addf(&buf_payload, ("%s %s/%s" | |
350 | " intervals:%"PRIu64 | |
351 | " total:%8.6f min:%8.6f max:%8.6f"), | |
352 | event_name, meta->category, meta->name, | |
353 | timer->interval_count, | |
354 | t_total, t_min, t_max); | |
355 | ||
356 | normal_io_write_fl(__FILE__, __LINE__, &buf_payload); | |
357 | strbuf_release(&buf_payload); | |
358 | } | |
359 | ||
81071626 JH |
360 | static void fn_counter(const struct tr2_counter_metadata *meta, |
361 | const struct tr2_counter *counter, | |
362 | int is_final_data) | |
363 | { | |
364 | const char *event_name = is_final_data ? "counter" : "th_counter"; | |
365 | struct strbuf buf_payload = STRBUF_INIT; | |
366 | ||
367 | strbuf_addf(&buf_payload, "%s %s/%s value:%"PRIu64, | |
368 | event_name, meta->category, meta->name, | |
369 | counter->value); | |
370 | ||
371 | normal_io_write_fl(__FILE__, __LINE__, &buf_payload); | |
372 | strbuf_release(&buf_payload); | |
373 | } | |
374 | ||
ee4512ed | 375 | struct tr2_tgt tr2_tgt_normal = { |
98593057 ÆAB |
376 | .pdst = &tr2dst_normal, |
377 | ||
378 | .pfn_init = fn_init, | |
379 | .pfn_term = fn_term, | |
380 | ||
381 | .pfn_version_fl = fn_version_fl, | |
382 | .pfn_start_fl = fn_start_fl, | |
383 | .pfn_exit_fl = fn_exit_fl, | |
384 | .pfn_signal = fn_signal, | |
385 | .pfn_atexit = fn_atexit, | |
386 | .pfn_error_va_fl = fn_error_va_fl, | |
387 | .pfn_command_path_fl = fn_command_path_fl, | |
388 | .pfn_command_ancestry_fl = fn_command_ancestry_fl, | |
389 | .pfn_command_name_fl = fn_command_name_fl, | |
390 | .pfn_command_mode_fl = fn_command_mode_fl, | |
391 | .pfn_alias_fl = fn_alias_fl, | |
392 | .pfn_child_start_fl = fn_child_start_fl, | |
393 | .pfn_child_exit_fl = fn_child_exit_fl, | |
394 | .pfn_child_ready_fl = fn_child_ready_fl, | |
395 | .pfn_thread_start_fl = NULL, | |
396 | .pfn_thread_exit_fl = NULL, | |
397 | .pfn_exec_fl = fn_exec_fl, | |
398 | .pfn_exec_result_fl = fn_exec_result_fl, | |
399 | .pfn_param_fl = fn_param_fl, | |
400 | .pfn_repo_fl = fn_repo_fl, | |
401 | .pfn_region_enter_printf_va_fl = NULL, | |
402 | .pfn_region_leave_printf_va_fl = NULL, | |
403 | .pfn_data_fl = NULL, | |
404 | .pfn_data_json_fl = NULL, | |
405 | .pfn_printf_va_fl = fn_printf_va_fl, | |
8ad57564 | 406 | .pfn_timer = fn_timer, |
81071626 | 407 | .pfn_counter = fn_counter, |
ee4512ed | 408 | }; |