3 #include "run-command.h"
6 #include "trace2/tr2_dst.h"
7 #include "trace2/tr2_sysenv.h"
8 #include "trace2/tr2_tbuf.h"
9 #include "trace2/tr2_tgt.h"
10 #include "trace2/tr2_tls.h"
12 static struct tr2_dst tr2dst_normal
= { TR2_SYSENV_NORMAL
, 0, 0, 0 };
15 * Use the TR2_SYSENV_NORMAL_BRIEF setting to omit the "<time> <file>:<line>"
16 * fields from each line written to the builtin normal target.
18 * Unit tests may want to use this to help with testing.
20 static int tr2env_normal_be_brief
;
22 #define TR2FMT_NORMAL_FL_WIDTH (50)
24 static int fn_init(void)
26 int want
= tr2_dst_trace_want(&tr2dst_normal
);
33 brief
= tr2_sysenv_get(TR2_SYSENV_NORMAL_BRIEF
);
34 if (brief
&& *brief
&&
35 ((want_brief
= git_parse_maybe_bool(brief
)) != -1))
36 tr2env_normal_be_brief
= want_brief
;
41 static void fn_term(void)
43 tr2_dst_trace_disable(&tr2dst_normal
);
46 static void normal_fmt_prepare(const char *file
, int line
, struct strbuf
*buf
)
48 strbuf_setlen(buf
, 0);
50 if (!tr2env_normal_be_brief
) {
51 struct tr2_tbuf tb_now
;
53 tr2_tbuf_local_time(&tb_now
);
54 strbuf_addstr(buf
, tb_now
.buf
);
55 strbuf_addch(buf
, ' ');
58 strbuf_addf(buf
, "%s:%d ", file
, line
);
59 while (buf
->len
< TR2FMT_NORMAL_FL_WIDTH
)
60 strbuf_addch(buf
, ' ');
64 static void normal_io_write_fl(const char *file
, int line
,
65 const struct strbuf
*buf_payload
)
67 struct strbuf buf_line
= STRBUF_INIT
;
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
);
75 static void fn_version_fl(const char *file
, int line
)
77 struct strbuf buf_payload
= STRBUF_INIT
;
79 strbuf_addf(&buf_payload
, "version %s", git_version_string
);
80 normal_io_write_fl(file
, line
, &buf_payload
);
81 strbuf_release(&buf_payload
);
84 static void fn_start_fl(const char *file
, int line
,
85 uint64_t us_elapsed_absolute
, const char **argv
)
87 struct strbuf buf_payload
= STRBUF_INIT
;
89 strbuf_addstr(&buf_payload
, "start ");
90 sq_quote_argv_pretty(&buf_payload
, argv
);
91 normal_io_write_fl(file
, line
, &buf_payload
);
92 strbuf_release(&buf_payload
);
95 static void fn_exit_fl(const char *file
, int line
, uint64_t us_elapsed_absolute
,
98 struct strbuf buf_payload
= STRBUF_INIT
;
99 double elapsed
= (double)us_elapsed_absolute
/ 1000000.0;
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
);
106 static void fn_signal(uint64_t us_elapsed_absolute
, int signo
)
108 struct strbuf buf_payload
= STRBUF_INIT
;
109 double elapsed
= (double)us_elapsed_absolute
/ 1000000.0;
111 strbuf_addf(&buf_payload
, "signal elapsed:%.6f code:%d", elapsed
,
113 normal_io_write_fl(__FILE__
, __LINE__
, &buf_payload
);
114 strbuf_release(&buf_payload
);
117 static void fn_atexit(uint64_t us_elapsed_absolute
, int code
)
119 struct strbuf buf_payload
= STRBUF_INIT
;
120 double elapsed
= (double)us_elapsed_absolute
/ 1000000.0;
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
);
127 static void maybe_append_string_va(struct strbuf
*buf
, const char *fmt
,
133 va_copy(copy_ap
, ap
);
134 strbuf_vaddf(buf
, fmt
, copy_ap
);
140 strbuf_addstr(buf
, fmt
);
145 static void fn_error_va_fl(const char *file
, int line
, const char *fmt
,
148 struct strbuf buf_payload
= STRBUF_INIT
;
150 strbuf_addstr(&buf_payload
, "error ");
151 maybe_append_string_va(&buf_payload
, fmt
, ap
);
152 normal_io_write_fl(file
, line
, &buf_payload
);
153 strbuf_release(&buf_payload
);
156 static void fn_command_path_fl(const char *file
, int line
, const char *pathname
)
158 struct strbuf buf_payload
= STRBUF_INIT
;
160 strbuf_addf(&buf_payload
, "cmd_path %s", pathname
);
161 normal_io_write_fl(file
, line
, &buf_payload
);
162 strbuf_release(&buf_payload
);
165 static void fn_command_name_fl(const char *file
, int line
, const char *name
,
166 const char *hierarchy
)
168 struct strbuf buf_payload
= STRBUF_INIT
;
170 strbuf_addf(&buf_payload
, "cmd_name %s", name
);
171 if (hierarchy
&& *hierarchy
)
172 strbuf_addf(&buf_payload
, " (%s)", hierarchy
);
173 normal_io_write_fl(file
, line
, &buf_payload
);
174 strbuf_release(&buf_payload
);
177 static void fn_command_mode_fl(const char *file
, int line
, const char *mode
)
179 struct strbuf buf_payload
= STRBUF_INIT
;
181 strbuf_addf(&buf_payload
, "cmd_mode %s", mode
);
182 normal_io_write_fl(file
, line
, &buf_payload
);
183 strbuf_release(&buf_payload
);
186 static void fn_alias_fl(const char *file
, int line
, const char *alias
,
189 struct strbuf buf_payload
= STRBUF_INIT
;
191 strbuf_addf(&buf_payload
, "alias %s ->", alias
);
192 sq_quote_argv_pretty(&buf_payload
, argv
);
193 normal_io_write_fl(file
, line
, &buf_payload
);
194 strbuf_release(&buf_payload
);
197 static void fn_child_start_fl(const char *file
, int line
,
198 uint64_t us_elapsed_absolute
,
199 const struct child_process
*cmd
)
201 struct strbuf buf_payload
= STRBUF_INIT
;
203 strbuf_addf(&buf_payload
, "child_start[%d] ", cmd
->trace2_child_id
);
206 strbuf_addstr(&buf_payload
, " cd");
207 sq_quote_buf_pretty(&buf_payload
, cmd
->dir
);
208 strbuf_addstr(&buf_payload
, "; ");
212 * TODO if (cmd->env) { Consider dumping changes to environment. }
213 * See trace_add_env() in run-command.c as used by original trace.c
217 strbuf_addstr(&buf_payload
, "git");
218 sq_quote_argv_pretty(&buf_payload
, cmd
->argv
);
220 normal_io_write_fl(file
, line
, &buf_payload
);
221 strbuf_release(&buf_payload
);
224 static void fn_child_exit_fl(const char *file
, int line
,
225 uint64_t us_elapsed_absolute
, int cid
, int pid
,
226 int code
, uint64_t us_elapsed_child
)
228 struct strbuf buf_payload
= STRBUF_INIT
;
229 double elapsed
= (double)us_elapsed_child
/ 1000000.0;
231 strbuf_addf(&buf_payload
, "child_exit[%d] pid:%d code:%d elapsed:%.6f",
232 cid
, pid
, code
, elapsed
);
233 normal_io_write_fl(file
, line
, &buf_payload
);
234 strbuf_release(&buf_payload
);
237 static void fn_exec_fl(const char *file
, int line
, uint64_t us_elapsed_absolute
,
238 int exec_id
, const char *exe
, const char **argv
)
240 struct strbuf buf_payload
= STRBUF_INIT
;
242 strbuf_addf(&buf_payload
, "exec[%d] ", exec_id
);
244 strbuf_addstr(&buf_payload
, exe
);
245 sq_quote_argv_pretty(&buf_payload
, argv
);
246 normal_io_write_fl(file
, line
, &buf_payload
);
247 strbuf_release(&buf_payload
);
250 static void fn_exec_result_fl(const char *file
, int line
,
251 uint64_t us_elapsed_absolute
, int exec_id
,
254 struct strbuf buf_payload
= STRBUF_INIT
;
256 strbuf_addf(&buf_payload
, "exec_result[%d] code:%d", exec_id
, code
);
258 strbuf_addf(&buf_payload
, " err:%s", strerror(code
));
259 normal_io_write_fl(file
, line
, &buf_payload
);
260 strbuf_release(&buf_payload
);
263 static void fn_param_fl(const char *file
, int line
, const char *param
,
266 struct strbuf buf_payload
= STRBUF_INIT
;
268 strbuf_addf(&buf_payload
, "def_param %s=%s", param
, value
);
269 normal_io_write_fl(file
, line
, &buf_payload
);
270 strbuf_release(&buf_payload
);
273 static void fn_repo_fl(const char *file
, int line
,
274 const struct repository
*repo
)
276 struct strbuf buf_payload
= STRBUF_INIT
;
278 strbuf_addstr(&buf_payload
, "worktree ");
279 sq_quote_buf_pretty(&buf_payload
, repo
->worktree
);
280 normal_io_write_fl(file
, line
, &buf_payload
);
281 strbuf_release(&buf_payload
);
284 static void fn_printf_va_fl(const char *file
, int line
,
285 uint64_t us_elapsed_absolute
, const char *fmt
,
288 struct strbuf buf_payload
= STRBUF_INIT
;
290 maybe_append_string_va(&buf_payload
, fmt
, ap
);
291 normal_io_write_fl(file
, line
, &buf_payload
);
292 strbuf_release(&buf_payload
);
295 struct tr2_tgt tr2_tgt_normal
= {
313 NULL
, /* thread_start */
314 NULL
, /* thread_exit */
319 NULL
, /* region_enter */
320 NULL
, /* region_leave */
322 NULL
, /* data_json */