clean up file open/close logic

This commit is contained in:
Mark Whitehorn
2016-04-27 08:28:39 -06:00
committed by Lorenz Meier
parent 672042e051
commit 728de5f87b
3 changed files with 30 additions and 28 deletions
+28 -27
View File
@@ -22,11 +22,23 @@ LogWriter::LogWriter(uint8_t *buffer, size_t buffer_size) :
void LogWriter::start_log(const char *filename)
{
::strncpy(_filename, filename, sizeof(_filename));
_fd = ::open(_filename, O_CREAT | O_WRONLY, PX4_O_MODE_666);
if (_fd < 0) {
PX4_WARN("can't open log file %s", _filename);
_should_run = false;
return;
} else {
PX4_WARN("opened log file: %s", _filename);
_should_run = true;
_running = true;
}
// Clear buffer and counters
_head = 0;
_count = 0;
_total_written = 0;
_should_run = true;
notify();
}
@@ -90,21 +102,7 @@ void LogWriter::run()
}
while (!_exit_thread) {
// Outer endless loop, start new file each time
// _filename must be set before setting _should_run = true
_fd = ::open(_filename, O_CREAT | O_WRONLY, PX4_O_MODE_666);
if (_fd < 0) {
PX4_WARN("can't open log file %s", _filename);
_should_run = false;
continue;
}
PX4_WARN("started, log file: %s", _filename);
_should_run = true;
// Outer endless loop
int poll_count = 0;
int written = 0;
@@ -164,22 +162,25 @@ void LogWriter::run()
_total_written += written;
}
if (!_should_run && written == static_cast<int>(available) && !is_part) {
if (_running && !_should_run && written == static_cast<int>(available) && !is_part) {
// Stop only when all data written
_running = false;
_head = 0;
_count = 0;
int res = ::close(_fd);
if (res) {
PX4_WARN("error closing log file");
} else {
PX4_WARN("closed logfile: %s, bytes written: %zu", _filename, _total_written);
}
break;
}
}
_head = 0;
_count = 0;
int res = ::close(_fd);
if (res) {
PX4_WARN("error closing log file");
}
PX4_WARN("stopped, bytes written: %zu", _total_written);
}
}