00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2008, Willow Garage, Inc. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Willow Garage, Inc. nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 */ 00034 00035 #include "ros/rosout_appender.h" 00036 #include "ros/this_node.h" 00037 #include "ros/node_handle.h" 00038 #include "ros/topic_manager.h" 00039 #include "ros/advertise_options.h" 00040 #include "ros/names.h" 00041 00042 #include <log4cxx/spi/loggingevent.h> 00043 00044 #include <rosgraph_msgs/Log.h> 00045 00046 namespace ros 00047 { 00048 00049 ROSOutAppender::ROSOutAppender() 00050 : shutting_down_(false) 00051 , publish_thread_(boost::bind(&ROSOutAppender::logThread, this)) 00052 { 00053 AdvertiseOptions ops; 00054 ops.init<rosgraph_msgs::Log>(names::resolve("/rosout"), 0); 00055 ops.latch = true; 00056 SubscriberCallbacksPtr cbs(new SubscriberCallbacks); 00057 TopicManager::instance()->advertise(ops, cbs); 00058 } 00059 00060 ROSOutAppender::~ROSOutAppender() 00061 { 00062 shutting_down_ = true; 00063 00064 { 00065 boost::mutex::scoped_lock lock(queue_mutex_); 00066 queue_condition_.notify_all(); 00067 } 00068 00069 publish_thread_.join(); 00070 } 00071 00072 const std::string& ROSOutAppender::getLastError() 00073 { 00074 return last_error_; 00075 } 00076 00077 void ROSOutAppender::append(const log4cxx::spi::LoggingEventPtr& event, log4cxx::helpers::Pool& pool) 00078 { 00079 rosgraph_msgs::LogPtr msg(new rosgraph_msgs::Log); 00080 00081 msg->header.stamp = ros::Time::now(); 00082 00083 if (event->getLevel() == log4cxx::Level::getFatal()) 00084 { 00085 msg->level = rosgraph_msgs::Log::FATAL; 00086 last_error_ = event->getMessage(); 00087 } 00088 else if (event->getLevel() == log4cxx::Level::getError()) 00089 { 00090 msg->level = rosgraph_msgs::Log::ERROR; 00091 last_error_ = event->getMessage(); 00092 } 00093 else if (event->getLevel() == log4cxx::Level::getWarn()) 00094 { 00095 msg->level = rosgraph_msgs::Log::WARN; 00096 } 00097 else if (event->getLevel() == log4cxx::Level::getInfo()) 00098 { 00099 msg->level = rosgraph_msgs::Log::INFO; 00100 } 00101 else if (event->getLevel() == log4cxx::Level::getDebug()) 00102 { 00103 msg->level = rosgraph_msgs::Log::DEBUG; 00104 } 00105 00106 msg->name = this_node::getName(); 00107 msg->msg = event->getMessage(); 00108 00109 const log4cxx::spi::LocationInfo& info = event->getLocationInformation(); 00110 msg->file = info.getFileName(); 00111 msg->function = info.getMethodName(); 00112 msg->line = info.getLineNumber(); 00113 00114 this_node::getAdvertisedTopics(msg->topics); 00115 00116 boost::mutex::scoped_lock lock(queue_mutex_); 00117 log_queue_.push_back(msg); 00118 queue_condition_.notify_all(); 00119 } 00120 00121 void ROSOutAppender::logThread() 00122 { 00123 while (!shutting_down_) 00124 { 00125 V_Log local_queue; 00126 00127 { 00128 boost::mutex::scoped_lock lock(queue_mutex_); 00129 00130 if (shutting_down_) 00131 { 00132 return; 00133 } 00134 00135 queue_condition_.wait(lock); 00136 00137 if (shutting_down_) 00138 { 00139 return; 00140 } 00141 00142 local_queue.swap(log_queue_); 00143 } 00144 00145 V_Log::iterator it = local_queue.begin(); 00146 V_Log::iterator end = local_queue.end(); 00147 for (; it != end; ++it) 00148 { 00149 TopicManager::instance()->publish(names::resolve("/rosout"), *(*it)); 00150 } 00151 } 00152 } 00153 00154 } // namespace ros