GestureRecognitionToolkit  Version: 0.2.5
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
Log.h
1 /*
2 GRT MIT License
3 Copyright (c) <2012> <Nicholas Gillian, Media Lab, MIT>
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
6 and associated documentation files (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all copies or substantial
12 portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
15 LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
18 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 */
20 
21 #ifndef GRT_LOG_HEADER
22 #define GRT_LOG_HEADER
23 
24 #include "GRTTypedefs.h"
25 //Only include the C++ 11 code if C++11 support it is enabled
26 #ifdef GRT_CXX11_ENABLED
27 #include <atomic>
28 #include <thread>
29 #include <mutex>
30 #endif //GRT_CXX11_ENABLED
31 
32 GRT_BEGIN_NAMESPACE
33 
37 class GRT_API Log{
38 public:
39  typedef std::basic_ostream<char, std::char_traits<char> > CoutType;
40  typedef CoutType& (*StandardEndLine)(CoutType&);
41 
46  Log(const std::string &key = ""){
47  setKey(key);
48  instanceLoggingEnabled = true;
49  writeKey = true;
50  loggingEnabledPtr = &instanceLoggingEnabled;
51  writeKeyPtr = &writeKey;
52  lastMessagePtr = &lastMessage;
53  }
54 
59  Log(const Log &rhs){
60  this->key = rhs.key;
61  this->writeKey = rhs.writeKey;
62  this->lastMessage = rhs.lastMessage;
63  this->instanceLoggingEnabled = rhs.instanceLoggingEnabled;
64  this->loggingEnabledPtr = &(this->instanceLoggingEnabled);
65  this->writeKeyPtr = &(this->writeKey);
66  this->lastMessagePtr = &(this->lastMessage);
67  }
68 
69  virtual ~Log(){}
70 
76  Log& operator=(const Log &rhs){
77  if( this != &rhs ){
78  this->key = rhs.key;
79  this->writeKey = rhs.writeKey;
80  this->lastMessage = rhs.lastMessage;
81  this->instanceLoggingEnabled = rhs.instanceLoggingEnabled;
82  this->loggingEnabledPtr = &(this->instanceLoggingEnabled);
83  this->writeKeyPtr = &(this->writeKey);
84  this->lastMessagePtr = &(this->lastMessage);
85  }
86  return *this;
87  }
88 
92  template < class T >
93  const Log& operator<< (const T &val ) const{
94 
95 #ifdef GRT_CXX11_ENABLED
96  std::unique_lock<std::mutex> lock( logMutex );
97 #endif
98  if( !baseLoggingEnabled ) return *this; //If the base class global logging is disabled, then there is nothing to do
99 
100  if( *loggingEnabledPtr && instanceLoggingEnabled ){
101  if( *writeKeyPtr ){
102  *writeKeyPtr = false;
103  std::cout << key.c_str();
104  std::cout << " ";
105  *lastMessagePtr = ""; //Reset the last message
106  }
107  std::cout << val;
108  std::stringstream stream;
109  stream << val;
110  *lastMessagePtr += stream.str(); //Update the last message
111  }
112  return *this;
113  }
114 
118  const Log& operator<<(const StandardEndLine manip) const{
119 
120 #ifdef GRT_CXX11_ENABLED
121  std::unique_lock<std::mutex> lock( logMutex );
122 #endif
123  if( !baseLoggingEnabled ) return *this; //If the base class global logging is disabled, then there is nothing to do
124 
125  if( *loggingEnabledPtr && instanceLoggingEnabled ){
126  // call the function, but we cannot return it's value
127  manip(std::cout);
128  *writeKeyPtr = true; //The message is now complete, so toggle back the key
129 
130  //Trigger any logging callbacks
131  triggerCallback( lastMessage );
132  }
133 
134  return *this;
135  }
136 
141  virtual bool getInstanceLoggingEnabled() const {
142  return instanceLoggingEnabled;
143  }
144 
149  virtual std::string getKey() const {
150  return key;
151  }
152 
157  virtual std::string getLastMessage() const{
158  return lastMessage;
159  }
160 
166  virtual bool setKey(const std::string &key){
167  if( key.length() == 0 ) this->key = "";
168  else this->key = key;
169  return true;
170  }
171 
176  static bool getLoggingEnabled() {
177  return baseLoggingEnabled;
178  }
179 
185  return this->instanceLoggingEnabled;
186  }
187 
192  static bool setLoggingEnabled(const bool enabled) {
193  baseLoggingEnabled = enabled;
194  return true;
195  }
196 
201  virtual bool setInstanceLoggingEnabled(const bool loggingEnabled){
202  this->instanceLoggingEnabled = loggingEnabled;
203  return true;
204  }
205 
206  GRT_DEPRECATED_MSG("setProceedingText is deprecated, use setKey instead", void setProceedingText(std::string proceedingText) );
207  GRT_DEPRECATED_MSG("getProceedingText is deprecated, use getKey instead", std::string getProceedingText() const );
208  GRT_DEPRECATED_MSG("setEnableInstanceLogging is deprecated, use setInstanceLoggingEnabled instead", bool setEnableInstanceLogging(const bool loggingEnabled) );
209 
210 protected:
217  virtual void triggerCallback( const std::string &message ) const{ return; }
218 
219  std::string key;
220  std::string lastMessage;
221  bool writeKey;
223 
225  bool *writeKeyPtr;
226  std::string *lastMessagePtr;
227 
228  static bool baseLoggingEnabled;
229 
230 #ifdef GRT_CXX11_ENABLED
231  static std::mutex logMutex;
232 #endif
233 };
234 
235 GRT_END_NAMESPACE
236 
237 #endif //GRT_LOG_HEADER
static bool setLoggingEnabled(const bool enabled)
sets if logging is enabled for this class, this supersedes the specific instance logging ...
Definition: Log.h:192
std::basic_ostream< char, std::char_traits< char > > CoutType
this is the type of std::cout
Definition: Log.h:39
std::string key
The key that will be written at the start of each log.
Definition: Log.h:219
std::string * lastMessagePtr
This is a hack that enables variables to be updated inside const methods.
Definition: Log.h:226
static bool baseLoggingEnabled
This controls logging across all Log instances, as opposed to a single instance.
Definition: Log.h:228
std::string lastMessage
The last message written.
Definition: Log.h:220
virtual std::string getLastMessage() const
returns the last message written by the log
Definition: Log.h:157
virtual bool setKey(const std::string &key)
sets the key that gets written at the start of each message, this will be written in the format &#39;key ...
Definition: Log.h:166
virtual void triggerCallback(const std::string &message) const
This callback can be used to propagate messages to other interfaces (e.g., a GUI built on top of the ...
Definition: Log.h:217
Log(const std::string &key="")
defines the log default constructor
Definition: Log.h:46
virtual bool setInstanceLoggingEnabled(const bool loggingEnabled)
sets if logging is enabled for this specific instance
Definition: Log.h:201
virtual bool getInstanceLoggingEnabled() const
returns true if logging is enabled for this specific instance
Definition: Log.h:141
virtual bool getInstanceLoggingEnabled()
returns if logging is enabled for this specific instance
Definition: Log.h:184
virtual std::string getKey() const
returns the key that gets written at the start of each message
Definition: Log.h:149
bool * loggingEnabledPtr
This is a hack that enables variables to be updated inside const methods.
Definition: Log.h:224
Log(const Log &rhs)
defines the log copy constructor
Definition: Log.h:59
The Log class provides the base class for all GRT logging functionality.
Definition: Log.h:37
Log & operator=(const Log &rhs)
defines the log equals operator
Definition: Log.h:76
const Log & operator<<(const StandardEndLine manip) const
defines an operator<< to take in std::endl, this ends a message and triggers the mesaage callback ...
Definition: Log.h:118
static bool getLoggingEnabled()
returns true if logging is enabled for this class, this supersedes the specific instance logging ...
Definition: Log.h:176
bool writeKey
If true, then the key will be written at the start of each log message.
Definition: Log.h:221
bool * writeKeyPtr
This is a hack that enables variables to be updated inside const methods.
Definition: Log.h:225
bool instanceLoggingEnabled
If true, then this instance should log messages.
Definition: Log.h:222