DebConfig.cc 4.23 KB
Newer Older
1
2
3
4
5
6
// (C) Copyright 2016 by Autodesk, Inc.

#ifdef DEB_ON

#include "DebConfig.hh"
#include "DebDefault.hh"
7
#include "DebFile.hh"
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
#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");
Max Lyon's avatar
Max Lyon committed
39
    size_t pos = flnm.rfind(root_dir);
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    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)
  {
Max Lyon's avatar
Max Lyon committed
55
    for (std::list<std::string>::const_iterator sel_it = _sel_strings.begin(); sel_it != _sel_strings.end(); ++sel_it)
56
    {
Max Lyon's avatar
Max Lyon committed
57
      const std::string& sel = *sel_it;
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
      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()
{
Max Lyon's avatar
Max Lyon committed
89
  const std::string flnm =
90
91
92
93
94
95
96
97
98
99
    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;
    
Max Lyon's avatar
Max Lyon committed
100
    void (FilterLevelSelector::*add_string)(const std::string&) = NULL;
101
102
103
104
105
106
107
108
109
110
111
112
    
    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;
Max Lyon's avatar
Max Lyon committed
113
    if (add_string == NULL)
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
    {
      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;
}

//////////////////////////////////////////////////////////////////////////
144
Config& Config::modify()
145
146
147
148
149
{
  static Config glbl_cnfg;
  return glbl_cnfg;
}

150
151
152
153
154
const Config& Config::query()
{
  return modify();
}

155
156
157
158
159
160
161
162
163
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);
}

164
165
166
167
168
169
void Config::set_console(const bool _on) { File::modify().set_console(_on); }
bool Config::console() const { return File::query().console(); }

void Config::set_logfile(bool _on) { File::modify().set_logfile(_on); }
bool Config::logfile() const { return File::query().logfile(); }

170
171
172
}//namespace Debug

#endif//DEB_ON