OrocosComponentLibrary  2.8.3
Category.cpp
1 #include "logging/Category.hpp"
2 #include <rtt/Logger.hpp>
3 #include <rtt/ConnPolicy.hpp>
4 #include <log4cpp/NDC.hh>
5 #include <log4cpp/HierarchyMaintainer.hh>
6 
7 using namespace RTT;
8 
9 namespace OCL {
10 namespace logging {
11 
12 Category::Category(const std::string& name,
13  log4cpp::Category* parent,
14  log4cpp::Priority::Value priority) :
15  log4cpp::Category(name, parent, priority),
16  log_port( convertName(name) , false )
17 {
18 }
19 
20 Category::~Category()
21 {
22 }
23 
24 void Category::log(log4cpp::Priority::Value priority,
25  const RTT::rt_string& message) throw()
26 {
27  if (isPriorityEnabled(priority))
28  {
29  _logUnconditionally2(priority, message);
30  }
31 }
32 
33 void Category::debug(const RTT::rt_string& message) throw()
34 {
35  if (isPriorityEnabled(log4cpp::Priority::DEBUG))
36  _logUnconditionally2(log4cpp::Priority::DEBUG, message);
37 }
38 
39 void Category::info(const RTT::rt_string& message) throw()
40 {
41  if (isPriorityEnabled(log4cpp::Priority::INFO))
42  _logUnconditionally2(log4cpp::Priority::INFO, message);
43 }
44 
45 void Category::notice(const RTT::rt_string& message) throw()
46 {
47  if (isPriorityEnabled(log4cpp::Priority::NOTICE))
48  _logUnconditionally2(log4cpp::Priority::NOTICE, message);
49 }
50 
51 void Category::warn(const RTT::rt_string& message) throw()
52 {
53  if (isPriorityEnabled(log4cpp::Priority::WARN))
54  _logUnconditionally2(log4cpp::Priority::WARN, message);
55 }
56 
57 void Category::error(const RTT::rt_string& message) throw()
58 {
59  if (isPriorityEnabled(log4cpp::Priority::ERROR))
60  _logUnconditionally2(log4cpp::Priority::ERROR, message);
61 }
62 
63 void Category::crit(const RTT::rt_string& message) throw()
64 {
65  if (isPriorityEnabled(log4cpp::Priority::CRIT))
66  _logUnconditionally2(log4cpp::Priority::CRIT, message);
67 }
68 
69 void Category::alert(const RTT::rt_string& message) throw()
70 {
71  if (isPriorityEnabled(log4cpp::Priority::ALERT))
72  _logUnconditionally2(log4cpp::Priority::ALERT, message);
73 }
74 
75 void Category::emerg(const RTT::rt_string& message) throw()
76 {
77  if (isPriorityEnabled(log4cpp::Priority::EMERG))
78  _logUnconditionally2(log4cpp::Priority::EMERG, message);
79 }
80 
81 void Category::fatal(const RTT::rt_string& message) throw()
82 {
83  if (isPriorityEnabled(log4cpp::Priority::FATAL))
84  _logUnconditionally2(log4cpp::Priority::FATAL, message);
85 }
86 
87 
88 void Category::_logUnconditionally2(log4cpp::Priority::Value priority,
89  const RTT::rt_string& message) throw()
90 {
91  try
92  {
93  OCL::logging::LoggingEvent event(RTT::rt_string(getName().c_str()),
94  RTT::rt_string(message.c_str()),
95  // NDC's are not real-time
96 // RTT::rt_string(log4cpp::NDC::get().c_str()),
97  RTT::rt_string(""),
98  priority);
99  callAppenders(event);
100  }
101  catch (std::bad_alloc& e)
102  {
103  // \todo do what?
104 // std::cerr << "Failed to log event: out of real-time memory" << std::endl;
106  }
107 }
108 
110 {
111  log_port.write( event );
112 
113  // let our parent categories append (if they want to)
114  if (getAdditivity() && (getParent() != NULL))
115  {
116  OCL::logging::Category* parent = dynamic_cast<OCL::logging::Category*>(getParent());
117  if (parent)
118  {
119  parent->callAppenders(event);
120  }
121  // else we don't use non-realtime parent Category's!
122  }
123 }
124 
125 std::string Category::convertName(const std::string& name)
126 {
127  std::string rc(name);
128 
129  std::replace_if(rc.begin(),
130  rc.end(),
131  std::bind2nd(std::equal_to<char>(), '.'),
132  '_');
133 
134  return rc;
135 }
136 
137 log4cpp::Category* Category::createOCLCategory(const std::string& name,
138  log4cpp::Category* parent,
139  log4cpp::Priority::Value priority)
140 {
141  // do _NOT_ log from within this function! You will cause a lockup due to
142  // recursive calls to log4cpp, if you use RTT w/ log4cpp support.
143 
144  // \todo try catch on memory exceptions or failures?
145  OCL::logging::Category* c = new OCL::logging::Category(name, parent, priority);
146  return c;
147 }
148 
149 CategoryStream Category::getRTStream(log4cpp::Priority::Value priority)
150 {
151  return CategoryStream(this, isPriorityEnabled(priority) ?
152  priority : log4cpp::Priority::NOTSET);
153 }
154 
155 bool Category::connectToLogPort(RTT::base::PortInterface& otherPort)
156 {
157  return otherPort.connectTo(&log_port);
158 }
159 
160 bool Category::connectToLogPort(RTT::base::PortInterface& otherPort,
161  RTT::ConnPolicy& cp)
162 {
163  return otherPort.connectTo(&log_port, cp);
164 }
165 
166 // namespaces
167 }
168 }
A mirror of log4cpp::LoggingEvent, except using real-time capable strings.
static log4cpp::Category * createOCLCategory(const std::string &name, log4cpp::Category *parent, log4cpp::Priority::Value priority)
Factory function for log4cpp::HierarchyMaintainer Creates an OCL logging category.
Definition: Category.cpp:137
bool connectToLogPort(RTT::base::PortInterface &otherPort)
Connect otherPort to log_port.
Definition: Category.cpp:155
static std::string convertName(const std::string &name)
Convert name into Orocos notation (e.g.
Definition: Category.cpp:125
This is a utility class which you can use to stream messages into a category object.
void _logUnconditionally2(log4cpp::Priority::Value priority, const RTT::rt_string &message)
Definition: Category.cpp:88
RTT::InputPort< OCL::logging::LoggingEvent > log_port
Port we receive logging events on Initially unconnected.
Definition: Appender.hpp:47
The Orocos Component Library.
Definition: Component.hpp:43
A real-time capable category.
Definition: Category.hpp:25
Definition: Category.hpp:10
virtual void callAppenders(const OCL::logging::LoggingEvent &event)
Send event to all attached appenders.
Definition: Category.cpp:109
CategoryStream getRTStream(log4cpp::Priority::Value priority)
Returns a stream-like object into which you can log arbitrary data which supports the operator<<()...
Definition: Category.cpp:149