trace2 / tr2_cfg.con commit Merge branch 'jk/perf-aggregate-wo-libjson' (6cfa633)
   1#include "cache.h"
   2#include "config.h"
   3#include "trace2/tr2_cfg.h"
   4#include "trace2/tr2_sysenv.h"
   5
   6static struct strbuf **tr2_cfg_patterns;
   7static int tr2_cfg_count_patterns;
   8static int tr2_cfg_loaded;
   9
  10/*
  11 * Parse a string containing a comma-delimited list of config keys
  12 * or wildcard patterns into a list of strbufs.
  13 */
  14static int tr2_cfg_load_patterns(void)
  15{
  16        struct strbuf **s;
  17        const char *envvar;
  18
  19        if (tr2_cfg_loaded)
  20                return tr2_cfg_count_patterns;
  21        tr2_cfg_loaded = 1;
  22
  23        envvar = tr2_sysenv_get(TR2_SYSENV_CFG_PARAM);
  24        if (!envvar || !*envvar)
  25                return tr2_cfg_count_patterns;
  26
  27        tr2_cfg_patterns = strbuf_split_buf(envvar, strlen(envvar), ',', -1);
  28        for (s = tr2_cfg_patterns; *s; s++) {
  29                struct strbuf *buf = *s;
  30
  31                if (buf->len && buf->buf[buf->len - 1] == ',')
  32                        strbuf_setlen(buf, buf->len - 1);
  33                strbuf_trim_trailing_newline(*s);
  34                strbuf_trim(*s);
  35        }
  36
  37        tr2_cfg_count_patterns = s - tr2_cfg_patterns;
  38        return tr2_cfg_count_patterns;
  39}
  40
  41void tr2_cfg_free_patterns(void)
  42{
  43        if (tr2_cfg_patterns)
  44                strbuf_list_free(tr2_cfg_patterns);
  45        tr2_cfg_count_patterns = 0;
  46        tr2_cfg_loaded = 0;
  47}
  48
  49struct tr2_cfg_data {
  50        const char *file;
  51        int line;
  52};
  53
  54/*
  55 * See if the given config key matches any of our patterns of interest.
  56 */
  57static int tr2_cfg_cb(const char *key, const char *value, void *d)
  58{
  59        struct strbuf **s;
  60        struct tr2_cfg_data *data = (struct tr2_cfg_data *)d;
  61
  62        for (s = tr2_cfg_patterns; *s; s++) {
  63                struct strbuf *buf = *s;
  64                int wm = wildmatch(buf->buf, key, WM_CASEFOLD);
  65                if (wm == WM_MATCH) {
  66                        trace2_def_param_fl(data->file, data->line, key, value);
  67                        return 0;
  68                }
  69        }
  70
  71        return 0;
  72}
  73
  74void tr2_cfg_list_config_fl(const char *file, int line)
  75{
  76        struct tr2_cfg_data data = { file, line };
  77
  78        if (tr2_cfg_load_patterns() > 0)
  79                read_early_config(tr2_cfg_cb, &data);
  80}
  81
  82void tr2_cfg_set_fl(const char *file, int line, const char *key,
  83                    const char *value)
  84{
  85        struct tr2_cfg_data data = { file, line };
  86
  87        if (tr2_cfg_load_patterns() > 0)
  88                tr2_cfg_cb(key, value, &data);
  89}