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


ocl
Author(s): OCL Development Team
autogenerated on Mon Sep 14 2015 14:21:46