DebugHelper.cpp
Go to the documentation of this file.
1 
20 #include <boost/algorithm/string.hpp>
21 #include <boost/range/irange.hpp>
22 
24 
25 namespace robot_model_services {
26 
28 
30  if (!instancePtr)
32  return instancePtr;
33 }
34 
37  this->setLevels();
38 }
39 
40 void DebugHelper::write(const char *text, DebugHelper::DebugLevel level)
41 {
42  if (this->checkLevel(level)) {
43  ROS_DEBUG("%s", text);
44  }
45 }
46 
47 void DebugHelper::write(const std::string& text, DebugLevel level) {
48  const char* cText = text.c_str();
49 
50  this->write(cText, level);
51 }
52 
53 void DebugHelper::write(const std::ostream& text, DebugLevel level) {
54  std::stringstream ss;
55  ss << text.rdbuf();
56  std::string s = ss.str();
57 
58  this->write(s, level);
59 }
60 
62 {
63  if (this->checkLevel(level)) {
64  ROS_DEBUG(" ");
65  ROS_DEBUG("%s", text);
66  ROS_DEBUG(" ");
67  }
68 }
69 
70 void DebugHelper::writeNoticeably(const std::string &text, DebugHelper::DebugLevel level)
71 {
72  const char* cText = text.c_str();
73 
74  this->writeNoticeably(cText, level);
75 }
76 
77 void DebugHelper::writeNoticeably(const std::ostream &text, DebugHelper::DebugLevel level)
78 {
79  std::stringstream ss;
80  ss << text.rdbuf();
81  std::string s = ss.str();
82 
83  this->writeNoticeably(s, level);
84 }
85 
86 unsigned int DebugHelper::getLevel()
87 {
88  return mLevels;
89 }
90 
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  level += "PARAMETERS";
102  if (mLevels & RATING) {
103  addToString(level, "RATING");
104  }
105  if (mLevels & ROBOT_MODEL) {
106  addToString(level, "ROBOT_MODEL");
107  }
108  if (mLevels & MAP) {
109  addToString(level, "MAP");
110  }
111  if (mLevels & IK_RATING) {
112  addToString(level, "IK_RATING");
113  }
114 
115  return level;
116 }
117 
119  return level & mLevels;
120 }
121 
123  std::vector<std::string> levels;
124  mNodeHandle.param("debugLevels", levels, std::vector<std::string>());
125  mLevels = parseLevels(levels);
126 }
127 
128 void DebugHelper::setLevels(std::string levelsStr) {
129  std::vector<std::string> levels;
130  boost::trim_if(levelsStr, boost::is_any_of("\"',[]{} "));
131  boost::split(levels, levelsStr, boost::is_any_of(", "), boost::token_compress_on);
132  // trim each split string and to uppercase
133  for (int i : boost::irange(0, (int) levels.size())) {
134  boost::trim_if(levels[i], boost::is_any_of("\"',[]{} "));
135  boost::to_upper(levels[i]);
136  }
137  mLevels = parseLevels(levels);
138 }
139 
140 int DebugHelper::parseLevels(std::vector<std::string> levels) {
141  if (levels.size() == 0)
142  return ALL;
143  if (levels.size() == 1) {
144  if (levels.at(0).compare("ALL") == 0)
145  return ALL;
146  if (levels.at(0).compare("NONE") == 0)
147  return NONE;
148  }
149 
150  int level = 0;
151 
152  for (unsigned int i = 0; i < levels.size(); i++) {
153  if (levels.at(i).compare("PARAMETERS") == 0) {
154  level += PARAMETERS;
155  }
156  else if (levels.at(i).compare("RATING") == 0) {
157  level += RATING;
158  }
159  else if (levels.at(i).compare("ROBOT_MODEL") == 0) {
160  level += ROBOT_MODEL;
161  }
162  else if (levels.at(i).compare("MAP") == 0) {
163  level += MAP;
164  }
165  else if (levels.at(i).compare("IK_RATING") == 0) {
166  level += IK_RATING;
167  }
168  else {
169  ROS_ERROR_STREAM("Invalid debug level: " << levels.at(i));
170  throw "Invalid debug level";
171  }
172  }
173 
174  return level;
175 }
176 
177 void DebugHelper::addToString(std::string& s, const std::string& add)
178 {
179  if (s.size() != 0)
180  s += ", ";
181  s += add;
182 }
183 
184 }
static boost::shared_ptr< DebugHelper > instancePtr
Definition: DebugHelper.hpp:46
static boost::shared_ptr< DebugHelper > getInstance()
Definition: DebugHelper.cpp:29
XmlRpcServer s
ROSCPP_DECL const std::string & getName()
std::string getLevelString()
returns the debug levels that are set as string
Definition: DebugHelper.cpp:91
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...
void writeNoticeably(const char *text, DebugLevel level)
writes the text noticeably to the console if it has a level that allows it
Definition: DebugHelper.cpp:61
void setLevels()
sets the allowed debug levels
bool param(const std::string &param_name, T &param_val, const T &default_val) const
this namespace contains all generally usable classes.
Definition: DebugHelper.hpp:27
static int parseLevels(std::vector< std::string > levels)
parses the level list to the corresponding integer
void write(const char *text, DebugLevel level)
writes the text to the console if it has a level that allows it
Definition: DebugHelper.cpp:40
bool checkLevel(DebugLevel level)
checks whether the given level is allowed
unsigned int getLevel()
returns the debug level that is set
Definition: DebugHelper.cpp:86
#define ROS_ERROR_STREAM(args)
#define ROS_DEBUG(...)


asr_robot_model_services
Author(s): Aumann Florian, Borella Jocelyn, Heller Florian, Meißner Pascal, Schleicher Ralf, Stöckle Patrick, Stroh Daniel, Trautmann Jeremias, Walter Milena, Wittenbeck Valerij
autogenerated on Mon Jun 10 2019 12:49:59