trace2 / tr2_tgt_normal.con commit trace2: remove dead code in maybe_add_string_va() (04f10d3)
   1#include "cache.h"
   2#include "config.h"
   3#include "run-command.h"
   4#include "quote.h"
   5#include "version.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"
  11
  12static struct tr2_dst tr2dst_normal = { TR2_SYSENV_NORMAL, 0, 0, 0 };
  13
  14/*
  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.
  17 *
  18 * Unit tests may want to use this to help with testing.
  19 */
  20static int tr2env_normal_be_brief;
  21
  22#define TR2FMT_NORMAL_FL_WIDTH (50)
  23
  24static int fn_init(void)
  25{
  26        int want = tr2_dst_trace_want(&tr2dst_normal);
  27        int want_brief;
  28        const char *brief;
  29
  30        if (!want)
  31                return want;
  32
  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;
  37
  38        return want;
  39}
  40
  41static void fn_term(void)
  42{
  43        tr2_dst_trace_disable(&tr2dst_normal);
  44}
  45
  46static void normal_fmt_prepare(const char *file, int line, struct strbuf *buf)
  47{
  48        strbuf_setlen(buf, 0);
  49
  50        if (!tr2env_normal_be_brief) {
  51                struct tr2_tbuf tb_now;
  52
  53                tr2_tbuf_local_time(&tb_now);
  54                strbuf_addstr(buf, tb_now.buf);
  55                strbuf_addch(buf, ' ');
  56
  57                if (file && *file)
  58                        strbuf_addf(buf, "%s:%d ", file, line);
  59                while (buf->len < TR2FMT_NORMAL_FL_WIDTH)
  60                        strbuf_addch(buf, ' ');
  61        }
  62}
  63
  64static void normal_io_write_fl(const char *file, int line,
  65                               const struct strbuf *buf_payload)
  66{
  67        struct strbuf buf_line = STRBUF_INIT;
  68
  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);
  73}
  74
  75static void fn_version_fl(const char *file, int line)
  76{
  77        struct strbuf buf_payload = STRBUF_INIT;
  78
  79        strbuf_addf(&buf_payload, "version %s", git_version_string);
  80        normal_io_write_fl(file, line, &buf_payload);
  81        strbuf_release(&buf_payload);
  82}
  83
  84static void fn_start_fl(const char *file, int line,
  85                        uint64_t us_elapsed_absolute, const char **argv)
  86{
  87        struct strbuf buf_payload = STRBUF_INIT;
  88
  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);
  93}
  94
  95static void fn_exit_fl(const char *file, int line, uint64_t us_elapsed_absolute,
  96                       int code)
  97{
  98        struct strbuf buf_payload = STRBUF_INIT;
  99        double elapsed = (double)us_elapsed_absolute / 1000000.0;
 100
 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);
 104}
 105
 106static void fn_signal(uint64_t us_elapsed_absolute, int signo)
 107{
 108        struct strbuf buf_payload = STRBUF_INIT;
 109        double elapsed = (double)us_elapsed_absolute / 1000000.0;
 110
 111        strbuf_addf(&buf_payload, "signal elapsed:%.6f code:%d", elapsed,
 112                    signo);
 113        normal_io_write_fl(__FILE__, __LINE__, &buf_payload);
 114        strbuf_release(&buf_payload);
 115}
 116
 117static void fn_atexit(uint64_t us_elapsed_absolute, int code)
 118{
 119        struct strbuf buf_payload = STRBUF_INIT;
 120        double elapsed = (double)us_elapsed_absolute / 1000000.0;
 121
 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);
 125}
 126
 127static void maybe_append_string_va(struct strbuf *buf, const char *fmt,
 128                                   va_list ap)
 129{
 130        if (fmt && *fmt) {
 131                va_list copy_ap;
 132
 133                va_copy(copy_ap, ap);
 134                strbuf_vaddf(buf, fmt, copy_ap);
 135                va_end(copy_ap);
 136                return;
 137        }
 138}
 139
 140static void fn_error_va_fl(const char *file, int line, const char *fmt,
 141                           va_list ap)
 142{
 143        struct strbuf buf_payload = STRBUF_INIT;
 144
 145        strbuf_addstr(&buf_payload, "error ");
 146        maybe_append_string_va(&buf_payload, fmt, ap);
 147        normal_io_write_fl(file, line, &buf_payload);
 148        strbuf_release(&buf_payload);
 149}
 150
 151static void fn_command_path_fl(const char *file, int line, const char *pathname)
 152{
 153        struct strbuf buf_payload = STRBUF_INIT;
 154
 155        strbuf_addf(&buf_payload, "cmd_path %s", pathname);
 156        normal_io_write_fl(file, line, &buf_payload);
 157        strbuf_release(&buf_payload);
 158}
 159
 160static void fn_command_name_fl(const char *file, int line, const char *name,
 161                               const char *hierarchy)
 162{
 163        struct strbuf buf_payload = STRBUF_INIT;
 164
 165        strbuf_addf(&buf_payload, "cmd_name %s", name);
 166        if (hierarchy && *hierarchy)
 167                strbuf_addf(&buf_payload, " (%s)", hierarchy);
 168        normal_io_write_fl(file, line, &buf_payload);
 169        strbuf_release(&buf_payload);
 170}
 171
 172static void fn_command_mode_fl(const char *file, int line, const char *mode)
 173{
 174        struct strbuf buf_payload = STRBUF_INIT;
 175
 176        strbuf_addf(&buf_payload, "cmd_mode %s", mode);
 177        normal_io_write_fl(file, line, &buf_payload);
 178        strbuf_release(&buf_payload);
 179}
 180
 181static void fn_alias_fl(const char *file, int line, const char *alias,
 182                        const char **argv)
 183{
 184        struct strbuf buf_payload = STRBUF_INIT;
 185
 186        strbuf_addf(&buf_payload, "alias %s ->", alias);
 187        sq_quote_argv_pretty(&buf_payload, argv);
 188        normal_io_write_fl(file, line, &buf_payload);
 189        strbuf_release(&buf_payload);
 190}
 191
 192static void fn_child_start_fl(const char *file, int line,
 193                              uint64_t us_elapsed_absolute,
 194                              const struct child_process *cmd)
 195{
 196        struct strbuf buf_payload = STRBUF_INIT;
 197
 198        strbuf_addf(&buf_payload, "child_start[%d] ", cmd->trace2_child_id);
 199
 200        if (cmd->dir) {
 201                strbuf_addstr(&buf_payload, " cd");
 202                sq_quote_buf_pretty(&buf_payload, cmd->dir);
 203                strbuf_addstr(&buf_payload, "; ");
 204        }
 205
 206        /*
 207         * TODO if (cmd->env) { Consider dumping changes to environment. }
 208         * See trace_add_env() in run-command.c as used by original trace.c
 209         */
 210
 211        if (cmd->git_cmd)
 212                strbuf_addstr(&buf_payload, "git");
 213        sq_quote_argv_pretty(&buf_payload, cmd->argv);
 214
 215        normal_io_write_fl(file, line, &buf_payload);
 216        strbuf_release(&buf_payload);
 217}
 218
 219static void fn_child_exit_fl(const char *file, int line,
 220                             uint64_t us_elapsed_absolute, int cid, int pid,
 221                             int code, uint64_t us_elapsed_child)
 222{
 223        struct strbuf buf_payload = STRBUF_INIT;
 224        double elapsed = (double)us_elapsed_child / 1000000.0;
 225
 226        strbuf_addf(&buf_payload, "child_exit[%d] pid:%d code:%d elapsed:%.6f",
 227                    cid, pid, code, elapsed);
 228        normal_io_write_fl(file, line, &buf_payload);
 229        strbuf_release(&buf_payload);
 230}
 231
 232static void fn_exec_fl(const char *file, int line, uint64_t us_elapsed_absolute,
 233                       int exec_id, const char *exe, const char **argv)
 234{
 235        struct strbuf buf_payload = STRBUF_INIT;
 236
 237        strbuf_addf(&buf_payload, "exec[%d] ", exec_id);
 238        if (exe)
 239                strbuf_addstr(&buf_payload, exe);
 240        sq_quote_argv_pretty(&buf_payload, argv);
 241        normal_io_write_fl(file, line, &buf_payload);
 242        strbuf_release(&buf_payload);
 243}
 244
 245static void fn_exec_result_fl(const char *file, int line,
 246                              uint64_t us_elapsed_absolute, int exec_id,
 247                              int code)
 248{
 249        struct strbuf buf_payload = STRBUF_INIT;
 250
 251        strbuf_addf(&buf_payload, "exec_result[%d] code:%d", exec_id, code);
 252        if (code > 0)
 253                strbuf_addf(&buf_payload, " err:%s", strerror(code));
 254        normal_io_write_fl(file, line, &buf_payload);
 255        strbuf_release(&buf_payload);
 256}
 257
 258static void fn_param_fl(const char *file, int line, const char *param,
 259                        const char *value)
 260{
 261        struct strbuf buf_payload = STRBUF_INIT;
 262
 263        strbuf_addf(&buf_payload, "def_param %s=%s", param, value);
 264        normal_io_write_fl(file, line, &buf_payload);
 265        strbuf_release(&buf_payload);
 266}
 267
 268static void fn_repo_fl(const char *file, int line,
 269                       const struct repository *repo)
 270{
 271        struct strbuf buf_payload = STRBUF_INIT;
 272
 273        strbuf_addstr(&buf_payload, "worktree ");
 274        sq_quote_buf_pretty(&buf_payload, repo->worktree);
 275        normal_io_write_fl(file, line, &buf_payload);
 276        strbuf_release(&buf_payload);
 277}
 278
 279static void fn_printf_va_fl(const char *file, int line,
 280                            uint64_t us_elapsed_absolute, const char *fmt,
 281                            va_list ap)
 282{
 283        struct strbuf buf_payload = STRBUF_INIT;
 284
 285        maybe_append_string_va(&buf_payload, fmt, ap);
 286        normal_io_write_fl(file, line, &buf_payload);
 287        strbuf_release(&buf_payload);
 288}
 289
 290struct tr2_tgt tr2_tgt_normal = {
 291        &tr2dst_normal,
 292
 293        fn_init,
 294        fn_term,
 295
 296        fn_version_fl,
 297        fn_start_fl,
 298        fn_exit_fl,
 299        fn_signal,
 300        fn_atexit,
 301        fn_error_va_fl,
 302        fn_command_path_fl,
 303        fn_command_name_fl,
 304        fn_command_mode_fl,
 305        fn_alias_fl,
 306        fn_child_start_fl,
 307        fn_child_exit_fl,
 308        NULL, /* thread_start */
 309        NULL, /* thread_exit */
 310        fn_exec_fl,
 311        fn_exec_result_fl,
 312        fn_param_fl,
 313        fn_repo_fl,
 314        NULL, /* region_enter */
 315        NULL, /* region_leave */
 316        NULL, /* data */
 317        NULL, /* data_json */
 318        fn_printf_va_fl,
 319};