MIRA
Status.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_STATUS_H_
48 #define _MIRA_STATUS_H_
49 
50 #include <sstream>
51 #include <list>
52 
53 #ifndef Q_MOC_RUN
54 #include <boost/optional.hpp>
55 #endif
56 
57 #include <utils/Time.h>
59 #include <serialization/adapters/std/map>
60 
61 #include <fw/FrameworkExports.h>
62 
63 namespace mira {
64 
66 
71 class Status
72 {
73 public:
76  {
77  OK,
82  };
83 
84 public:
85 
86  Status() :
87  mode(OK)
88  {}
89 
91  Status(StatusMode iMode, const std::string& iCategory,
92  const std::string& iTrText, const std::string& iMessage) :
93  mode(iMode),
94  category(iCategory),
95  trText(iTrText),
96  message(iMessage)
97  {}
98 
99  bool operator==(const Status& other) const
100  {
101  return mode == other.mode &&
102  category == other.category &&
103  trText == other.trText &&
104  message == other.message;
105  }
106 
107  template <typename Reflector>
108  void reflect(Reflector& r)
109  {
110  r.member("Mode", mode, "Status mode");
111  r.member("Category", category, "Category");
112  r.member("TranslatedText", trText, "Translated text");
113  r.member("Message", message, "Message");
114  }
115 
116 public:
117 
120 
122  std::string category;
123 
125  std::string trText;
126 
128  std::string message;
129 };
130 
132 
139 {
140 public:
141 
142  typedef std::map<std::string, Status> StatusMap;
143 
144  DiagnosticsModule(const std::string& name="") :
145  mHeartbeatInterval(Duration::seconds(1)),
146  mName(name) {}
147 
148  virtual ~DiagnosticsModule() {}
149 
153  void setHeartbeatInterval(const Duration& interval)
154  {
155  mHeartbeatInterval = interval;
156  }
157 
158  void setName(const std::string& name)
159  {
160  mName = name;
161  }
162 
167  {
168  return mHeartbeatInterval;
169  }
170 
179  void heartbeat()
180  {
181  mLastHeartbeat.reset(Time::now());
182  }
183 
188  bool hasHeartbeatTimeout() const
189  {
190  return mLastHeartbeat && (Time::now()-*mLastHeartbeat) > mHeartbeatInterval;
191  }
192 
197  void bootup(const std::string& message, const std::string& trText="");
198 
203  void bootupFinished();
204 
209  void recoverFinished();
210 
215  void recover(const std::string& message, const std::string& trText="");
216 
221  void ok(const std::string& category="");
222 
229  bool warning(const std::string& category, const std::string& message,
230  const std::string& trText="");
231 
238  bool error(const std::string& category, const std::string& message,
239  const std::string& trText="");
240 
254  bool setStatus(Status::StatusMode mode, const std::string& category,
255  const std::string& message, const std::string& trText="");
256 
265  Status::StatusMode getStatus() const;
266 
271  StatusMap getStatusMap() const;
272 
273 protected:
274 
275  boost::optional<Status> mBootUpStatus;
276  boost::optional<Status> mRecoverStatus;
278  boost::optional<Time> mLastHeartbeat;
280  std::string mName;
281 };
282 
285 
287 
292 {
293 public:
294 
295  typedef std::multimap<std::string, Status> StatusMap;
296 
298  static Status::StatusMode getOverallStatus(StatusMap::const_iterator start,
299  StatusMap::const_iterator end)
300  {
301  Status::StatusMode mostSevereMode = Status::OK;
302  for(auto i = start; i != end; ++i)
303  {
304  if (mostSevereMode < i->second.mode)
305  mostSevereMode = i->second.mode;
306  }
307  return mostSevereMode;
308  }
309 
311  void setID(const std::string& id)
312  {
313  mID = id;
314  }
315 
320  Status::StatusMode getStatus() const;
321 
325  Status::StatusMode getStatus(const std::string& diagnosticModule) const;
326 
333  void registerDiagnostics(const std::string& name, DiagnosticsModulePtr ptr);
334 
339  void unregisterDiagnostics(const std::string& name);
340 
345  void unregisterDiagnostics(DiagnosticsModulePtr ptr);
346 
350  std::vector<std::string> getDiagnosticModules() const;
351 
355  StatusMap getStatusMap() const;
356 
360  DiagnosticsModule::StatusMap getStatusMap(const std::string& diagnosticModule) const;
361 
362  template <typename Reflector>
363  void reflect(Reflector& r)
364  {
365  } // do nothing here
366 
367 private:
368  typedef std::map<std::string, DiagnosticsModulePtr> ModuleMap;
369  // vector used for storing the order the modules were registered
370  std::vector<std::string> mModuleOrder;
371  mutable boost::mutex mModuleMutex;
372  ModuleMap mModules;
373  StatusMap mStatus;
374  std::string mID;
375 };
376 
378 
379 }
380 
381 #endif
Manages the status of one or multiple modules inheriting from DiagnosticsModule.
Definition: Status.h:291
Status()
Definition: Status.h:86
Definition: Status.h:77
Definition: Status.h:81
void setHeartbeatInterval(const Duration &interval)
Set the watchdog interval.
Definition: Status.h:153
Status(StatusMode iMode, const std::string &iCategory, const std::string &iTrText, const std::string &iMessage)
Construct a new status with given mode, category and text.
Definition: Status.h:91
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
StatusMap mStatusMap
Definition: Status.h:279
Time and Duration wrapper class.
std::string category
category of that status
Definition: Status.h:122
boost::optional< Status > mRecoverStatus
Definition: Status.h:276
std::multimap< std::string, Status > StatusMap
Definition: Status.h:295
virtual ~DiagnosticsModule()
Definition: Status.h:148
Provides MIRA_CLASS_SERIALIZATION macro and includes the major headers of the serialization framework...
Framework export macro declaration.
Duration getHeartbeatInterval() const
Return the watchdog interval.
Definition: Status.h:166
StatusMode
Different status levels sorted from "okay" to "most severe".
Definition: Status.h:75
boost::optional< Time > mLastHeartbeat
Definition: Status.h:278
DiagnosticsModule(const std::string &name="")
Definition: Status.h:144
std::string mName
Definition: Status.h:280
std::string message
the corresponding message
Definition: Status.h:128
Status entry class.
Definition: Status.h:71
boost::optional< Status > mBootUpStatus
Definition: Status.h:275
#define MIRA_FRAMEWORK_EXPORT
Definition: FrameworkExports.h:61
Use this class to represent time durations.
Definition: Time.h:106
Definition: Status.h:80
void reflect(Reflector &r)
Definition: Status.h:363
Definition: Status.h:78
DiagnosticsModule * DiagnosticsModulePtr
Typedef of a DiagnosticsModule pointer.
Definition: Status.h:284
Definition: Status.h:79
void heartbeat()
When called for the first time heartbeat usage will be enabled.
Definition: Status.h:179
void setID(const std::string &id)
Set the id that is used for displaying status messages.
Definition: Status.h:311
bool hasHeartbeatTimeout() const
Returns true if heartbeat usage is enabled (by first call to heartbeat()) and last heartbeat was more...
Definition: Status.h:188
Duration mHeartbeatInterval
Definition: Status.h:277
static Status::StatusMode getOverallStatus(StatusMap::const_iterator start, StatusMap::const_iterator end)
Return the overall (most severe) status from a range of status map entries.
Definition: Status.h:298
StatusMode mode
the status flag (OK, WARNING or ERROR)
Definition: Status.h:119
static Time now() static Time eternity()
Returns the current utc based time.
Definition: Time.h:481
std::map< std::string, Status > StatusMap
Definition: Status.h:142
Base class for modules that want to use diagnostics and set the current status.
Definition: Status.h:138
void reflect(Reflector &r)
Definition: Status.h:108
std::string trText
Status text that can be used for translation of the error to other languages.
Definition: Status.h:125
bool operator==(const Status &other) const
Definition: Status.h:99
void setName(const std::string &name)
Definition: Status.h:158