OrocosComponentLibrary  2.8.3
PrintService.cpp
1 #include <rtt/Service.hpp>
2 #include <rtt/Logger.hpp>
3 #include <iostream>
4 
5 #include <rtt/types/GlobalsRepository.hpp>
6 #include <rtt/types/Types.hpp>
7 #include <rtt/types/TypeInfoName.hpp>
8 #include <rtt/plugin/ServicePlugin.hpp>
9 #include "OCL.hpp"
10 
11 namespace OCL
12 {
13 
18  class PrintService: public RTT::Service
19  {
20  public:
21  PrintService(TaskContext* parent) :
22  RTT::Service("print", parent)
23  {
24  doc("A service that provides basic printing to std::cout, std::cerr and the RTT::Logger.");
25  // add the operations
26  addOperation("ln", &PrintService::println, this).doc(
27  "Prints a line to standard output.").arg("line",
28  "A string. Use a '+' to mix strings with numbers/variables.");
29  addOperation("err", &PrintService::printerr, this).doc(
30  "Prints a line to standard error.").arg("line",
31  "A string. Use a '+' to mix strings with numbers/variables.");
32  addOperation("log", &PrintService::printlog, this).doc(
33  "Prints a line to Orocos logger class.").arg("level","The LogLevel to use.").arg("line",
34  "A string. Use a '+' to mix strings with numbers/variables.");
35 
36  // add the log-levels as global variables.
37  if (types::Types()->type("LogLevel") == 0) {
38  types::Types()->addType( new types::TypeInfoName<Logger::LogLevel>("LogLevel") );
39  types::GlobalsRepository::shared_ptr globals = types::GlobalsRepository::Instance();
40 
41 
42  // Data Flow enums:
43  globals->setValue( new Constant<Logger::LogLevel>("Never",Logger::Never) );
44  globals->setValue( new Constant<Logger::LogLevel>("Error",Logger::Error) );
45  globals->setValue( new Constant<Logger::LogLevel>("Fatal",Logger::Fatal) );
46  globals->setValue( new Constant<Logger::LogLevel>("Critical",Logger::Critical) );
47  globals->setValue( new Constant<Logger::LogLevel>("Warning",Logger::Warning) );
48  globals->setValue( new Constant<Logger::LogLevel>("Info",Logger::Info) );
49  globals->setValue( new Constant<Logger::LogLevel>("Debug",Logger::Debug) );
50  globals->setValue( new Constant<Logger::LogLevel>("RealTime",Logger::RealTime) );
51  }
52  }
53 
54  void println(const std::string& arg)
55  {
56  std::cout << arg << std::endl;
57  }
58  void printerr(const std::string& arg)
59  {
60  std::cerr << arg << std::endl;
61  }
62  void printlog(Logger::LogLevel level, const std::string& arg)
63  {
64  log(LoggerLevel(level)) << arg <<endlog();
65  }
66  };
67 }
68 
69 ORO_SERVICE_NAMED_PLUGIN( OCL::PrintService, "print")
70 
A service that provides basic printing to std::cout, std::cerr and the RTT::Logger.
The Orocos Component Library.
Definition: Component.hpp:43