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

#ifdef DEB_ON

#include "DebConfig.hh"
6
#include "DebFile.hh"
7 8 9 10
#include "Base/Utils/Environment.hh"

#include <fstream>
#include <sstream>
11
#include <iostream>
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 <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
    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:
52 53 54 55 56 57 58 59 60
  typedef std::list<std::string> StringList;

  // list of strings to be found inside the file name.
  StringList file_selct_strngs_; 
  // list of strings to be found inside the function name.
  StringList func_selct_strngs_; 

private:
  static bool search(const std::string& _str, const StringList& _slcts)
61
  {
62
    for (const auto& slct : _slcts)
63
    {
64
      if (_str.find(slct) != std::string::npos)
65 66 67 68 69 70 71 72
        return true;
    }
    return false;
  }
};

}//namespace 

73
void print_char_to_cerr(const char _c) { std::cerr << _c; }
74

75
class Config::LevelFilterMap : public std::map<int, FilterLevelSelector> {};
76

77
bool Config::load(const char* const _cnfg_envr, const char* const _cnfg_flnm)
78
{
79
  const auto flnm = System::Environment::variable(_cnfg_envr, _cnfg_flnm);
80 81

  std::ifstream cnfg_strm(flnm.c_str());
82 83 84 85 86 87
  if (!cnfg_strm.is_open())
    return false;

  delete lvl_fltrs_;
  lvl_fltrs_ = new LevelFilterMap;

88 89 90 91 92 93 94
  std::string line;
  while (std::getline(cnfg_strm, line))
  {
    std::stringstream line_stream(line);
    std::string type;
    line_stream >> type;
    
95
    void (FilterLevelSelector::*add_string)(const std::string&) = nullptr;
96 97 98 99 100 101 102 103 104 105 106 107
    
    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;
108
    if (add_string == nullptr)
109
    {
110
      output_level = lvl; // We have read the default level.
111 112 113 114 115 116 117
      continue;
    }
    char colon;
    line_stream >> colon;
    if (colon != ':')
      continue;
    std::string select_str;
118 119
    while (line_stream >> select_str)
      ((*lvl_fltrs_)[lvl].*add_string)(select_str);
120
  }
121
  return true;
122 123
}

124
int Config::custom_level(const char* const _flnm, const char* const _fnct) const
125
{
126 127 128 129
  if (lvl_fltrs_ == nullptr)
    return output_level;
  int lvl = output_level;
  for (const auto& fltr : *lvl_fltrs_)
130 131 132 133 134 135 136 137
  {// continue this iteration until the maximum allowed level if found
    if (fltr.second.select_file(_flnm) || fltr.second.select_function(_fnct))
      lvl = fltr.first;
  }
  return lvl;
}

//////////////////////////////////////////////////////////////////////////
138
Config& Config::modify()
139 140 141 142 143
{
  static Config glbl_cnfg;
  return glbl_cnfg;
}

144 145 146 147 148
const Config& Config::query()
{
  return modify();
}

149
const Config& Config::defaults()
150
{
151 152
  static Config dflt_cnfg;
  return dflt_cnfg;
153 154
}

155
Config::Config() {}
156

157
Config::~Config() { delete lvl_fltrs_; }
158

159 160 161
}//namespace Debug

#endif//DEB_ON