GestureRecognitionToolkit  Version: 0.2.0
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 
34 class GRT_API Log{
35 public:
36  Log(std::string proceedingText = ""){
37  setProceedingText(proceedingText);
38  loggingEnabledPtr = NULL;
39  instanceLoggingEnabled = true;
40  writeProceedingText = true;
41  writeProceedingTextPtr = &writeProceedingText;
42  lastMessagePtr = &lastMessage;
43  }
44 
45  virtual ~Log(){}
46 
47  template < class T >
48  const Log& operator<< (const T val ) const{
49 
50 #ifdef GRT_CXX11_ENABLED
51  std::unique_lock<std::mutex> lock( logMutex );
52 #endif
53 
54  if( *loggingEnabledPtr && instanceLoggingEnabled ){
55  if( *writeProceedingTextPtr ){
56  *writeProceedingTextPtr = false;
57  std::cout << proceedingText.c_str();
58  *lastMessagePtr = "";
59  }
60  std::cout << val;
61  std::stringstream stream;
62  stream << val;
63  *lastMessagePtr += stream.str();
64  }
65  return *this;
66  }
67 
68  // this is the type of std::cout
69  typedef std::basic_ostream<char, std::char_traits<char> > CoutType;
70 
71  // this is the function signature of std::endl
72  typedef CoutType& (*StandardEndLine)(CoutType&);
73 
74  // define an operator<< to take in std::endl
75  const Log& operator<<(const StandardEndLine manip) const{
76 
77 #ifdef GRT_CXX11_ENABLED
78  std::unique_lock<std::mutex> lock( logMutex );
79 #endif
80 
81  if( *loggingEnabledPtr && instanceLoggingEnabled ){
82  // call the function, but we cannot return it's value
83  manip(std::cout);
84  *writeProceedingTextPtr = true;
85 
86  //Trigger any logging callbacks
87  triggerCallback( lastMessage );
88  }
89 
90  return *this;
91  }
92 
93  //Getters
94  virtual bool getLoggingEnabled() const{ return false; }
95 
96  bool getInstanceLoggingEnabled() const { return instanceLoggingEnabled; };
97 
98  std::string getProceedingText() const{ return proceedingText; }
99 
100  virtual std::string getLastMessage() const{ return lastMessage; }
101 
102  //Setters
103  void setProceedingText(const std::string &proceedingText){
104  if( proceedingText.length() == 0 ) this->proceedingText = "";
105  else this->proceedingText = proceedingText + " ";
106  }
107 
108  bool setEnableInstanceLogging(bool loggingEnabled){
109  this->instanceLoggingEnabled = loggingEnabled;
110  return true;
111  }
112 
113 protected:
114  virtual void triggerCallback( const std::string &message ) const{
115  return;
116  }
117 
118  template< class T >
119  std::string to_str( const T &val ) const {
120  std::stringstream s;
121  s << val;
122  return s.str();
123  }
124 
125  std::string proceedingText;
126  std::string lastMessage;
127  bool instanceLoggingEnabled;
128  bool *loggingEnabledPtr;
129  bool *writeProceedingTextPtr;
130  std::string *lastMessagePtr;
131  bool writeProceedingText;
132 
133 #ifdef GRT_CXX11_ENABLED
134  static std::mutex logMutex;
135 #endif
136 };
137 
138 GRT_END_NAMESPACE
139 
140 #endif //GRT_LOG_HEADER
Definition: Log.h:34