Category.cpp
Go to the documentation of this file.
1 #include "logging/Category.hpp"
2 #include <rtt/Logger.hpp>
3 #include <rtt/ConnPolicy.hpp>
4 #include <log4cpp/NDC.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 
21 {
22 }
23 
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 {
37 }
38 
39 void Category::info(const RTT::rt_string& message) throw()
40 {
43 }
44 
45 void Category::notice(const RTT::rt_string& message) throw()
46 {
49 }
50 
51 void Category::warn(const RTT::rt_string& message) throw()
52 {
55 }
56 
57 void Category::error(const RTT::rt_string& message) throw()
58 {
61 }
62 
63 void Category::crit(const RTT::rt_string& message) throw()
64 {
67 }
68 
69 void Category::alert(const RTT::rt_string& message) throw()
70 {
73 }
74 
75 void Category::emerg(const RTT::rt_string& message) throw()
76 {
79 }
80 
81 void Category::fatal(const RTT::rt_string& message) throw()
82 {
85 }
86 
87 
89  const RTT::rt_string& message) throw()
90 {
91  try
92  {
94  message,
95  priority);
96  callAppenders(event);
97  }
98  catch (std::bad_alloc& e)
99  {
100  // \todo do what?
101 // std::cerr << "Failed to log event: out of real-time memory" << std::endl;
103  }
104 }
105 
107 {
108  // only write if the port is connected
109  if (log_port.connected())
110  {
111  log_port.write( event );
112  }
113 
114  // let our parent categories append (if they want to)
115  if (getAdditivity() && (getParent() != NULL))
116  {
117  OCL::logging::Category* parent = dynamic_cast<OCL::logging::Category*>(getParent());
118  if (parent)
119  {
120  parent->callAppenders(event);
121  }
122  // else we don't use non-realtime parent Category's!
123  }
124 }
125 
126 std::string Category::convertName(const std::string& name)
127 {
128  std::string rc(name);
129 
130  std::replace_if(rc.begin(),
131  rc.end(),
132  std::bind2nd(std::equal_to<char>(), '.'),
133  '_');
134 
135  return rc;
136 }
137 
139  log4cpp::Category* parent,
140  log4cpp::Priority::Value priority)
141 {
142  // do _NOT_ log from within this function! You will cause a lockup due to
143  // recursive calls to log4cpp, if you use RTT w/ log4cpp support.
144 
145  // \todo try catch on memory exceptions or failures?
146  OCL::logging::Category* c = new OCL::logging::Category(name, parent, priority);
147  return c;
148 }
149 
151 {
152  return CategoryStream(this, isPriorityEnabled(priority) ?
153  priority : log4cpp::Priority::NOTSET);
154 }
155 
157 {
158  return otherPort.connectTo(&log_port);
159 }
160 
162  RTT::ConnPolicy& cp)
163 {
164  return otherPort.connectTo(&log_port, cp);
165 }
166 
167 // namespaces
168 }
169 }
A mirror of log4cpp::LoggingEvent, except using real-time capable strings.
void fatal(const RTT::rt_string &message)
Definition: Category.cpp:81
virtual void log(log4cpp::Priority::Value priority, const RTT::rt_string &message)
Definition: Category.cpp:24
RTT::OutputPort< OCL::logging::LoggingEvent > log_port
Definition: Category.hpp:162
void debug(const RTT::rt_string &message)
Definition: Category.cpp:33
virtual Category * getParent()
std::basic_string< char, std::char_traits< char >, RTT::os::rt_allocator< char > > rt_string
static std::string convertName(const std::string &name)
Definition: Category.cpp:126
void emerg(const RTT::rt_string &message)
Definition: Category.cpp:75
void alert(const RTT::rt_string &message)
Definition: Category.cpp:69
CategoryStream getRTStream(log4cpp::Priority::Value priority)
Definition: Category.cpp:150
void info(const RTT::rt_string &message)
Definition: Category.cpp:39
virtual const std::string & getName() const
static log4cpp::Category * createOCLCategory(const std::string &name, log4cpp::Category *parent, log4cpp::Priority::Value priority)
Definition: Category.cpp:138
bool connectToLogPort(RTT::base::PortInterface &otherPort)
Definition: Category.cpp:156
void warn(const RTT::rt_string &message)
Definition: Category.cpp:51
void notice(const RTT::rt_string &message)
Definition: Category.cpp:45
virtual bool getAdditivity() const
void crit(const RTT::rt_string &message)
Definition: Category.cpp:63
virtual bool isPriorityEnabled(Priority::Value priority) const
void error(const RTT::rt_string &message)
Definition: Category.cpp:57
virtual bool connectTo(PortInterface *other, ConnPolicy const &policy)=0
void _logUnconditionally2(log4cpp::Priority::Value priority, const RTT::rt_string &message)
Definition: Category.cpp:88
class OCL_API Category
virtual void callAppenders(const OCL::logging::LoggingEvent &event)
Definition: Category.cpp:106


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