DebConfig.cc 3.77 KB
Newer Older
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
// (C) Copyright 2016 by Autodesk, Inc.

#ifdef DEB_ON

#include "DebConfig.hh"
#include "DebDefault.hh"
#include "Base/Utils/Environment.hh"

#include <fstream>
#include <sstream>
#include <list>
#include <string>
#include <map>

namespace Debug {
namespace {
// We use this data to decide the debug level of a function in a file.
class FilterLevelSelector
{
public:
  void add_file_string(const std::string& _str) 
  { 
    file_selct_strngs_.push_back(_str); 
  }

  void add_func_string(const std::string& _str) 
  { 
    func_selct_strngs_.push_back(_str); 
  }

  bool select_file(const char* _flnm) const
  {
    // TODO: this should be possible to implement w/o a copy
    std::string flnm(_flnm);
    // TODO: this code below only works in ReForm, should be made to work
    // for IGM, CoMISo, etc
    const std::string root_dir("ReForm");
    auto pos = flnm.rfind(root_dir);
    if (pos != std::string::npos)
      flnm = flnm.substr(pos + root_dir.size());

    return search(flnm, file_selct_strngs_);
  }

  bool select_function(const char* _func) const
  {
    return search(_func, func_selct_strngs_);
  }

private:
  static bool search(const std::string& _flnm, 
    const std::list<std::string>& _sel_strings)
  {
    for (const auto& sel : _sel_strings)
    {
      if (_flnm.find(sel) != std::string::npos)
        return true;
    }
    return false;
  }

private:
  // list of strings to be found inside the file name.
  std::list<std::string> file_selct_strngs_; 
  // list of strings to be found inside the function name.
  std::list<std::string> func_selct_strngs_; 
};

}//namespace 

class Config::Impl
{
public:
  Impl() : dflt_lvl_(Default::LEVEL) { read(); }

  void read();
  int level(const char* const _flnm, const char* const _fnct) const;

private:
  int dflt_lvl_;
  typedef std::map<int, FilterLevelSelector> LevelFilterMap;
  LevelFilterMap lvl_fltrs_; // filters for each level
};

void Config::Impl::read()
{
  const auto flnm = 
    System::Environment::variable("REFORM_DEB_CONFIG", "reform_deb.cfg");

  std::ifstream cnfg_strm(flnm.c_str());
  std::string line;
  while (std::getline(cnfg_strm, line))
  {
    std::stringstream line_stream(line);
    std::string type;
    line_stream >> type;
    
    void (FilterLevelSelector::*add_string)(const std::string&) = nullptr;
    
    if (type == "all") 
      {}
    else if (type == "file")
      add_string = &FilterLevelSelector::add_file_string;
    else if (type == "func")
      add_string = &FilterLevelSelector::add_func_string;
    else
      continue;

    int lvl;
    line_stream >> lvl;
    if (add_string == nullptr)
    {
      dflt_lvl_ = lvl; // We have read the default level.
      continue;
    }
    char colon;
    line_stream >> colon;
    if (colon != ':')
      continue;
    std::string select_str;
    while(line_stream >> select_str)
      (lvl_fltrs_[lvl].*add_string)(select_str);
  }
}

int Config::Impl::level(const char* const _flnm, const char* const _fnct) const
{
  int lvl = dflt_lvl_;
  for (LevelFilterMap::const_iterator fltr_it = lvl_fltrs_.begin(); 
    fltr_it != lvl_fltrs_.end(); ++fltr_it)
  {// continue this iteration until the maximum allowed level if found
    const LevelFilterMap::value_type& fltr = *fltr_it;
    if (lvl >= fltr.first) // can this filter increase the current level?
      continue;      
    if (fltr.second.select_file(_flnm) || fltr.second.select_function(_fnct))
      lvl = fltr.first;
  }
  return lvl;
}

//////////////////////////////////////////////////////////////////////////
const Config& Config::query()
{
  static Config glbl_cnfg;
  return glbl_cnfg;
}

Config::Config() : impl_(new Impl) {}

Config::~Config() { delete impl_; }

int Config::level(const char* const _flnm, const char* const _fnct) const
{
  return impl_->level(_flnm, _fnct);
}

}//namespace Debug

#endif//DEB_ON