Skip to content

Commit

Permalink
Proper logrotation for rosout
Browse files Browse the repository at this point in the history
  • Loading branch information
mgrrx authored and dirk-thomas committed Feb 22, 2017
1 parent e1b91ce commit ce4d726
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions tools/rosout/rosout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,40 +178,42 @@ class Rosout
// check for rolling
if (current_file_size_ > max_file_size_)
{
std::cout << "rosout log file " << log_file_name_.c_str() << " reached max size, rotating log files" << std::endl;
if (fclose(handle_))
{
std::cerr << "Error closing rosout log file '" << log_file_name_.c_str() << "': " << strerror(ferror(handle_)) << std::endl;
}
current_backup_index_++;
if (current_backup_index_ <= max_backup_index_)
if (current_backup_index_ == max_backup_index_)
{
std::stringstream backup_file_name;
backup_file_name << log_file_name_ << "." << current_backup_index_;
int rc = rename(log_file_name_.c_str(), backup_file_name.str().c_str());
backup_file_name << log_file_name_ << "." << max_backup_index_;
int rc = remove(backup_file_name.str().c_str());
if (rc != 0)
{
rc = remove(backup_file_name.str().c_str());
if (rc == 0)
{
rc = rename(log_file_name_.c_str(), backup_file_name.str().c_str());
if (rc)
{
std::cerr << "Error rotating rosout log file '" << log_file_name_.c_str() << "' to '" << backup_file_name.str().c_str() << "': " << strerror(errno);
}
}
std::cerr << "Error deleting oldest rosout log file '" << backup_file_name.str().c_str() << "': " << strerror(errno) << std::endl;
}
if (rc)
{
std::cerr << "Error rotating rosout log file '" << log_file_name_.c_str() << "'' to '" << backup_file_name.str().c_str() << "'" << std::endl;
}
else
}
std::size_t i = std::min(max_backup_index_, current_backup_index_ + 1);
while (i > 0)
{
std::stringstream current_file_name;
current_file_name << log_file_name_;
if (i > 1)
{
std::cout << "rosout log file " << log_file_name_.c_str() << " reached max size, back up data to " << backup_file_name.str().c_str() << std::endl;
current_file_name << "." << (i - 1);
}
if (current_backup_index_ == max_backup_index_)
std::stringstream rotated_file_name;
rotated_file_name << log_file_name_ << "." << i;
int rc = rename(current_file_name.str().c_str(), rotated_file_name.str().c_str());
if (rc != 0)
{
current_backup_index_ = 0;
std::cerr << "Error rotating rosout log file '" << current_file_name.str().c_str() << "' to '" << rotated_file_name.str().c_str() << "': " << strerror(errno) << std::endl;
}
--i;
}
if (current_backup_index_ < max_backup_index_)
{
++current_backup_index_;
}
handle_ = fopen(log_file_name_.c_str(), "w");
if (handle_ == 0)
Expand Down

0 comments on commit ce4d726

Please sign in to comment.