$search
00001 /* 00002 * Copyright (c) 2008, Willow Garage, Inc. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of Willow Garage, Inc. nor the names of its 00014 * contributors may be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00021 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00022 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00023 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00024 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00025 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00026 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00027 * POSSIBILITY OF SUCH DAMAGE. 00028 */ 00029 00030 #include <cstring> 00031 #include <cstdlib> 00032 00033 #include "ros/ros.h" 00034 #include "ros/file_log.h" 00035 00036 #ifdef WIN32 00037 #ifdef ERROR 00038 // ach, windows.h polluting everything again, 00039 // clashes with autogenerated rosgraph_msgs/Log.h 00040 #undef ERROR 00041 #endif 00042 #endif 00043 #include "rosgraph_msgs/Log.h" 00044 00045 #include "log4cxx/logger.h" 00046 #include "log4cxx/rollingfileappender.h" 00047 #include "log4cxx/patternlayout.h" 00048 #include "log4cxx/helpers/pool.h" 00049 00062 class Rosout 00063 { 00064 public: 00065 log4cxx::LoggerPtr logger_; 00066 ros::NodeHandle node_; 00067 ros::Subscriber rosout_sub_; 00068 ros::Publisher agg_pub_; 00069 00070 Rosout() 00071 { 00072 init(); 00073 } 00074 00075 void init() 00076 { 00077 //calculate log directory 00078 std::string log_file_name = ros::file_log::getLogDirectory() + "/rosout.log"; 00079 00080 logger_ = log4cxx::Logger::getRootLogger(); 00081 log4cxx::LayoutPtr layout = new log4cxx::PatternLayout(""); 00082 log4cxx::RollingFileAppenderPtr appender = new log4cxx::RollingFileAppender(layout, log_file_name, true); 00083 logger_->addAppender( appender ); 00084 appender->setMaximumFileSize(100*1024*1024); 00085 appender->setMaxBackupIndex(10); 00086 log4cxx::helpers::Pool pool; 00087 appender->activateOptions(pool); 00088 00089 std::cout << "logging to " << log_file_name << std::endl; 00090 00091 LOG4CXX_INFO(logger_, "\n\n" << ros::Time::now() << " Node Startup\n"); 00092 00093 agg_pub_ = node_.advertise<rosgraph_msgs::Log>("/rosout_agg", 0); 00094 std::cout << "re-publishing aggregated messages to /rosout_agg" << std::endl; 00095 00096 rosout_sub_ = node_.subscribe("/rosout", 0, &Rosout::rosoutCallback, this); 00097 std::cout << "subscribed to /rosout" << std::endl; 00098 } 00099 00100 void rosoutCallback(const rosgraph_msgs::Log::ConstPtr& msg) 00101 { 00102 agg_pub_.publish(msg); 00103 00104 std::stringstream ss; 00105 ss << msg->header.stamp << " "; 00106 switch (msg->level) 00107 { 00108 case rosgraph_msgs::Log::FATAL: 00109 ss << "FATAL "; 00110 break; 00111 case rosgraph_msgs::Log::ERROR: 00112 ss << "ERROR "; 00113 break; 00114 case rosgraph_msgs::Log::WARN: 00115 ss << "WARN "; 00116 break; 00117 case rosgraph_msgs::Log::DEBUG: 00118 ss << "DEBUG "; 00119 break; 00120 case rosgraph_msgs::Log::INFO: 00121 ss << "INFO "; 00122 break; 00123 default: 00124 ss << msg->level << " "; 00125 } 00126 00127 ss << "[" << msg->file << ":" << msg->line << "(" << msg->function << ") "; 00128 00129 ss << "[topics: "; 00130 std::vector<std::string>::const_iterator it = msg->topics.begin(); 00131 std::vector<std::string>::const_iterator end = msg->topics.end(); 00132 for ( ; it != end; ++it ) 00133 { 00134 const std::string& topic = *it; 00135 00136 if ( it != msg->topics.begin() ) 00137 { 00138 ss << ", "; 00139 } 00140 00141 ss << topic; 00142 } 00143 ss << "] "; 00144 00145 ss << msg->msg; 00146 LOG4CXX_INFO(logger_, ss.str()); 00147 } 00148 }; 00149 00150 int main(int argc, char **argv) 00151 { 00152 ros::init(argc, argv, "rosout", ros::init_options::NoRosout); 00153 ros::NodeHandle n; 00154 Rosout r; 00155 00156 ros::spin(); 00157 00158 return 0; 00159 } 00160