refspec: store raw refspecs inside refspec_item
[git/gitster.git] / trace2 / tr2_tgt_normal.c
blobbaef48aa6989cede1d8bf62837388ab3379e9604
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "repository.h"
4 #include "run-command.h"
5 #include "strbuf.h"
6 #include "quote.h"
7 #include "version.h"
8 #include "trace2/tr2_dst.h"
9 #include "trace2/tr2_sysenv.h"
10 #include "trace2/tr2_tbuf.h"
11 #include "trace2/tr2_tgt.h"
12 #include "trace2/tr2_tls.h"
13 #include "trace2/tr2_tmr.h"
15 static struct tr2_dst tr2dst_normal = {
16 .sysenv_var = TR2_SYSENV_NORMAL,
20 * Use the TR2_SYSENV_NORMAL_BRIEF setting to omit the "<time> <file>:<line>"
21 * fields from each line written to the builtin normal target.
23 * Unit tests may want to use this to help with testing.
25 static int tr2env_normal_be_brief;
27 #define TR2FMT_NORMAL_FL_WIDTH (50)
29 static int fn_init(void)
31 int want = tr2_dst_trace_want(&tr2dst_normal);
32 int want_brief;
33 const char *brief;
35 if (!want)
36 return want;
38 brief = tr2_sysenv_get(TR2_SYSENV_NORMAL_BRIEF);
39 if (brief && *brief &&
40 ((want_brief = git_parse_maybe_bool(brief)) != -1))
41 tr2env_normal_be_brief = want_brief;
43 return want;
46 static void fn_term(void)
48 tr2_dst_trace_disable(&tr2dst_normal);
51 static void normal_fmt_prepare(const char *file, int line, struct strbuf *buf)
53 strbuf_setlen(buf, 0);
55 if (!tr2env_normal_be_brief) {
56 struct tr2_tbuf tb_now;
58 tr2_tbuf_local_time(&tb_now);
59 strbuf_addstr(buf, tb_now.buf);
60 strbuf_addch(buf, ' ');
62 if (file && *file)
63 strbuf_addf(buf, "%s:%d ", file, line);
64 while (buf->len < TR2FMT_NORMAL_FL_WIDTH)
65 strbuf_addch(buf, ' ');
69 static void normal_io_write_fl(const char *file, int line,
70 const struct strbuf *buf_payload)
72 struct strbuf buf_line = STRBUF_INIT;
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);
80 static void fn_version_fl(const char *file, int line)
82 struct strbuf buf_payload = STRBUF_INIT;
84 strbuf_addf(&buf_payload, "version %s", git_version_string);
85 normal_io_write_fl(file, line, &buf_payload);
86 strbuf_release(&buf_payload);
89 static void fn_start_fl(const char *file, int line,
90 uint64_t us_elapsed_absolute UNUSED, const char **argv)
92 struct strbuf buf_payload = STRBUF_INIT;
94 strbuf_addstr(&buf_payload, "start ");
95 sq_append_quote_argv_pretty(&buf_payload, argv);
96 normal_io_write_fl(file, line, &buf_payload);
97 strbuf_release(&buf_payload);
100 static void fn_exit_fl(const char *file, int line, uint64_t us_elapsed_absolute,
101 int code)
103 struct strbuf buf_payload = STRBUF_INIT;
104 double elapsed = (double)us_elapsed_absolute / 1000000.0;
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);
111 static void fn_signal(uint64_t us_elapsed_absolute, int signo)
113 struct strbuf buf_payload = STRBUF_INIT;
114 double elapsed = (double)us_elapsed_absolute / 1000000.0;
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);
122 static void fn_atexit(uint64_t us_elapsed_absolute, int code)
124 struct strbuf buf_payload = STRBUF_INIT;
125 double elapsed = (double)us_elapsed_absolute / 1000000.0;
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);
132 static void maybe_append_string_va(struct strbuf *buf, const char *fmt,
133 va_list ap)
135 if (fmt && *fmt) {
136 va_list copy_ap;
138 va_copy(copy_ap, ap);
139 strbuf_vaddf(buf, fmt, copy_ap);
140 va_end(copy_ap);
141 return;
145 static void fn_error_va_fl(const char *file, int line, const char *fmt,
146 va_list ap)
148 struct strbuf buf_payload = STRBUF_INIT;
150 strbuf_addstr(&buf_payload, "error");
151 if (fmt && *fmt) {
152 strbuf_addch(&buf_payload, ' ');
153 maybe_append_string_va(&buf_payload, fmt, ap);
155 normal_io_write_fl(file, line, &buf_payload);
156 strbuf_release(&buf_payload);
159 static void fn_command_path_fl(const char *file, int line, const char *pathname)
161 struct strbuf buf_payload = STRBUF_INIT;
163 strbuf_addf(&buf_payload, "cmd_path %s", pathname);
164 normal_io_write_fl(file, line, &buf_payload);
165 strbuf_release(&buf_payload);
168 static void fn_command_ancestry_fl(const char *file, int line, const char **parent_names)
170 const char *parent_name = NULL;
171 struct strbuf buf_payload = STRBUF_INIT;
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, " <- ");
182 normal_io_write_fl(file, line, &buf_payload);
183 strbuf_release(&buf_payload);
186 static void fn_command_name_fl(const char *file, int line, const char *name,
187 const char *hierarchy)
189 struct strbuf buf_payload = STRBUF_INIT;
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);
198 static void fn_command_mode_fl(const char *file, int line, const char *mode)
200 struct strbuf buf_payload = STRBUF_INIT;
202 strbuf_addf(&buf_payload, "cmd_mode %s", mode);
203 normal_io_write_fl(file, line, &buf_payload);
204 strbuf_release(&buf_payload);
207 static void fn_alias_fl(const char *file, int line, const char *alias,
208 const char **argv)
210 struct strbuf buf_payload = STRBUF_INIT;
212 strbuf_addf(&buf_payload, "alias %s -> ", alias);
213 sq_append_quote_argv_pretty(&buf_payload, argv);
214 normal_io_write_fl(file, line, &buf_payload);
215 strbuf_release(&buf_payload);
218 static void fn_child_start_fl(const char *file, int line,
219 uint64_t us_elapsed_absolute UNUSED,
220 const struct child_process *cmd)
222 struct strbuf buf_payload = STRBUF_INIT;
224 strbuf_addf(&buf_payload, "child_start[%d]", cmd->trace2_child_id);
226 if (cmd->dir) {
227 strbuf_addstr(&buf_payload, " cd ");
228 sq_quote_buf_pretty(&buf_payload, cmd->dir);
229 strbuf_addstr(&buf_payload, ";");
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
237 strbuf_addch(&buf_payload, ' ');
238 if (cmd->git_cmd)
239 strbuf_addstr(&buf_payload, "git ");
240 sq_append_quote_argv_pretty(&buf_payload, cmd->args.v);
242 normal_io_write_fl(file, line, &buf_payload);
243 strbuf_release(&buf_payload);
246 static void fn_child_exit_fl(const char *file, int line,
247 uint64_t us_elapsed_absolute UNUSED,
248 int cid, int pid,
249 int code, uint64_t us_elapsed_child)
251 struct strbuf buf_payload = STRBUF_INIT;
252 double elapsed = (double)us_elapsed_child / 1000000.0;
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);
260 static void fn_child_ready_fl(const char *file, int line,
261 uint64_t us_elapsed_absolute UNUSED,
262 int cid, int pid,
263 const char *ready, uint64_t us_elapsed_child)
265 struct strbuf buf_payload = STRBUF_INIT;
266 double elapsed = (double)us_elapsed_child / 1000000.0;
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);
274 static void fn_exec_fl(const char *file, int line,
275 uint64_t us_elapsed_absolute UNUSED,
276 int exec_id, const char *exe, const char **argv)
278 struct strbuf buf_payload = STRBUF_INIT;
280 strbuf_addf(&buf_payload, "exec[%d] ", exec_id);
281 if (exe) {
282 strbuf_addstr(&buf_payload, exe);
283 strbuf_addch(&buf_payload, ' ');
285 sq_append_quote_argv_pretty(&buf_payload, argv);
286 normal_io_write_fl(file, line, &buf_payload);
287 strbuf_release(&buf_payload);
290 static void fn_exec_result_fl(const char *file, int line,
291 uint64_t us_elapsed_absolute UNUSED,
292 int exec_id, int code)
294 struct strbuf buf_payload = STRBUF_INIT;
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);
303 static void fn_param_fl(const char *file, int line, const char *param,
304 const char *value, const struct key_value_info *kvi)
306 struct strbuf buf_payload = STRBUF_INIT;
307 enum config_scope scope = kvi->scope;
308 const char *scope_name = config_scope_name(scope);
310 strbuf_addf(&buf_payload, "def_param scope:%s %s=%s", scope_name, param,
311 value);
312 normal_io_write_fl(file, line, &buf_payload);
313 strbuf_release(&buf_payload);
316 static void fn_repo_fl(const char *file, int line,
317 const struct repository *repo)
319 struct strbuf buf_payload = STRBUF_INIT;
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);
327 static void fn_printf_va_fl(const char *file, int line,
328 uint64_t us_elapsed_absolute UNUSED,
329 const char *fmt,
330 va_list ap)
332 struct strbuf buf_payload = STRBUF_INIT;
334 maybe_append_string_va(&buf_payload, fmt, ap);
335 normal_io_write_fl(file, line, &buf_payload);
336 strbuf_release(&buf_payload);
339 static void fn_timer(const struct tr2_timer_metadata *meta,
340 const struct tr2_timer *timer,
341 int is_final_data)
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);
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);
356 normal_io_write_fl(__FILE__, __LINE__, &buf_payload);
357 strbuf_release(&buf_payload);
360 static void fn_counter(const struct tr2_counter_metadata *meta,
361 const struct tr2_counter *counter,
362 int is_final_data)
364 const char *event_name = is_final_data ? "counter" : "th_counter";
365 struct strbuf buf_payload = STRBUF_INIT;
367 strbuf_addf(&buf_payload, "%s %s/%s value:%"PRIu64,
368 event_name, meta->category, meta->name,
369 counter->value);
371 normal_io_write_fl(__FILE__, __LINE__, &buf_payload);
372 strbuf_release(&buf_payload);
375 struct tr2_tgt tr2_tgt_normal = {
376 .pdst = &tr2dst_normal,
378 .pfn_init = fn_init,
379 .pfn_term = fn_term,
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,
406 .pfn_timer = fn_timer,
407 .pfn_counter = fn_counter,