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 namespace mira {
62 
64 
69 class Status
70 {
71 public:
74  {
75  OK,
80  };
81 
82 public:
83 
84  Status() :
85  mode(OK)
86  {}
87 
89  Status(StatusMode iMode, const std::string& iCategory,
90  const std::string& iTrText, const std::string& iMessage) :
91  mode(iMode),
92  category(iCategory),
93  trText(iTrText),
94  message(iMessage)
95  {}
96 
97  bool operator==(const Status& other) const
98  {
99  return mode == other.mode &&
100  category == other.category &&
101  trText == other.trText &&
102  message == other.message;
103  }
104 
105  template <typename Reflector>
106  void reflect(Reflector& r)
107  {
108  r.member("Mode", mode, "Status mode");
109  r.member("Category", category, "Category");
110  r.member("TranslatedText", trText, "Translated text");
111  r.member("Message", message, "Message");
112  }
113 
114 public:
115 
118 
120  std::string category;
121 
123  std::string trText;
124 
126  std::string message;
127 };
128 
130 
137 {
138 public:
139 
140  typedef std::map<std::string, Status> StatusMap;
141 
142  DiagnosticsModule(const std::string& name="") :
143  mHeartbeatInterval(Duration::seconds(1)),
144  mName(name) {}
145 
146  virtual ~DiagnosticsModule() {}
147 
151  void setHeartbeatInterval(const Duration& interval)
152  {
153  mHeartbeatInterval = interval;
154  }
155 
156  void setName(const std::string& name)
157  {
158  mName = name;
159  }
160 
165  {
166  return mHeartbeatInterval;
167  }
168 
177  void heartbeat()
178  {
179  mLastHeartbeat.reset(Time::now());
180  }
181 
186  bool hasHeartbeatTimeout() const
187  {
189  }
190 
195  void bootup(const std::string& message, const std::string& trText="");
196 
201  void bootupFinished();
202 
207  void recoverFinished();
208 
213  void recover(const std::string& message, const std::string& trText="");
214 
219  void ok(const std::string& category="");
220 
227  bool warning(const std::string& category, const std::string& message,
228  const std::string& trText="");
229 
236  bool error(const std::string& category, const std::string& message,
237  const std::string& trText="");
238 
252  bool setStatus(Status::StatusMode mode, const std::string& category,
253  const std::string& message, const std::string& trText="");
254 
264 
269  StatusMap getStatusMap() const;
270 
271 protected:
272 
273  boost::optional<Status> mBootUpStatus;
274  boost::optional<Status> mRecoverStatus;
276  boost::optional<Time> mLastHeartbeat;
278  std::string mName;
279 };
280 
283 
285 
290 {
291 public:
292 
293  typedef std::multimap<std::string, Status> StatusMap;
294 
296  static Status::StatusMode getOverallStatus(StatusMap::const_iterator start,
297  StatusMap::const_iterator end)
298  {
299  Status::StatusMode mostSevereMode = Status::OK;
300  for(auto i = start; i != end; ++i)
301  {
302  if (mostSevereMode < i->second.mode)
303  mostSevereMode = i->second.mode;
304  }
305  return mostSevereMode;
306  }
307 
309  void setID(const std::string& id)
310  {
311  mID = id;
312  }
313 
319 
323  Status::StatusMode getStatus(const std::string& diagnosticModule) const;
324 
331  void registerDiagnostics(const std::string& name, DiagnosticsModulePtr ptr);
332 
337  void unregisterDiagnostics(const std::string& name);
338 
344 
348  std::vector<std::string> getDiagnosticModules() const;
349 
353  StatusMap getStatusMap() const;
354 
358  DiagnosticsModule::StatusMap getStatusMap(const std::string& diagnosticModule) const;
359 
360  template <typename Reflector>
361  void reflect(Reflector& r)
362  {
363  } // do nothing here
364 
365 private:
366  typedef std::map<std::string, DiagnosticsModulePtr> ModuleMap;
367  // vector used for storing the order the modules were registered
368  std::vector<std::string> mModuleOrder;
369  mutable boost::mutex mModuleMutex;
370  ModuleMap mModules;
371  StatusMap mStatus;
372  std::string mID;
373 };
374 
376 
377 }
378 
379 #endif
void recover(const std::string &message, const std::string &trText="")
Signal that the module is recovering.
Manages the status of one or multiple modules inheriting from DiagnosticsModule.
Definition: Status.h:289
void registerDiagnostics(const std::string &name, DiagnosticsModulePtr ptr)
Register a new diagnostics module by specifying a name and a pointer to the module.
Status()
Definition: Status.h:84
Definition: Status.h:75
Definition: Status.h:79
void setHeartbeatInterval(const Duration &interval)
Set the watchdog interval.
Definition: Status.h:151
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:89
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
StatusMap mStatusMap
Definition: Status.h:277
Time and Duration wrapper class.
std::string category
category of that status
Definition: Status.h:120
void bootupFinished()
Signal that the module has finished booting up (There can still be errors but booting up is finished)...
boost::optional< Status > mRecoverStatus
Definition: Status.h:274
std::multimap< std::string, Status > StatusMap
Definition: Status.h:293
virtual ~DiagnosticsModule()
Definition: Status.h:146
Provides MIRA_CLASS_SERIALIZATION macro and includes the major headers of the serialization framework...
StatusMap getStatusMap() const
Gets the current status map containing all errors, warnings and bootup messages.
Duration getHeartbeatInterval() const
Return the watchdog interval.
Definition: Status.h:164
StatusMode
Different status levels sorted from "okay" to "most severe".
Definition: Status.h:73
boost::optional< Time > mLastHeartbeat
Definition: Status.h:276
DiagnosticsModule(const std::string &name="")
Definition: Status.h:142
void unregisterDiagnostics(const std::string &name)
Unregister a diagnostics module.
bool setStatus(Status::StatusMode mode, const std::string &category, const std::string &message, const std::string &trText="")
Set the status of a given category.
std::string mName
Definition: Status.h:278
std::string message
the corresponding message
Definition: Status.h:126
Status entry class.
Definition: Status.h:69
boost::optional< Status > mBootUpStatus
Definition: Status.h:273
Use this class to represent time durations.
Definition: Time.h:106
Definition: Status.h:78
void reflect(Reflector &r)
Definition: Status.h:361
void recoverFinished()
Signal that the module has finished recovering (There can still be errors but recovering is finished)...
StatusMap getStatusMap() const
Gets the current status map.
static Time now()
Returns the current utc based time.
Definition: Time.h:481
Definition: Status.h:76
DiagnosticsModule * DiagnosticsModulePtr
Typedef of a DiagnosticsModule pointer.
Definition: Status.h:282
Definition: Status.h:77
void heartbeat()
When called for the first time heartbeat usage will be enabled.
Definition: Status.h:177
void bootup(const std::string &message, const std::string &trText="")
Signal that the module is booting up.
void setID(const std::string &id)
Set the id that is used for displaying status messages.
Definition: Status.h:309
bool hasHeartbeatTimeout() const
Returns true if heartbeat usage is enabled (by first call to heartbeat()) and last heartbeat was more...
Definition: Status.h:186
Duration mHeartbeatInterval
Definition: Status.h:275
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:296
StatusMode mode
the status flag (OK, WARNING or ERROR)
Definition: Status.h:117
bool error(const std::string &category, const std::string &message, const std::string &trText="")
Signal an error in a category.
bool warning(const std::string &category, const std::string &message, const std::string &trText="")
Signal a warning in a category.
Status::StatusMode getStatus() const
Get the status mode defined by the status of the watchdog (if used) and the bootup, error and warning methods.
Status::StatusMode getStatus() const
Get the overall status mode defined by the status of all registered diagnostic modules.
std::map< std::string, Status > StatusMap
Definition: Status.h:140
Base class for modules that want to use diagnostics and set the current status.
Definition: Status.h:136
void reflect(Reflector &r)
Definition: Status.h:106
std::string trText
Status text that can be used for translation of the error to other languages.
Definition: Status.h:123
bool operator==(const Status &other) const
Definition: Status.h:97
void ok(const std::string &category="")
Signal that a category contains no more errors.
std::vector< std::string > getDiagnosticModules() const
Get the list of all registered modules.
void setName(const std::string &name)
Definition: Status.h:156