Category.cpp
Go to the documentation of this file.
00001 #include "logging/Category.hpp"
00002 #include <rtt/Logger.hpp>
00003 #include <rtt/ConnPolicy.hpp>
00004 #include <log4cpp/NDC.hh>
00005 #include <log4cpp/HierarchyMaintainer.hh>
00006 
00007 using namespace RTT;
00008 
00009 namespace OCL {
00010 namespace logging {
00011 
00012 Category::Category(const std::string& name,
00013                    log4cpp::Category* parent,
00014                    log4cpp::Priority::Value priority) :
00015         log4cpp::Category(name, parent, priority),
00016         log_port( convertName(name) , false )
00017 {
00018 }
00019 
00020 Category::~Category()
00021 {
00022 }
00023 
00024 void Category::log(log4cpp::Priority::Value priority,
00025                  const RTT::rt_string& message) throw()
00026 {
00027     if (isPriorityEnabled(priority))
00028     {
00029         _logUnconditionally2(priority, message);
00030     }
00031 }
00032 
00033 void Category::debug(const RTT::rt_string& message) throw()
00034 {
00035     if (isPriorityEnabled(log4cpp::Priority::DEBUG))
00036         _logUnconditionally2(log4cpp::Priority::DEBUG, message);
00037 }
00038 
00039 void Category::info(const RTT::rt_string& message) throw()
00040 {
00041     if (isPriorityEnabled(log4cpp::Priority::INFO))
00042         _logUnconditionally2(log4cpp::Priority::INFO, message);
00043 }
00044 
00045 void Category::notice(const RTT::rt_string& message) throw()
00046 {
00047     if (isPriorityEnabled(log4cpp::Priority::NOTICE))
00048         _logUnconditionally2(log4cpp::Priority::NOTICE, message);
00049 }
00050 
00051 void Category::warn(const RTT::rt_string& message) throw()
00052 {
00053     if (isPriorityEnabled(log4cpp::Priority::WARN))
00054         _logUnconditionally2(log4cpp::Priority::WARN, message);
00055 }
00056 
00057 void Category::error(const RTT::rt_string& message) throw()
00058 {
00059     if (isPriorityEnabled(log4cpp::Priority::ERROR))
00060         _logUnconditionally2(log4cpp::Priority::ERROR, message);
00061 }
00062 
00063 void Category::crit(const RTT::rt_string& message) throw()
00064 {
00065     if (isPriorityEnabled(log4cpp::Priority::CRIT))
00066         _logUnconditionally2(log4cpp::Priority::CRIT, message);
00067 }
00068 
00069 void Category::alert(const RTT::rt_string& message) throw()
00070 {
00071     if (isPriorityEnabled(log4cpp::Priority::ALERT))
00072         _logUnconditionally2(log4cpp::Priority::ALERT, message);
00073 }
00074 
00075 void Category::emerg(const RTT::rt_string& message) throw()
00076 {
00077     if (isPriorityEnabled(log4cpp::Priority::EMERG))
00078         _logUnconditionally2(log4cpp::Priority::EMERG, message);
00079 }
00080 
00081 void Category::fatal(const RTT::rt_string& message) throw()
00082 {
00083     if (isPriorityEnabled(log4cpp::Priority::FATAL))
00084         _logUnconditionally2(log4cpp::Priority::FATAL, message);
00085 }
00086 
00087 
00088 void Category::_logUnconditionally2(log4cpp::Priority::Value priority,
00089                                     const RTT::rt_string& message) throw()
00090 {
00091     try
00092     {
00093         OCL::logging::LoggingEvent event(RTT::rt_string(getName().c_str()),
00094                                          RTT::rt_string(message.c_str()),
00095                                          // NDC's are not real-time
00096 //                                     RTT::rt_string(log4cpp::NDC::get().c_str()),
00097                                          RTT::rt_string(""),
00098                                          priority);
00099         callAppenders(event);
00100     }
00101     catch (std::bad_alloc& e)
00102     {
00103         // \todo do what?
00104 //        std::cerr << "Failed to log event: out of real-time memory" << std::endl;
00106     }
00107 }
00108 
00109 void Category::callAppenders(const OCL::logging::LoggingEvent& event) throw()
00110 {
00111     log_port.write( event );
00112 
00113     // let our parent categories append (if they want to)
00114     if (getAdditivity() && (getParent() != NULL))
00115     {
00116         OCL::logging::Category* parent = dynamic_cast<OCL::logging::Category*>(getParent());
00117         if (parent)
00118         {
00119             parent->callAppenders(event);
00120         }
00121         // else we don't use non-realtime parent Category's!
00122     }
00123 }
00124 
00125 std::string Category::convertName(const std::string& name)
00126 {
00127     std::string     rc(name);
00128 
00129     std::replace_if(rc.begin(),
00130                     rc.end(),
00131                     std::bind2nd(std::equal_to<char>(), '.'),
00132                     '_');
00133 
00134     return rc;
00135 }
00136 
00137 log4cpp::Category* Category::createOCLCategory(const std::string& name,
00138                                                log4cpp::Category* parent,
00139                                                log4cpp::Priority::Value priority)
00140 {
00141     // do _NOT_ log from within this function! You will cause a lockup due to
00142     // recursive calls to log4cpp, if you use RTT w/ log4cpp support.
00143 
00144     // \todo try catch on memory exceptions or failures?
00145     OCL::logging::Category* c = new OCL::logging::Category(name, parent, priority);
00146     return c;
00147 }
00148 
00149 CategoryStream Category::getRTStream(log4cpp::Priority::Value priority)
00150 {
00151     return CategoryStream(this, isPriorityEnabled(priority) ? 
00152                           priority : log4cpp::Priority::NOTSET);
00153 }
00154 
00155 bool Category::connectToLogPort(RTT::base::PortInterface& otherPort)
00156 {
00157     return otherPort.connectTo(&log_port);
00158 }
00159 
00160 bool Category::connectToLogPort(RTT::base::PortInterface&   otherPort,
00161                                 RTT::ConnPolicy&            cp)
00162 {
00163     return otherPort.connectTo(&log_port, cp);
00164 }
00165 
00166 // namespaces
00167 }
00168 }


ocl
Author(s): OCL Development Team
autogenerated on Sun Nov 27 2016 03:45:30