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