iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LogHelpers.H
Go to the documentation of this file.
1 /*! @file
2  @author
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 #ifndef INCLUDE_NRT_CORE_DEBUGGING_DETAILS_LOGHELPERS_H
36 #define INCLUDE_NRT_CORE_DEBUGGING_DETAILS_LOGHELPERS_H
37 
38 #include <nrt/config.h>
39 
43 
44 #include <cstdlib>
45 #include <iostream>
46 #include <sstream>
47 #include <string>
48 #include <thread>
49 #include <mutex>
50 
51 namespace nrt
52 {
53  class Component;
54 
55  //! A generic logging class
56  /*! GenericLog is a policy-based logging class which relies on "policies" provided by template arguments to define the
57  behavior or the logger.
58 
59  @tparam LogLevelPolicy Specifies the destination for messages. See \ref LogLevelPolicies for some common policies
60 
61  \note Most users will just want to use the predefined macros described in Debugging/Log.H, which include
62  #NRT_DEBUG(msg), #NRT_INFO(msg), etc.
63 
64  \ingroup logging */
65  template <class LogLevelPolicy>
66  class GenericLog : public LogLevelPolicy
67  {
68  public:
69  //! Construct a new GenericLog, without any locality information
70  GenericLog();
71 
72  //! Construct a new GenericLog, adding a prefix to the log stream
73  GenericLog(std::string const & fullFileName, std::string const & functionName, nrt::Component * const component);
74 
75  //! Close the GenericLog, outputting the aggregated message
76  ~GenericLog();
77 
78  //! Overloaded stream input operator for any type that has operator<< defined for ostream.
79  template <class T>
80  GenericLog<LogLevelPolicy> & operator<<(T const & out_item) { itsLogStream << out_item; return *this; }
81 
82  //! Overload of operator<< for uint8 (displays it as an int rather than char)
83  GenericLog<LogLevelPolicy> & operator<<(uint8 const & out_item);
84 
85  //! Overload of operator<< for int8 (displays it as an int rather than char)
86  GenericLog<LogLevelPolicy> & operator<<(int8 const & out_item);
87 
88  private:
89  static std::mutex outputMutex;
90  std::ostringstream itsLogStream;
91  };
92 
93  template <class LogLevelPolicy> std::mutex GenericLog<LogLevelPolicy>::outputMutex;
94 
95  /*! \defgroup LogOutputPolicies Log Output Policies
96 
97  A GenericLog output policy specifies where a log message will go. For example, the StandardErrorOutputPolicy sends
98  all log output to standard error.
99 
100  Policy Requirement:
101  - void output(std::stringstream &stream) Takes a stream, and delivers it to the policy's endpoint
102 
103  \note Most users will just want to use the predefined macros described in Debugging/Log.H, which include
104  #NRT_DEBUG(msg), #NRT_INFO(msg), etc.
105 
106  \ingroup logging */
107  /*@{*/
108  //! Output to standard error stream
110  {
111  //! Output to standard error stream
112  void output(std::ostringstream & stream);
113  };
114 
115  //! Output to standard output stream
117  {
118  //! Output to standard output stream
119  void output(std::ostringstream & stream);
120  };
121  /*@}*/
122 
123  namespace exception
124  {
125  //! Fatal exception class that is thronw by #NRT_FATAL(msg) and others using the nrt::FatalLevelPolicy
126  /*! \ingroup logging */
128  {
129  public:
130  //! Constructor
131  inline FatalException() throw()
132  try : nrt::exception::Exception("NRT Fatal Exception") { } catch (...) { }
133 
134  //! Destructor
135  virtual inline ~FatalException() throw() { }
136  };
137  }
138 
139  /*! \defgroup LogLevelPolicies Log Level Policies
140  A log level policy specifies a numeric level which will be compared against nrt::currentLogLevel when deciding
141  whether or not to output the log message.
142 
143  Policy Requirement:
144  - std::string getName() Returns the string name of the log level (i.e. "DEBUG", or "WARNING")
145  - int getLevel() Returns the priority level of this policy, where lower levels specify a higher importance. For
146  example, a fatal policy should be at level 0.
147  - void postOutput() This function is executed after the message has been outputted. For example, FatalLevelPolicy
148  executes an exit(-1) in this function.
149 
150  \note Most users will just want to use the predefined macros described in Debugging/Log.H, which include
151  #NRT_DEBUG(msg), #NRT_INFO(msg), etc.
152 
153  \ingroup logging */
154  /*! @{ */
155 
156  //! Blackboard debug-level log output policy
158  {
159  static constexpr char const * name = "BBDEBUG"; //!< Name of the policy
160  static constexpr int level = 8; //!< Level of the policy (see syslog)
161  static constexpr char const * color = NRT_TERM_MAGENTA; //!< Color for messages issued under this policy
162  static void postOutput() { /* Do Nothing */ } //!< What to do after the message has been issued
163  };
164 
165  //! Debug-level log output policy
167  {
168  static constexpr char const * name = "DEBUG"; //!< Name of the policy
169  static constexpr int level = 7; //!< Level of the policy (see syslog)
170  static constexpr char const * color = NRT_TERM_GREEN; //!< Color for messages issued under this policy
171  static void postOutput() { /* Do Nothing */ } //!< What to do after the message has been issued
172  };
173 
174  //! Info-level log output policy
176  {
177  static constexpr char const * name = "INFO"; //!< Name of the policy
178  static constexpr int level = 6; //!< Level of the policy (see syslog)
179  static constexpr char const * color = NRT_TERM_BLUE; //!< Color for messages issued under this policy
180  static void postOutput() { /* Do Nothing */ } //!< What to do after the message has been issued
181  };
182 
183  //! Warning-level log output policy
185  {
186  static constexpr char const * name = "WARN"; //!< Name of the policy
187  static constexpr int level = 4; //!< Level of the policy (see syslog)
188  static constexpr char const * color = NRT_TERM_YELLOW; //!< Color for messages issued under this policy
189  static void postOutput() { /* Do Nothing */ } //!< What to do after the message has been issued
190  };
191 
192  //! Fatal-level log output policy
194  {
195  static constexpr char const * name = "FATAL"; //!< Name of the policy
196  static constexpr int level = 0; //!< Level of the policy (see syslog)
197  static constexpr char const * color = NRT_TERM_RED; //!< Color for messages issued under this policy
198  static void postOutput() { throw nrt::exception::FatalException(); } //!< What to do after message has been issued
199  };
200 
201  /*! @} */
202 
203 } //end nrt
204 
205 #endif // INCLUDE_NRT_CORE_DEBUGGING_DETAILS_LOGHELPERS_H