DiagnosticStatusWrapper.h
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
39 #ifndef DIAGNOSTICMESSAGEWRAPPER_HH
40 #define DIAGNOSTICMESSAGEWRAPPER_HH
41 
42 #include <vector>
43 #include <string>
44 #include <sstream>
45 #include <stdarg.h>
46 #include <cstdio>
47 
48 #include "ros/ros.h"
49 
50 #include "diagnostic_msgs/DiagnosticStatus.h"
51 
52 namespace diagnostic_updater
53 {
54 
66  class DiagnosticStatusWrapper : public diagnostic_msgs::DiagnosticStatus
67  {
68  public:
69 
76  void summary(unsigned char lvl, const std::string s)
77  {
78  level = lvl;
79  message = s;
80  }
81 
101  void mergeSummary(unsigned char lvl, const std::string s)
102  {
103  if ((lvl>0) && (level>0))
104  {
105  if (!message.empty())
106  message += "; ";
107  message += s;
108  }
109  else if (lvl > level)
110  message = s;
111 
112  if (lvl > level)
113  level = lvl;
114  }
115 
123  void mergeSummary(const diagnostic_msgs::DiagnosticStatus &src)
124  {
125  mergeSummary(src.level, src.message);
126  }
127 
140  void mergeSummaryf(unsigned char lvl, const char *format, ...)
141  {
142  va_list va;
143  char buff[1000]; // @todo This could be done more elegantly.
144  va_start(va, format);
145  if (vsnprintf(buff, 1000, format, va) >= 1000)
146  ROS_DEBUG("Really long string in DiagnosticStatusWrapper::addf, it was truncated.");
147  std::string value = std::string(buff);
148  mergeSummary(lvl, value);
149  va_end(va);
150  }
151 
163  void summaryf(unsigned char lvl, const char *format, ...)
164  {
165  va_list va;
166  char buff[1000]; // @todo This could be done more elegantly.
167  va_start(va, format);
168  if (vsnprintf(buff, 1000, format, va) >= 1000)
169  ROS_DEBUG("Really long string in DiagnosticStatusWrapper::addf, it was truncated.");
170  std::string value = std::string(buff);
171  summary(lvl, value);
172  va_end(va);
173  }
174 
180  {
181  summary(0, "");
182  }
183 
189  void summary(const diagnostic_msgs::DiagnosticStatus &src)
190  {
191  summary(src.level, src.message);
192  }
193 
203  template<class T>
204  void add(const std::string &key, const T &val)
205  {
206  std::stringstream ss;
207  ss << val;
208  std::string sval = ss.str();
209  add(key, sval);
210  }
211 
220  void addf(const std::string &key, const char *format, ...); // In practice format will always be a char *
221 
228  void clear()
229  {
230  values.clear();
231  }
232  };
233 
234  template<>
235  inline void DiagnosticStatusWrapper::add<std::string>(const std::string &key, const std::string &s)
236  {
237  diagnostic_msgs::KeyValue ds;
238  ds.key = key;
239  ds.value = s;
240  values.push_back(ds);
241  }
242 
244 template<>
245 inline void DiagnosticStatusWrapper::add<bool>(const std::string &key, const bool &b)
246 {
247  diagnostic_msgs::KeyValue ds;
248  ds.key = key;
249  ds.value = b ? "True" : "False";
250 
251  values.push_back(ds);
252 }
253 
254  // Need to place addf after DiagnosticStatusWrapper::add<std::string> or
255  // gcc complains that the specialization occurs after instatiation.
256  inline void DiagnosticStatusWrapper::addf(const std::string &key, const char *format, ...) // In practice format will always be a char *
257  {
258  va_list va;
259  char buff[1000]; // @todo This could be done more elegantly.
260  va_start(va, format);
261  if (vsnprintf(buff, 1000, format, va) >= 1000)
262  ROS_DEBUG("Really long string in DiagnosticStatusWrapper::addf, it was truncated.");
263  std::string value = std::string(buff);
264  add(key, value);
265  va_end(va);
266  }
267 
268 
269 }
270 #endif
void mergeSummary(const diagnostic_msgs::DiagnosticStatus &src)
Version of mergeSummary that merges in the summary from another DiagnosticStatus. ...
void summary(unsigned char lvl, const std::string s)
Fills out the level and message fields of the DiagnosticStatus.
XmlRpcServer s
Author: Blaise Gassend.
void addf(const std::string &key, const char *format,...)
Add a key-value pair using a format string.
void summaryf(unsigned char lvl, const char *format,...)
Formatted version of summary.
void clearSummary()
clears the summary, setting the level to zero and the message to "".
void mergeSummary(unsigned char lvl, const std::string s)
Merges a level and message with the existing ones.
void mergeSummaryf(unsigned char lvl, const char *format,...)
Formatted version of mergeSummary.
void add(const std::string &key, const T &val)
Add a key-value pair.
void summary(const diagnostic_msgs::DiagnosticStatus &src)
copies the summary from a DiagnosticStatus message
Wrapper for the diagnostic_msgs::DiagnosticStatus message that makes it easier to update...
#define ROS_DEBUG(...)


diagnostic_updater
Author(s): Kevin Watts, Brice Rebsamen , Jeremy Leibs, Blaise Gassend
autogenerated on Mon Feb 28 2022 22:18:16