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 OCL_API 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->methods()->addMethod( method( "display", &HMIConsoleOutput::display, this),
00072 "Display a message on the console",
00073 "message","The message to be displayed"
00074 );
00075 this->methods()->addMethod( method( "displayBool", &HMIConsoleOutput::displayBool, this),
00076 "Display a boolean on the console",
00077 "boolean","The Boolean to be displayed"
00078 );
00079 this->methods()->addMethod( method( "displayInt", &HMIConsoleOutput::displayInt, this),
00080 "Display a integer on the console",
00081 "integer","The Integer to be displayed"
00082 );
00083 this->methods()->addMethod( method( "displayDouble", &HMIConsoleOutput::displayDouble, this),
00084 "Display a double on the console",
00085 "double","The Double to be displayed"
00086 );
00087 this->methods()->addMethod( method( "log", &HMIConsoleOutput::log, this),
00088 "Log a message on the console",
00089 "message","The message to be logged"
00090 );
00091 this->methods()->addMethod( method( "logBool", &HMIConsoleOutput::logBool, this),
00092 "Log a boolean on the console",
00093 "boolean","The Boolean to be logged"
00094 );
00095 this->methods()->addMethod( method( "logInt", &HMIConsoleOutput::logInt, this),
00096 "Log a integer on the console",
00097 "integer","The Integer to be logged"
00098 );
00099 this->methods()->addMethod( method( "logDouble", &HMIConsoleOutput::logDouble, this),
00100 "Log a double on the console",
00101 "double","The Double to be logged"
00102 );
00103
00104 }
00105
00106 ~HMIConsoleOutput()
00107 {
00108 this->stop();
00109 }
00110
00111 void updateHook()
00112 {
00113 {
00114 RTT::OS::MutexLock lock1( msg_lock );
00115 if ( ! messages.str().empty() ) {
00116 std::cout << coloron << _prompt<< coloroff <<
00117 messages.str() << std::endl;
00118 messages.rdbuf()->str("");
00119 }
00120 }
00121 {
00122 RTT::OS::MutexLock lock1( log_lock );
00123 if ( ! logmessages.str().empty() ) {
00124 RTT::log(RTT::Info) << logmessages.str() << RTT::endlog();
00125 logmessages.rdbuf()->str("");
00126 }
00127 }
00128 }
00129
00133 void enableColor(bool yesno = true)
00134 {
00135 if (yesno == true) {
00136 coloron = "\033[1;34m";
00137 coloroff = "\033[0m";
00138 } else {
00139 coloron.clear();
00140 coloroff.clear();
00141 }
00142 }
00143
00147 void setPrompt(const std::string& prompt)
00148 {
00149 _prompt = prompt;
00150 }
00151
00152
00156 void display(const std::string & what)
00157 {
00158 this->enqueue( what );
00159 }
00160
00166 template<class T>
00167 void enqueue( const T& what )
00168 {
00169 {
00170 RTT::OS::MutexTryLock try_lock(msg_lock);
00171 if (try_lock.isSuccessful())
00172 {
00173
00174 messages << backup.str();
00175 messages << what << std::endl;
00176 backup.rdbuf()->str("");
00177 }
00178 else
00179
00180 backup << what << std::endl;
00181 }
00182
00183
00184 if (this->engine()->getActivity())
00185 this->engine()->getActivity()->trigger();
00186
00187 }
00188
00192 void displayBool(bool what)
00193 {
00194 this->enqueue( what );
00195 }
00196
00200 void displayInt( int what)
00201 {
00202 this->enqueue( what );
00203 }
00204
00208 void displayDouble( double what )
00209 {
00210 this->enqueue( what );
00211 }
00212
00213 template<class T>
00214 void dolog( const T& what )
00215 {
00216 {
00217 RTT::OS::MutexTryLock try_lock(log_lock);
00218 if (try_lock.isSuccessful())
00219 {
00220
00221 logmessages << logbackup.str();
00222 logmessages << what;
00223 logbackup.rdbuf()->str("");
00224 }
00225 else
00226
00227 logbackup << what;
00228 }
00229
00230
00231 if (this->engine()->getActivity())
00232 this->engine()->getActivity()->trigger();
00233 }
00234
00235
00236 void log(const std::string & what)
00237 {
00238 this->dolog( what );
00239 }
00243 void logBool(bool what)
00244 {
00245 this->dolog( what );
00246 }
00247
00251 void logInt( int what)
00252 {
00253 this->dolog( what );
00254 }
00255
00259 void logDouble( double what )
00260 {
00261 this->dolog( what );
00262 }
00263
00264 };
00265
00266 }
00267
00268 #endif