$search
00001 #include "logging/FileAppender.hpp" 00002 #include "ocl/Component.hpp" 00003 #include <rtt/Logger.hpp> 00004 00005 #include <log4cpp/FileAppender.hh> 00006 00007 using namespace RTT; 00008 00009 namespace OCL { 00010 namespace logging { 00011 00012 FileAppender::FileAppender(std::string name) : 00013 OCL::logging::Appender(name), 00014 filename_prop("Filename", "Name of file to log to"), 00015 maxEventsPerCycle_prop("MaxEventsPerCycle", "Maximum number of log events to pop per cycle"), 00016 maxEventsPerCycle(1) 00017 { 00018 properties()->addProperty(filename_prop); 00019 properties()->addProperty(maxEventsPerCycle_prop); 00020 } 00021 00022 FileAppender::~FileAppender() 00023 { 00024 } 00025 00026 bool FileAppender::configureHook() 00027 { 00028 // verify valid limits 00029 int m = maxEventsPerCycle_prop.rvalue(); 00030 if ((0 > m)) 00031 { 00032 log(Error) << "Invalid maxEventsPerCycle value of " 00033 << m << ". Value must be >= 0." 00034 << endlog(); 00035 return false; 00036 } 00037 maxEventsPerCycle = m; 00038 00039 // \todo error checking 00040 appender = new log4cpp::FileAppender(getName(), filename_prop.rvalue()); 00041 00042 return configureLayout(); 00043 } 00044 00045 void FileAppender::updateHook() 00046 { 00047 if (!log_port.connected()) return; // no category connected to us 00048 00049 /* Consume waiting events until 00050 a) the buffer is empty 00051 b) we consume too many events on one cycle 00052 */ 00053 OCL::logging::LoggingEvent event; 00054 assert(appender); 00055 assert(0 <= maxEventsPerCycle); 00056 if (0 == maxEventsPerCycle) 00057 { 00058 // consume infinite events 00059 for (;;) 00060 { 00061 if (log_port.read( event ) == NewData) 00062 { 00063 log4cpp::LoggingEvent e2 = event.toLog4cpp(); 00064 appender->doAppend(e2); 00065 } 00066 else 00067 { 00068 break; // nothing to do 00069 } 00070 } 00071 } 00072 else 00073 { 00074 00075 // consume up to maxEventsPerCycle events 00076 int n = maxEventsPerCycle; 00077 do 00078 { 00079 if (log_port.read( event ) == NewData) 00080 { 00081 log4cpp::LoggingEvent e2 = event.toLog4cpp(); 00082 appender->doAppend(e2); 00083 } 00084 else 00085 { 00086 break; // nothing to do 00087 } 00088 --n; 00089 } 00090 while (0 < n); 00091 } 00092 } 00093 00094 void FileAppender::cleanupHook() 00095 { 00096 /* normally in log4cpp the category owns the appenders and deletes them 00097 itself, however we don't associate appenders and categories in the 00098 same manner. Hence, you have to manually manage appenders. 00099 */ 00100 delete appender; 00101 appender = 0; 00102 } 00103 00104 // namespaces 00105 } 00106 } 00107 00108 ORO_LIST_COMPONENT_TYPE(OCL::logging::FileAppender);