debug_helper.cpp
Go to the documentation of this file.
1 
19 
20 namespace world_model {
21 
23 
25  if (!instancePtr)
27  return instancePtr;
28 }
29 
31  if (instancePtr) {
32  instancePtr->setLevels();
33  }
34 }
35 
37  this->setLevels();
38 }
39 
40 void DebugHelper::write(const char *text, const unsigned int &levels) const
41 {
42  if (this->checkLevel(levels)) {
43  ROS_DEBUG_STREAM(getDebugLevelString(levels) << ": " << text);
44  }
45 }
46 
47 void DebugHelper::write(const std::string& text, const unsigned int &levels) const {
48  const char* cText = text.c_str();
49 
50  this->write(cText, levels);
51 }
52 
53 void DebugHelper::write(const std::ostream& text, const unsigned int &levels) const {
54  std::stringstream ss;
55  ss << text.rdbuf();
56  std::string s = ss.str();
57 
58  this->write(s, levels);
59 }
60 
61 void DebugHelper::writeNoticeably(const char *text, const unsigned int &levels) const
62 {
63  if (this->checkLevel(levels)) {
64  ROS_DEBUG_STREAM(" ");
65  ROS_DEBUG_STREAM(getDebugLevelString(levels) << ": " << text);
66  ROS_DEBUG_STREAM(" ");
67  }
68 }
69 
70 void DebugHelper::writeNoticeably(const std::string &text, const unsigned int &levels) const
71 {
72  const char* cText = text.c_str();
73 
74  this->writeNoticeably(cText, levels);
75 }
76 
77 void DebugHelper::writeNoticeably(const std::ostream &text, const unsigned int &levels) const
78 {
79  std::stringstream ss;
80  ss << text.rdbuf();
81  std::string s = ss.str();
82 
83  this->writeNoticeably(s, levels);
84 }
85 
86 unsigned int DebugHelper::getLevel() const
87 {
88  return mLevels;
89 }
90 
91 std::string DebugHelper::getLevelString() const
92 {
93  if (mLevels == ALL)
94  return "ALL";
95  if (mLevels == NONE)
96  return "NONE";
97 
98  std::string level = "";
99 
100  if (mLevels & PARAMETERS) {
101  addToString(level, "PARAMETERS");
102  }
103  if (mLevels & SERVICE_CALLS) {
104  addToString(level, "SERVICE_CALLS");
105  }
106  if (mLevels & COMMON_INFORMATION) {
107  addToString(level, "COMMON_INFORMATION");
108  }
109  if (mLevels & FOUND_OBJECT) {
110  addToString(level, "FOUND_OBJECT");
111  }
112  if (mLevels & VIEW_PORT) {
113  addToString(level, "VIEW_PORT");
114  }
115  if (mLevels & COMPLETE_PATTERN) {
116  addToString(level, "COMPLETE_PATTERN");
117  }
118 
119  return level;
120 }
121 
122 std::string DebugHelper::getDebugLevelString(const unsigned int &levels) const
123 {
124  std::string level = "";
125 
126  if (levels & PARAMETERS)
127  addToString(level, "PARAMETERS");
128  if (levels & SERVICE_CALLS)
129  addToString(level, "SERVICE_CALLS");
130  if (levels & COMMON_INFORMATION)
131  addToString(level, "COMMON_INFORMATION");
132  if (levels & FOUND_OBJECT)
133  addToString(level, "FOUND_OBJECT");
134  if (levels & VIEW_PORT)
135  addToString(level, "VIEW_PORT");
136  if (levels & COMPLETE_PATTERN)
137  addToString(level, "COMPLETE_PATTERN");
138 
139  return level;
140 }
141 
142 bool DebugHelper::checkLevel(const unsigned int &levels) const {
143  return levels & mLevels;
144 }
145 
148 
149  std::vector<std::string> debugLevels;
150  mNodeHandle.getParam("debugLevels", debugLevels);
151 
152  if (debugLevels.size() == 0) {
153  // Parse debugLevels, because there is no vector in dynamic_reconfigure
154  std::string levelString;
155  mNodeHandle.getParam("debugLevels", levelString);
156 
157  levelString.erase(std::remove(levelString.begin(), levelString.end(), '['), levelString.end());
158  levelString.erase(std::remove(levelString.begin(), levelString.end(), ']'), levelString.end());
159  levelString.erase(std::remove(levelString.begin(), levelString.end(), ' '), levelString.end());
160 
161  std::stringstream ss(levelString);
162  std::string item;
163  while (std::getline(ss, item, ',')) {
164  debugLevels.push_back(item);
165  }
166  }
167 
168  mLevels = parseLevels(debugLevels);
169 }
170 
171 int DebugHelper::parseLevels(const std::vector<std::string> &levels) {
172  if (levels.size() == 0)
173  return ALL;
174  if (levels.size() == 1) {
175  if (levels.at(0).compare("ALL") == 0)
176  return ALL;
177  if (levels.at(0).compare("NONE") == 0)
178  return NONE;
179  }
180 
181  int level = 0;
182  for (unsigned int i = 0; i < levels.size(); ++i) {
183  if (levels.at(i).compare("PARAMETERS") == 0) {
184  level += PARAMETERS;
185  }
186  else if (levels.at(i).compare("SERVICE_CALLS") == 0) {
187  level += SERVICE_CALLS;
188  }
189  else if (levels.at(i).compare("COMMON_INFORMATION") == 0) {
190  level += COMMON_INFORMATION;
191  }
192  else if (levels.at(i).compare("FOUND_OBJECT") == 0) {
193  level += FOUND_OBJECT;
194  }
195  else if (levels.at(i).compare("VIEW_PORT") == 0) {
196  level += VIEW_PORT;
197  }
198  else if (levels.at(i).compare("COMPLETE_PATTERN") == 0) {
199  level += COMPLETE_PATTERN;
200  }
201  else {
202  ROS_ERROR_STREAM("Invalid debug level: " << levels.at(i));
203  throw "Invalid debug level";
204  }
205  }
206 
207  return level;
208 }
209 
210 void DebugHelper::addToString(std::string& s, const std::string& add)
211 {
212  if (s.size() != 0)
213  s += ", ";
214  s += add;
215 }
216 
217 }
static void addToString(std::string &s, const std::string &add)
adds a string to a given string s. Puts a comma between them if the string s has a size bigger than 0...
static boost::shared_ptr< DebugHelper > instancePtr
bool checkLevel(const unsigned int &levels) const
checks whether the given level is allowed
std::string getDebugLevelString(const unsigned int &levels) const
returns the name of the given DebugLevel
XmlRpcServer s
unsigned int getLevel() const
returns the debug level that is set
ROSCPP_DECL const std::string & getName()
void setLevels()
sets the allowed debug levels
static boost::shared_ptr< DebugHelper > getInstance()
static int parseLevels(const std::vector< std::string > &levels)
parses the level list to the corresponding integer
void write(const char *text, const unsigned int &levels) const
writes the text to the console if it has a level that allows it
#define ROS_DEBUG_STREAM(args)
static const int ALL
static void resetInstance()
void writeNoticeably(const char *text, const unsigned int &levels) const
writes the text noticeably to the console if it has a level that allows it
std::string getLevelString() const
returns the debug levels that are set as string
bool getParam(const std::string &key, std::string &s) const
static const int NONE
#define ROS_ERROR_STREAM(args)


asr_world_model
Author(s): Aumann Florian, Borella Jocelyn, Hutmacher Robin, Karrenbauer Oliver, Meißner Pascal, Schleicher Ralf, Stöckle Patrick, Trautmann Jeremias
autogenerated on Thu Jan 9 2020 07:20:01