44 #include "rosgraph_msgs/Log.h" 75 max_file_size_(100*1024*1024),
76 current_file_size_(0),
77 max_backup_index_(10),
78 current_backup_index_(0),
86 const char* disable_file_logging_env = getenv(
"ROSOUT_DISABLE_FILE_LOGGING");
87 std::string disable_file_logging(disable_file_logging_env ? disable_file_logging_env :
"");
89 disable_file_logging.begin(),
90 disable_file_logging.end(),
91 disable_file_logging.begin(),
92 [](
char c){
return std::tolower(c); });
93 if (disable_file_logging.empty() ||
94 disable_file_logging ==
"0" ||
95 disable_file_logging ==
"false" ||
96 disable_file_logging ==
"off" ||
97 disable_file_logging ==
"no")
99 handle_ = fopen(log_file_name_.c_str(),
"w");
103 std::cerr <<
"Error opening rosout log file '" << log_file_name_.c_str() <<
"': " << strerror(errno);
107 std::cout <<
"logging to " << log_file_name_.c_str() << std::endl;
109 std::stringstream ss;
111 int written = fprintf(handle_,
"%s", ss.str().c_str());
114 std::cerr <<
"Error writing to rosout log file '" << log_file_name_.c_str() <<
"': " << strerror(ferror(handle_)) << std::endl;
116 else if (written > 0)
118 current_file_size_ += written;
121 std::cerr <<
"Error flushing rosout log file '" << log_file_name_.c_str() <<
"': " << strerror(ferror(handle_));
127 agg_pub_ = node_.
advertise<rosgraph_msgs::Log>(
"/rosout_agg", 0);
128 std::cout <<
"re-publishing aggregated messages to /rosout_agg" << std::endl;
131 std::cout <<
"subscribed to /rosout" << std::endl;
143 std::stringstream ss;
144 ss << msg->header.stamp <<
" ";
147 case rosgraph_msgs::Log::FATAL:
150 case rosgraph_msgs::Log::ERROR:
153 case rosgraph_msgs::Log::WARN:
156 case rosgraph_msgs::Log::DEBUG:
159 case rosgraph_msgs::Log::INFO:
163 ss << msg->level <<
" ";
166 ss << msg->name <<
" ";
167 ss <<
"[" << msg->file <<
":" << msg->line <<
"(" << msg->function <<
")] ";
175 std::vector<std::string>::const_iterator it = msg->topics.begin();
176 std::vector<std::string>::const_iterator end = msg->topics.end();
177 for ( ; it != end; ++it )
179 const std::string& topic = *it;
181 if ( it != msg->topics.begin() )
193 int written = fprintf(handle_,
"%s", ss.str().c_str());
196 std::cerr <<
"Error writing to rosout log file '" << log_file_name_.c_str() <<
"': " << strerror(ferror(handle_)) << std::endl;
198 else if (written > 0)
200 current_file_size_ += written;
203 std::cerr <<
"Error flushing rosout log file '" << log_file_name_.c_str() <<
"': " << strerror(errno);
207 if (current_file_size_ > max_file_size_)
209 std::cout <<
"rosout log file " << log_file_name_.c_str() <<
" reached max size, rotating log files" << std::endl;
212 std::cerr <<
"Error closing rosout log file '" << log_file_name_.c_str() <<
"': " << strerror(errno) << std::endl;
214 if (current_backup_index_ == max_backup_index_)
216 std::stringstream backup_file_name;
218 int rc =
remove(backup_file_name.str().c_str());
221 std::cerr <<
"Error deleting oldest rosout log file '" << backup_file_name.str().c_str() <<
"': " << strerror(errno) << std::endl;
224 std::size_t i = std::min(max_backup_index_, current_backup_index_ + 1);
227 std::stringstream current_file_name;
231 current_file_name <<
"." << (i - 1);
233 std::stringstream rotated_file_name;
234 rotated_file_name << log_file_name_ <<
"." << i;
235 int rc = rename(current_file_name.str().c_str(), rotated_file_name.str().c_str());
238 std::cerr <<
"Error rotating rosout log file '" << current_file_name.str().c_str() <<
"' to '" << rotated_file_name.str().c_str() <<
"': " << strerror(errno) << std::endl;
242 if (current_backup_index_ < max_backup_index_)
246 handle_ = fopen(log_file_name_.c_str(),
"w");
249 std::cerr <<
"Error opening rosout log file '" << log_file_name_.c_str() <<
"': " << strerror(errno);
251 current_file_size_ = 0;
257 int main(
int argc,
char **argv)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
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_
void publish(const boost::shared_ptr< M > &message) const
size_t current_file_size_
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ROSCPP_DECL const std::string & getLogDirectory()
bool getParamCached(const std::string &key, std::string &s) const
void rosoutCallback(const rosgraph_msgs::Log::ConstPtr &msg)
int main(int argc, char **argv)
ros::Subscriber rosout_sub_