logger: remove _log_buffer from Logger, initialize it in the writer instead

it's not used in the logger, so don't store it there. It is accessed via
LogWriter::write.
This also makes sure the buffer size is >= _min_write_chunk and handles
allocation failure properly.
This commit is contained in:
Beat Küng
2016-05-02 15:07:15 +02:00
committed by Lorenz Meier
parent 04f38b9197
commit 8b5a325644
4 changed files with 39 additions and 15 deletions
+22 -4
View File
@@ -2,15 +2,15 @@
#include <fcntl.h>
#include <string.h>
#include <mathlib/mathlib.h>
namespace px4
{
namespace logger
{
LogWriter::LogWriter(uint8_t *buffer, size_t buffer_size) :
_buffer(buffer),
_buffer_size(buffer_size),
_min_write_chunk(4096)
LogWriter::LogWriter(size_t buffer_size) :
_buffer_size(math::max(buffer_size, _min_write_chunk))
{
pthread_mutex_init(&_mtx, nullptr);
pthread_cond_init(&_cv, nullptr);
@@ -19,6 +19,24 @@ LogWriter::LogWriter(uint8_t *buffer, size_t buffer_size) :
perf_fsync = perf_alloc(PC_ELAPSED, "sd fsync");
}
bool LogWriter::init()
{
if (_buffer) {
return true;
}
_buffer = new uint8_t[_buffer_size];
return _buffer;
}
LogWriter::~LogWriter()
{
if (_buffer) {
delete[] _buffer;
}
}
void LogWriter::start_log(const char *filename)
{
::strncpy(_filename, filename, sizeof(_filename));