00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef HMI_CONSOLE_OUTPUT_HPP
00029 #define HMI_CONSOLE_OUTPUT_HPP
00030
00031 #include <rtt/TaskContext.hpp>
00032 #include <rtt/PeriodicActivity.hpp>
00033 #include <rtt/Method.hpp>
00034 #include <rtt/Logger.hpp>
00035 #include <rtt/os/MutexLock.hpp>
00036 #include <sstream>
00037 #include <iostream>
00038
00039 #include <ocl/OCL.hpp>
00040
00041 namespace OCL
00042 {
00051 class HMIConsoleOutput
00052 : public RTT::TaskContext
00053 {
00054 std::string coloron;
00055 std::string coloroff;
00056 std::string _prompt;
00057 std::ostringstream messages;
00058 std::ostringstream backup;
00059 std::ostringstream logmessages;
00060 std::ostringstream logbackup;
00061
00062 RTT::OS::Mutex msg_lock;
00063 RTT::OS::Mutex log_lock;
00064
00065 public :
00066 HMIConsoleOutput( const std::string& name = "cout")
00067 : TaskContext( name ),
00068 coloron("\033[1;34m"), coloroff("\033[0m"),
00069 _prompt("HMIConsoleOutput :\n")
00070 {
00071 this->clear();
00072
00073 this->methods()->addMethod( method( "display", &HMIConsoleOutput::display, this),
00074 "Display a message on the console",
00075 "message","The message to be displayed"
00076 );
00077 this->methods()->addMethod( method( "displayBool", &HMIConsoleOutput::displayBool, this),
00078 "Display a boolean on the console",
00079 "boolean","The Boolean to be displayed"
00080 );
00081 this->methods()->addMethod( method( "displayInt", &HMIConsoleOutput::displayInt, this),
00082 "Display a integer on the console",
00083 "integer","The Integer to be displayed"
00084 );
00085 this->methods()->addMethod( method( "displayDouble", &HMIConsoleOutput::displayDouble, this),
00086 "Display a double on the console",
00087 "double","The Double to be displayed"
00088 );
00089 this->methods()->addMethod( method( "log", &HMIConsoleOutput::log, this),
00090 "Log a message on the console",
00091 "message","The message to be logged"
00092 );
00093 this->methods()->addMethod( method( "logBool", &HMIConsoleOutput::logBool, this),
00094 "Log a boolean on the console",
00095 "boolean","The Boolean to be logged"
00096 );
00097 this->methods()->addMethod( method( "logInt", &HMIConsoleOutput::logInt, this),
00098 "Log a integer on the console",
00099 "integer","The Integer to be logged"
00100 );
00101 this->methods()->addMethod( method( "logDouble", &HMIConsoleOutput::logDouble, this),
00102 "Log a double on the console",
00103 "double","The Double to be logged"
00104 );
00105
00106 }
00107
00108 ~HMIConsoleOutput()
00109 {
00110 this->stop();
00111 }
00112
00113 void updateHook()
00114 {
00115 {
00116 RTT::OS::MutexLock lock1( msg_lock );
00117 if ( ! messages.str().empty() ) {
00118 std::cout << coloron << _prompt<< coloroff <<
00119 messages.str() << std::endl;
00120 messages.rdbuf()->str("");
00121 }
00122 }
00123 {
00124 RTT::OS::MutexLock lock1( log_lock );
00125 if ( ! logmessages.str().empty() ) {
00126 RTT::log(RTT::Info) << logmessages.str() << RTT::endlog();
00127 logmessages.rdbuf()->str("");
00128 }
00129 }
00130 }
00131
00135 void enableColor(bool yesno = true)
00136 {
00137 if (yesno == true) {
00138 coloron = "\033[1;34m";
00139 coloroff = "\033[0m";
00140 } else {
00141 coloron.clear();
00142 coloroff.clear();
00143 }
00144 }
00145
00149 void setPrompt(const std::string& prompt)
00150 {
00151 _prompt = prompt;
00152 }
00153
00154
00158 void display(const std::string & what)
00159 {
00160 this->enqueue( what );
00161 }
00162
00168 template<class T>
00169 void enqueue( const T& what )
00170 {
00171 {
00172 RTT::OS::MutexTryLock try_lock(msg_lock);
00173 if (try_lock.isSuccessful())
00174 {
00175
00176 messages << backup.str();
00177 messages << what << std::endl;
00178 backup.rdbuf()->str("");
00179 }
00180 else
00181
00182 backup << what << std::endl;
00183 }
00184
00185
00186 if (this->engine()->getActivity())
00187 this->engine()->getActivity()->trigger();
00188
00189 }
00190
00194 void displayBool(bool what)
00195 {
00196 this->enqueue( what );
00197 }
00198
00202 void displayInt( int what)
00203 {
00204 this->enqueue( what );
00205 }
00206
00210 void displayDouble( double what )
00211 {
00212 this->enqueue( what );
00213 }
00214
00215 template<class T>
00216 void dolog( const T& what )
00217 {
00218 {
00219 RTT::OS::MutexTryLock try_lock(log_lock);
00220 if (try_lock.isSuccessful())
00221 {
00222
00223 logmessages << logbackup.str();
00224 logmessages << what;
00225 logbackup.rdbuf()->str("");
00226 }
00227 else
00228
00229 logbackup << what;
00230 }
00231
00232
00233 if (this->engine()->getActivity())
00234 this->engine()->getActivity()->trigger();
00235 }
00236
00237
00238 void log(const std::string & what)
00239 {
00240 this->dolog( what );
00241 }
00245 void logBool(bool what)
00246 {
00247 this->dolog( what );
00248 }
00249
00253 void logInt( int what)
00254 {
00255 this->dolog( what );
00256 }
00257
00261 void logDouble( double what )
00262 {
00263 this->dolog( what );
00264 }
00265
00266 };
00267
00268 }
00269
00270 #endif