From 63bd2cebf95223311ab8aa0b0b15e476853b0834 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Wed, 4 May 2016 11:22:05 +0200 Subject: [PATCH] refactor logger: add a write_wait method to avoid code duplication --- src/modules/logger/log_writer.h | 4 +++ src/modules/logger/logger.cpp | 46 ++++++++++++--------------------- src/modules/logger/logger.h | 6 +++++ 3 files changed, 26 insertions(+), 30 deletions(-) diff --git a/src/modules/logger/log_writer.h b/src/modules/logger/log_writer.h index 30d4818a69..45106b9ddf 100644 --- a/src/modules/logger/log_writer.h +++ b/src/modules/logger/log_writer.h @@ -33,6 +33,10 @@ public: void stop_log(); + /** + * Write data to be logged. The caller must call lock() before calling this. + * @return true on success, false if not enough space in the buffer left + */ bool write(void *ptr, size_t size); void lock() diff --git a/src/modules/logger/logger.cpp b/src/modules/logger/logger.cpp index e6f0748acf..5bb3f89732 100644 --- a/src/modules/logger/logger.cpp +++ b/src/modules/logger/logger.cpp @@ -701,6 +701,18 @@ void Logger::stop_log() _writer.stop_log(); } +bool Logger::write_wait(void *ptr, size_t size) +{ + while (!_writer.write(ptr, size)) { + _writer.unlock(); + _writer.notify(); + usleep(_log_interval); + _writer.lock(); + } + + return true; +} + void Logger::write_formats() { _writer.lock(); @@ -714,12 +726,7 @@ void Logger::write_formats() size_t msg_size = sizeof(msg) - sizeof(msg.format) + msg.format_len; msg.msg_size = msg_size - 3; - while (!_writer.write(&msg, msg_size)) { - _writer.unlock(); - _writer.notify(); - usleep(_log_interval); // Wait if buffer is full, don't skip FORMAT messages - _writer.lock(); - } + write_wait(&msg, msg_size); msg_id++; } @@ -748,14 +755,7 @@ void Logger::write_info(const char *name, const char *value) msg->msg_size = msg_size - 3; - /* write message */ - while (!_writer.write(buffer, msg_size)) { - /* wait if buffer is full, don't skip INFO messages */ - _writer.unlock(); - _writer.notify(); - usleep(_log_interval); - _writer.lock(); - } + write_wait(buffer, msg_size); } } @@ -815,14 +815,7 @@ void Logger::write_parameters() msg->msg_size = msg_size - 3; - /* write message */ - while (!_writer.write(buffer, msg_size)) { - /* wait if buffer is full, don't skip PARAMETER messages */ - _writer.unlock(); - _writer.notify(); - usleep(_log_interval); - _writer.lock(); - } + write_wait(buffer, msg_size); } } while ((param != PARAM_INVALID) && (param_idx < (int) param_count())); @@ -882,14 +875,7 @@ void Logger::write_changed_parameters() /* msg_size is now 1 (msg_type) + 2 (msg_size) + 1 (key_len) + key_len + value_size */ msg->msg_size = msg_size - 3; - /* write message */ - while (!_writer.write(buffer, msg_size)) { - /* wait if buffer is full, don't skip PARAMETER messages */ - _writer.unlock(); - _writer.notify(); - usleep(_log_interval); - _writer.lock(); - } + write_wait(buffer, msg_size); } } while ((param != PARAM_INVALID) && (param_idx < (int) param_count())); diff --git a/src/modules/logger/logger.h b/src/modules/logger/logger.h index c25e7171e6..4a0dc59d04 100644 --- a/src/modules/logger/logger.h +++ b/src/modules/logger/logger.h @@ -80,6 +80,12 @@ private: bool copy_if_updated_multi(orb_id_t topic, int multi_instance, int *handle, void *buffer, uint64_t *time_last_checked); + /** + * Write data to the logger. Waits if buffer is full until all data is written. + * Must be called with _writer.lock() held. + */ + bool write_wait(void *ptr, size_t size); + static constexpr size_t MAX_TOPICS_NUM = 128; /**< Maximum number of logged topics */ static constexpr unsigned MAX_NO_LOGFOLDER = 999; /**< Maximum number of log dirs */ static constexpr unsigned MAX_NO_LOGFILE = 999; /**< Maximum number of log files */