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",1), 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 if (appender) 00041 delete appender; // in case the filename changed... 00042 00043 appender = new log4cpp::FileAppender(getName(), filename_prop.rvalue()); 00044 00045 return configureLayout(); 00046 } 00047 00048 void FileAppender::updateHook() 00049 { 00050 if (!log_port.connected()) return; // no category connected to us 00051 00052 /* Consume waiting events until 00053 a) the buffer is empty 00054 b) we consume too many events on one cycle 00055 */ 00056 OCL::logging::LoggingEvent event; 00057 assert(appender); 00058 assert(0 <= maxEventsPerCycle); 00059 if (0 == maxEventsPerCycle) 00060 { 00061 // consume infinite events 00062 for (;;) 00063 { 00064 if (log_port.read( event ) == NewData) 00065 { 00066 log4cpp::LoggingEvent e2 = event.toLog4cpp(); 00067 appender->doAppend(e2); 00068 } 00069 else 00070 { 00071 break; // nothing to do 00072 } 00073 } 00074 } 00075 else 00076 { 00077 00078 // consume up to maxEventsPerCycle events 00079 int n = maxEventsPerCycle; 00080 do 00081 { 00082 if (log_port.read( event ) == NewData) 00083 { 00084 log4cpp::LoggingEvent e2 = event.toLog4cpp(); 00085 appender->doAppend(e2); 00086 } 00087 else 00088 { 00089 break; // nothing to do 00090 } 00091 --n; 00092 } 00093 while (0 < n); 00094 } 00095 } 00096 00097 void FileAppender::cleanupHook() 00098 { 00099 /* normally in log4cpp the category owns the appenders and deletes them 00100 itself, however we don't associate appenders and categories in the 00101 same manner. Hence, you have to manually manage appenders. 00102 */ 00103 delete appender; 00104 appender = 0; 00105 } 00106 00107 // namespaces 00108 } 00109 } 00110 00111 ORO_LIST_COMPONENT_TYPE(OCL::logging::FileAppender);