Appender.cpp
Go to the documentation of this file.
1 #include "logging/Appender.hpp"
2 #include "ocl/Component.hpp"
3 
4 #include <log4cpp/Appender.hh>
5 #include <log4cpp/BasicLayout.hh>
8 
9 namespace OCL {
10 namespace logging {
11 
12 Appender::Appender(std::string name) :
13  RTT::TaskContext(name, RTT::TaskContext::PreOperational),
14  appender(0),
15  layoutName_prop("LayoutName", "Layout name (e.g. 'simple', 'pattern')"),
16  layoutPattern_prop("LayoutPattern", "Layout conversion pattern (for those layouts that use a pattern)"),
17  countMaxPopped(0)
18 {
19  ports()->addEventPort("LogPort", log_port );
20 
23 }
24 
26 {
27 }
28 
30 {
31  bool rc;
32  const std::string& layoutName = layoutName_prop.rvalue();
33  const std::string& layoutPattern = layoutPattern_prop.rvalue();
34 
35  rc = true; // prove otherwise
36  if (appender &&
37  (!layoutName.empty()))
38  {
39  // \todo layout factory??
40  if (0 == layoutName.compare("basic"))
41  {
43  }
44  else if (0 == layoutName.compare("simple"))
45  {
47  }
48  else if (0 == layoutName.compare("pattern"))
49  {
52  layout->setConversionPattern(layoutPattern);
53  appender->setLayout(layout);
54  // the layout is now owned by the appender, and will be deleted
55  // by it when the appender is destroyed
56  }
57  else
58  {
59  RTT::log(RTT::Error) << "Invalid layout '" << layoutName
60  << "' in configuration for category: "
61  << getName() << RTT::endlog();
62  rc = false;
63  }
64  }
65 
66  return rc;
67 }
68 
70 {
72 // return log_port.ready();
73 
74  return true;
75 }
76 
78 {
79  drainBuffer();
80 
81  // introduce event to log diagnostics
82  if (0 != appender)
83  {
84  /* place a "#" at the front of the message, for appenders that are
85  reporting data for post-processing. These particular appenders
86  don't prepend the time data (it's one at time of sampling).
87  This way gnuplot, etc., ignore this diagnostic data.
88  */
89  std::stringstream ss;
90  ss << "# countMaxPopped=" << countMaxPopped;
91  log4cpp::LoggingEvent ev("OCL.logging.Appender",
92  ss.str(),
93  "",
95  appender->doAppend(ev);
96  }
97 }
98 
100 {
101  processEvents(0);
102 }
103 
105 {
106  if (!log_port.connected()) return; // no category connected to us
107  if (!appender) return; // no appender!?
108 
109  // check pre-conditions
110  if (0 > n) n = 1;
111 
112  /* Consume waiting events until
113  a) the buffer is empty
114  b) we consume enough events
115  */
116  bool again = false;
117  int count = 0;
118 
119  do
120  {
121  if (log_port.read( event ) == RTT::NewData)
122  {
123  ++count;
124 
126 
127  // Consume infinite events OR up to n events
128  again = (0 == n) || (count < n);
129  if ((0 != n) && (count == n)) ++countMaxPopped;
130  }
131  else
132  {
133  break; // nothing to do
134  }
135  }
136  while (again);
137 }
138 
139 // namespaces
140 }
141 }
142 
log4cpp::Appender * appender
Appender created by derived class.
Definition: Appender.hpp:52
OCL::logging::LoggingEvent event
Definition: Appender.hpp:67
virtual void doAppend(const LoggingEvent &event)=0
RTT::Property< std::string > layoutName_prop
Layout name (e.g. "simple", "basic", "pattern")
Definition: Appender.hpp:56
RTT::Property< std::string > layoutPattern_prop
Layout conversion pattern (for those layouts that use a pattern)
Definition: Appender.hpp:58
const_reference_t rvalue() const
Appender(std::string name)
Definition: Appender.cpp:12
RTT::InputPort< OCL::logging::LoggingEvent > log_port
Definition: Appender.hpp:49
virtual bool startHook()
ensure port is connected before we start
Definition: Appender.cpp:69
virtual void setConversionPattern(const std::string &conversionPattern)
base::InputPortInterface & addEventPort(const std::string &name, base::InputPortInterface &port, SlotFunction callback=SlotFunction())
virtual bool configureLayout()
Definition: Appender.cpp:29
DataFlowInterface * ports()
Property< T > & addProperty(const std::string &name, T &attr)
unsigned int countMaxPopped
Definition: Appender.hpp:70
virtual void processEvents(int n)
Definition: Appender.cpp:104
ORO_LIST_COMPONENT_TYPE(OCL::logging::Appender)
PropertyBag * properties()
virtual void stopHook()
Drain the buffer.
Definition: Appender.cpp:77
log4cpp::LoggingEvent toLog4cpp()
static Logger & log()
virtual void setLayout(Layout *layout)=0
static Logger::LogFunction endlog()
virtual void drainBuffer()
Definition: Appender.cpp:99
virtual const std::string & getName() const


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