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