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