summaryrefslogtreecommitdiffstats
path: root/src/common/config_values.cc
blob: 24f556e35a22a5317221dcc5f35e2edab5b9e9b7 (plain)
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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
#include "config_values.h"

#include "config.h"

ConfigValues::set_value_result_t
ConfigValues::set_value(const std::string& key,
                        Option::value_t&& new_value,
                        int level)
{  
  if (auto p = values.find(key); p != values.end()) {
    auto q = p->second.find(level);
    if (q != p->second.end()) {
      if (new_value == q->second) {
        return SET_NO_CHANGE;
      }
      q->second = std::move(new_value);
    } else {
      p->second[level] = std::move(new_value);
    }
    if (p->second.rbegin()->first > level) {
      // there was a higher priority value; no effect
      return SET_NO_EFFECT;
    } else {
      return SET_HAVE_EFFECT;
    }
  } else {
    values[key][level] = std::move(new_value);
    return SET_HAVE_EFFECT;
  }
}

int ConfigValues::rm_val(const std::string& key, int level)
{
  auto i = values.find(key);
  if (i == values.end()) {
    return -ENOENT;
  }
  auto j = i->second.find(level);
  if (j == i->second.end()) {
    return -ENOENT;
  }
  bool matters = (j->first == i->second.rbegin()->first);
  i->second.erase(j);
  if (matters) {
    return SET_HAVE_EFFECT;
  } else {
    return SET_NO_EFFECT;
  }
}

std::pair<Option::value_t, bool>
ConfigValues::get_value(const std::string& name, int level) const
{
  auto p = values.find(name);
  if (p != values.end() && !p->second.empty()) {
    // use highest-priority value available (see CONF_*)
    if (level < 0) {
      return {p->second.rbegin()->second, true};
    } else if (auto found = p->second.find(level);
               found != p->second.end()) {
      return {found->second, true};
    }
  }
  return {Option::value_t{}, false};
}

void ConfigValues::set_logging(int which, const char* val)
{
  int log, gather;
  int r = sscanf(val, "%d/%d", &log, &gather);
  if (r >= 1) {
    if (r < 2) {
      gather = log;
    }
    subsys.set_log_level(which, log);
    subsys.set_gather_level(which, gather);
  }
}

bool ConfigValues::contains(const std::string& key) const
{
  return values.count(key);
}