HMIConsoleOutput.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Thu Apr 22 20:40:59 CEST 2004 HMIConsoleOutput.hpp
3 
4  HMIConsoleOutput.hpp - description
5  -------------------
6  begin : Thu April 22 2004
7  copyright : (C) 2004 Peter Soetens
8  email : peter.soetens@mech.kuleuven.ac.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU Lesser General Public *
13  * License as published by the Free Software Foundation; either *
14  * version 2.1 of the License, or (at your option) any later version. *
15  * *
16  * This library is distributed in the hope that it will be useful, *
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
19  * Lesser General Public License for more details. *
20  * *
21  * You should have received a copy of the GNU Lesser General Public *
22  * License along with this library; if not, write to the Free Software *
23  * Foundation, Inc., 59 Temple Place, *
24  * Suite 330, Boston, MA 02111-1307 USA *
25  * *
26  ***************************************************************************/
27 
28 #ifndef HMI_CONSOLE_OUTPUT_HPP
29 #define HMI_CONSOLE_OUTPUT_HPP
30 
31 #include <rtt/TaskContext.hpp>
32 #include <rtt/Activity.hpp>
33 #include <rtt/Logger.hpp>
34 #include <rtt/os/MutexLock.hpp>
35 #include <sstream>
36 #include <iostream>
37 
38 #include <ocl/OCL.hpp>
39 
40 namespace OCL
41 {
50  class OCL_API HMIConsoleOutput
51  : public RTT::TaskContext
52  {
53  std::string coloron;
54  std::string coloroff;
55  std::string _prompt;
60 
63 
64  public :
65  HMIConsoleOutput( const std::string& name = "cout")
66  : RTT::TaskContext( name ),
67  coloron("\033[1;34m"), coloroff("\033[0m"),
68  _prompt("HMIConsoleOutput :\n")
69  {
70  this->addOperation("display", &HMIConsoleOutput::display, this, RTT::ClientThread).doc("Display a message on the console").arg("message", "The message to be displayed");
71  this->addOperation("displayBool", &HMIConsoleOutput::displayBool, this, RTT::ClientThread).doc("Display a boolean on the console").arg("boolean", "The Boolean to be displayed");
72  this->addOperation("displayInt", &HMIConsoleOutput::displayInt, this, RTT::ClientThread).doc("Display a integer on the console").arg("integer", "The Integer to be displayed");
73  this->addOperation("displayDouble", &HMIConsoleOutput::displayDouble, this, RTT::ClientThread).doc("Display a double on the console").arg("double", "The Double to be displayed");
74  this->addOperation("log", &HMIConsoleOutput::log, this, RTT::ClientThread).doc("Log a message on the console").arg("message", "The message to be logged");
75  this->addOperation("logBool", &HMIConsoleOutput::logBool, this, RTT::ClientThread).doc("Log a boolean on the console").arg("boolean", "The Boolean to be logged");
76  this->addOperation("logInt", &HMIConsoleOutput::logInt, this, RTT::ClientThread).doc("Log a integer on the console").arg("integer", "The Integer to be logged");
77  this->addOperation("logDouble", &HMIConsoleOutput::logDouble, this, RTT::ClientThread).doc("Log a double on the console").arg("double", "The Double to be logged");
78 
79  }
80 
82  {
83  this->stop();
84  }
85 
86  void updateHook()
87  {
88  {
89  RTT::os::MutexLock lock1( msg_lock );
90  if ( ! messages.str().empty() ) {
91  std::cout << coloron << _prompt<< coloroff <<
92  messages.str() << std::endl;
93  messages.rdbuf()->str("");
94  }
95  }
96  {
97  RTT::os::MutexLock lock1( log_lock );
98  if ( ! logmessages.str().empty() ) {
99  RTT::log(RTT::Info) << logmessages.str() << RTT::endlog();
100  logmessages.rdbuf()->str("");
101  }
102  }
103  }
104 
108  void enableColor(bool yesno = true)
109  {
110  if (yesno == true) {
111  coloron = "\033[1;34m";
112  coloroff = "\033[0m";
113  } else {
114  coloron.clear();
115  coloroff.clear();
116  }
117  }
118 
122  void setPrompt(const std::string& prompt)
123  {
124  _prompt = prompt;
125  }
126 
127 
131  void display(const std::string & what)
132  {
133  this->enqueue( what );
134  }
135 
141  template<class T>
142  void enqueue( const T& what )
143  {
144  {
145  RTT::os::MutexTryLock try_lock( msg_lock );
146  if ( try_lock.isSuccessful() ) {
147  // we got the lock, copy everything...
148  messages << backup.str();
149  messages << what << std::endl;
150  backup.rdbuf()->str("");
151  }
152  else // no lock, backup.
153  backup << what << std::endl;
154  // support for non periodic logging:
155  }
156  if ( this->engine()->getActivity() )
157  this->engine()->getActivity()->trigger();
158  }
159 
163  void displayBool(bool what)
164  {
165  this->enqueue( what );
166  }
167 
171  void displayInt( int what)
172  {
173  this->enqueue( what );
174  }
175 
179  void displayDouble( double what )
180  {
181  this->enqueue( what );
182  }
183 
184  template<class T>
185  void dolog( const T& what )
186  {
187  {
188  RTT::os::MutexTryLock try_lock( log_lock );
189  if ( try_lock.isSuccessful() ) {
190  // we got the lock, copy everything...
191  logmessages << logbackup.str();
192  logmessages << what;
193  logbackup.rdbuf()->str("");
194  }
195  else // no lock, backup.
196  logbackup << what;
197  }
198  if ( this->engine()->getActivity() )
199  this->engine()->getActivity()->trigger();
200  }
201 
202 
203  void log(const std::string & what)
204  {
205  this->dolog( what );
206  }
210  void logBool(bool what)
211  {
212  this->dolog( what );
213  }
214 
218  void logInt( int what)
219  {
220  this->dolog( what );
221  }
222 
226  void logDouble( double what )
227  {
228  this->dolog( what );
229  }
230 
231  };
232 
233 }
234 
235 #endif
void displayInt(int what)
Display an integer on standard output.
std::ostringstream logbackup
void enableColor(bool yesno=true)
void displayBool(bool what)
Display a boolean on standard output.
void dolog(const T &what)
void logInt(int what)
Log an integer on standard output.
std::ostringstream logmessages
std::string str()
void logDouble(double what)
Log a double on standard output.
std::ostringstream messages
void displayDouble(double what)
Display a double on standard output.
void display(const std::string &what)
Display a message on standard output.
void setPrompt(const std::string &prompt)
This component can be used to display messages on the standard output.
std::ostringstream backup
void enqueue(const T &what)
static Logger & log()
HMIConsoleOutput(const std::string &name="cout")
ClientThread
static Logger::LogFunction endlog()
void logBool(bool what)
Log a boolean on standard output.
void log(const std::string &what)


ocl
Author(s): OCL Development Team
autogenerated on Wed Jun 26 2019 19:26:27