43 #include "rosgraph_msgs/Log.h" 74 max_file_size_(100*1024*1024),
75 current_file_size_(0),
76 max_backup_index_(10),
77 current_backup_index_(0),
85 handle_ = fopen(log_file_name_.c_str(),
"w");
89 std::cerr <<
"Error opening rosout log file '" << log_file_name_.c_str() <<
"': " << strerror(errno);
93 std::cout <<
"logging to " << log_file_name_.c_str() << std::endl;
97 int written = fprintf(handle_,
"%s", ss.str().c_str());
100 std::cerr <<
"Error writting to rosout log file '" << log_file_name_.c_str() <<
"': " << strerror(ferror(handle_)) << std::endl;
102 else if (written > 0)
104 current_file_size_ += written;
107 std::cerr <<
"Error flushing rosout log file '" << log_file_name_.c_str() <<
"': " << strerror(ferror(handle_));
112 agg_pub_ = node_.
advertise<rosgraph_msgs::Log>(
"/rosout_agg", 0);
113 std::cout <<
"re-publishing aggregated messages to /rosout_agg" << std::endl;
116 std::cout <<
"subscribed to /rosout" << std::endl;
123 std::stringstream ss;
124 ss << msg->header.stamp <<
" ";
127 case rosgraph_msgs::Log::FATAL:
130 case rosgraph_msgs::Log::ERROR:
133 case rosgraph_msgs::Log::WARN:
136 case rosgraph_msgs::Log::DEBUG:
139 case rosgraph_msgs::Log::INFO:
143 ss << msg->level <<
" ";
146 ss << msg->name <<
" ";
147 ss <<
"[" << msg->file <<
":" << msg->line <<
"(" << msg->function <<
")] ";
155 std::vector<std::string>::const_iterator it = msg->topics.begin();
156 std::vector<std::string>::const_iterator end = msg->topics.end();
157 for ( ; it != end; ++it )
159 const std::string& topic = *it;
161 if ( it != msg->topics.begin() )
173 int written = fprintf(handle_,
"%s", ss.str().c_str());
176 std::cerr <<
"Error writting to rosout log file '" << log_file_name_.c_str() <<
"': " << strerror(ferror(handle_)) << std::endl;
178 else if (written > 0)
180 current_file_size_ += written;
183 std::cerr <<
"Error flushing rosout log file '" << log_file_name_.c_str() <<
"': " << strerror(ferror(handle_));
187 if (current_file_size_ > max_file_size_)
189 std::cout <<
"rosout log file " << log_file_name_.c_str() <<
" reached max size, rotating log files" << std::endl;
192 std::cerr <<
"Error closing rosout log file '" << log_file_name_.c_str() <<
"': " << strerror(ferror(handle_)) << std::endl;
194 if (current_backup_index_ == max_backup_index_)
196 std::stringstream backup_file_name;
198 int rc =
remove(backup_file_name.str().c_str());
201 std::cerr <<
"Error deleting oldest rosout log file '" << backup_file_name.str().c_str() <<
"': " << strerror(errno) << std::endl;
204 std::size_t i = std::min(max_backup_index_, current_backup_index_ + 1);
207 std::stringstream current_file_name;
211 current_file_name <<
"." << (i - 1);
213 std::stringstream rotated_file_name;
214 rotated_file_name << log_file_name_ <<
"." << i;
215 int rc = rename(current_file_name.str().c_str(), rotated_file_name.str().c_str());
218 std::cerr <<
"Error rotating rosout log file '" << current_file_name.str().c_str() <<
"' to '" << rotated_file_name.str().c_str() <<
"': " << strerror(errno) << std::endl;
222 if (current_backup_index_ < max_backup_index_)
226 handle_ = fopen(log_file_name_.c_str(),
"w");
229 std::cerr <<
"Error opening rosout log file '" << log_file_name_.c_str() <<
"': " << strerror(errno);
231 current_file_size_ = 0;
237 int main(
int argc,
char **argv)
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
bool getParamCached(const std::string &key, std::string &s) const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
std::string log_file_name_
size_t current_backup_index_
size_t current_file_size_
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ROSCPP_DECL const std::string & getLogDirectory()
void rosoutCallback(const rosgraph_msgs::Log::ConstPtr &msg)
int main(int argc, char **argv)
ros::Subscriber rosout_sub_