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