OrocosComponentLibrary  2.9.0
GenerationalFileAppender.cpp
1 #include "logging/GenerationalFileAppender.hpp"
2 #include "ocl/Component.hpp"
3 #include <rtt/Logger.hpp>
4 
5 #include <log4cpp/GenerationalFileAppender.hh>
6 
7 using namespace RTT;
8 
9 namespace OCL {
10 namespace logging {
11 
12 GenerationalFileAppender::GenerationalFileAppender(std::string name) :
13  OCL::logging::Appender(name),
14  advanceGeneration_op("advanceGeneration", &GenerationalFileAppender::advanceGeneration, this, RTT::OwnThread),
15  filename_prop("Filename", "Name of file to log to"),
16  maxEventsPerCycle_prop("MaxEventsPerCycle",
17  "Maximum number of log events to pop per cycle",
18  1),
19  maxEventsPerCycle(1)
20 {
21  provides()->addOperation(advanceGeneration_op).doc("Advance to the next logfile generation");
22 
23  properties()->addProperty(filename_prop);
24  properties()->addProperty(maxEventsPerCycle_prop);
25 }
26 
27 GenerationalFileAppender::~GenerationalFileAppender()
28 {
29 }
30 
31 bool GenerationalFileAppender::configureHook()
32 {
33  // verify valid limits
34  int m = maxEventsPerCycle_prop.rvalue();
35  if ((0 > m))
36  {
37  log(Error) << "Invalid maxEventsPerCycle value of "
38  << m << ". Value must be >= 0."
39  << endlog();
40  return false;
41  }
42  maxEventsPerCycle = m;
43 
44  // \todo error checking
45 
46  appender = new log4cpp::GenerationalFileAppender(getName(),
47  filename_prop.get());
48 
49  return configureLayout();
50 }
51 
52 void GenerationalFileAppender::updateHook()
53 {
54  processEvents(maxEventsPerCycle);
55 }
56 
57 void GenerationalFileAppender::cleanupHook()
58 {
59  /* normally in log4cpp the category owns the appenders and deletes them
60  itself, however we don't associate appenders and categories in the
61  same manner. Hence, you have to manually manage appenders.
62  */
63  delete appender;
64  appender = 0;
65 }
66 
67 void GenerationalFileAppender::advanceGeneration()
68 {
69  if (0 != appender)
70  {
71  static_cast<log4cpp::GenerationalFileAppender*>(appender)->advanceGeneration();
72  }
73  else
74  {
75  log(Error) << "No appender to roll over!" << endlog();
76  }
77 }
78 
79 // namespaces
80 }
81 }
82 
83 ORO_LIST_COMPONENT_TYPE(OCL::logging::GenerationalFileAppender);
This file contains the macros and definitions to create dynamically loadable components.
The Orocos Component Library.
Definition: Component.hpp:43
Definition: Category.hpp:10
Appender supporting generations of log files.