HMIConsoleOutput.hpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: Peter Soetens  Thu Apr 22 20:40:59 CEST 2004  HMIConsoleOutput.hpp
00003 
00004                         HMIConsoleOutput.hpp -  description
00005                            -------------------
00006     begin                : Thu April 22 2004
00007     copyright            : (C) 2004 Peter Soetens
00008     email                : peter.soetens@mech.kuleuven.ac.be
00009 
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU Lesser General Public            *
00013  *   License as published by the Free Software Foundation; either          *
00014  *   version 2.1 of the License, or (at your option) any later version.    *
00015  *                                                                         *
00016  *   This library is distributed in the hope that it will be useful,       *
00017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00019  *   Lesser General Public License for more details.                       *
00020  *                                                                         *
00021  *   You should have received a copy of the GNU Lesser General Public      *
00022  *   License along with this library; if not, write to the Free Software   *
00023  *   Foundation, Inc., 59 Temple Place,                                    *
00024  *   Suite 330, Boston, MA  02111-1307  USA                                *
00025  *                                                                         *
00026  ***************************************************************************/
00027 
00028 #ifndef HMI_CONSOLE_OUTPUT_HPP
00029 #define HMI_CONSOLE_OUTPUT_HPP
00030 
00031 #include <rtt/TaskContext.hpp>
00032 #include <rtt/Activity.hpp>
00033 #include <rtt/Logger.hpp>
00034 #include <rtt/os/MutexLock.hpp>
00035 #include <sstream>
00036 #include <iostream>
00037 
00038 #include <ocl/OCL.hpp>
00039 
00040 namespace OCL
00041 {
00050     class OCL_API HMIConsoleOutput
00051         : public RTT::TaskContext
00052     {
00053         std::string coloron;
00054         std::string coloroff;
00055         std::string _prompt;
00056         std::ostringstream messages;
00057         std::ostringstream backup;
00058         std::ostringstream logmessages;
00059         std::ostringstream logbackup;
00060 
00061         RTT::os::Mutex msg_lock;
00062         RTT::os::Mutex log_lock;
00063 
00064     public :
00065         HMIConsoleOutput( const std::string& name = "cout")
00066             : RTT::TaskContext( name ),
00067               coloron("\033[1;34m"), coloroff("\033[0m"),
00068               _prompt("HMIConsoleOutput :\n")
00069         {
00070             this->addOperation("display", &HMIConsoleOutput::display, this, RTT::ClientThread).doc("Display a message on the console").arg("message", "The message to be displayed");
00071             this->addOperation("displayBool", &HMIConsoleOutput::displayBool, this, RTT::ClientThread).doc("Display a boolean on the console").arg("boolean", "The Boolean to be displayed");
00072             this->addOperation("displayInt", &HMIConsoleOutput::displayInt, this, RTT::ClientThread).doc("Display a integer on the console").arg("integer", "The Integer to be displayed");
00073             this->addOperation("displayDouble", &HMIConsoleOutput::displayDouble, this, RTT::ClientThread).doc("Display a double on the console").arg("double", "The Double to be displayed");
00074             this->addOperation("log", &HMIConsoleOutput::log, this, RTT::ClientThread).doc("Log a message on the console").arg("message", "The message to be logged");
00075             this->addOperation("logBool", &HMIConsoleOutput::logBool, this, RTT::ClientThread).doc("Log a boolean on the console").arg("boolean", "The Boolean to be logged");
00076             this->addOperation("logInt", &HMIConsoleOutput::logInt, this, RTT::ClientThread).doc("Log a integer on the console").arg("integer", "The Integer to be logged");
00077             this->addOperation("logDouble", &HMIConsoleOutput::logDouble, this, RTT::ClientThread).doc("Log a double on the console").arg("double", "The Double to be logged");
00078 
00079         }
00080 
00081         ~HMIConsoleOutput()
00082         {
00083             this->stop();
00084         }
00085 
00086         void updateHook()
00087         {
00088             {
00089                 RTT::os::MutexLock lock1( msg_lock );
00090                 if ( ! messages.str().empty() ) {
00091                     std::cout << coloron << _prompt<< coloroff <<
00092                         messages.str() << std::endl;
00093                     messages.rdbuf()->str("");
00094                 }
00095             }
00096             {
00097                 RTT::os::MutexLock lock1( log_lock );
00098                 if ( ! logmessages.str().empty() ) {
00099                     RTT::log(RTT::Info) << logmessages.str() << RTT::endlog();
00100                     logmessages.rdbuf()->str("");
00101                 }
00102             }
00103         }
00104 
00108         void enableColor(bool yesno = true)
00109         {
00110             if (yesno == true) {
00111                 coloron = "\033[1;34m";
00112                 coloroff = "\033[0m";
00113             } else {
00114                 coloron.clear();
00115                 coloroff.clear();
00116             }
00117         }
00118 
00122         void setPrompt(const std::string& prompt)
00123         {
00124             _prompt = prompt;
00125         }
00126 
00127 
00131         void display(const std::string & what)
00132         {
00133             this->enqueue( what );
00134         }
00135 
00141         template<class T>
00142         void enqueue( const T& what )
00143         {
00144             {
00145                 RTT::os::MutexTryLock try_lock( msg_lock );
00146                 if ( try_lock.isSuccessful() ) {
00147                     // we got the lock, copy everything...
00148                     messages << backup.str();
00149                     messages << what << std::endl;
00150                     backup.rdbuf()->str("");
00151                 }
00152                 else  // no lock, backup.
00153                     backup << what << std::endl;
00154                 // support for non periodic logging:
00155             }
00156             if ( this->engine()->getActivity() )
00157                 this->engine()->getActivity()->trigger();
00158         }
00159 
00163         void displayBool(bool what)
00164         {
00165             this->enqueue( what );
00166         }
00167 
00171         void displayInt( int what)
00172         {
00173             this->enqueue( what );
00174         }
00175 
00179         void displayDouble( double what )
00180         {
00181             this->enqueue( what );
00182         }
00183 
00184         template<class T>
00185         void dolog( const T& what )
00186         {
00187             {
00188                 RTT::os::MutexTryLock try_lock( log_lock );
00189                 if ( try_lock.isSuccessful() ) {
00190                     // we got the lock, copy everything...
00191                     logmessages << logbackup.str();
00192                     logmessages << what;
00193                     logbackup.rdbuf()->str("");
00194                 }
00195                 else  // no lock, backup.
00196                     logbackup << what;
00197             }
00198             if ( this->engine()->getActivity() )
00199                 this->engine()->getActivity()->trigger();
00200         }
00201 
00202 
00203         void log(const std::string & what)
00204         {
00205             this->dolog( what );
00206         }
00210         void logBool(bool what)
00211         {
00212             this->dolog( what );
00213         }
00214 
00218         void logInt( int what)
00219         {
00220             this->dolog( what );
00221         }
00222 
00226         void logDouble( double what )
00227         {
00228             this->dolog( what );
00229         }
00230 
00231     };
00232 
00233 }
00234 
00235 #endif


ocl
Author(s): OCL Development Team
autogenerated on Mon Sep 14 2015 14:21:46