aboutsummaryrefslogtreecommitdiffstats
path: root/mpm/lib
diff options
context:
space:
mode:
authorMartin Braun <martin.braun@ettus.com>2017-11-14 14:29:25 -0800
committerMartin Braun <martin.braun@ettus.com>2017-12-22 15:05:06 -0800
commit5c63a83d22d99d8def9cb5c943853f579c533ada (patch)
tree3bc995fe13bf94ae5c6187215be855acff54a3d2 /mpm/lib
parentbc85ad9df0670e534fa73440c26609573ce1bc8f (diff)
downloaduhd-5c63a83d22d99d8def9cb5c943853f579c533ada.tar.gz
uhd-5c63a83d22d99d8def9cb5c943853f579c533ada.tar.bz2
uhd-5c63a83d22d99d8def9cb5c943853f579c533ada.zip
mpm: Add log_buf class
This is a buffer for log messages, designed for getting log messages from C++ back into Python. Reviewed-By: Mark Meserve <mark.meserve@ni.com>
Diffstat (limited to 'mpm/lib')
-rw-r--r--mpm/lib/types/CMakeLists.txt1
-rw-r--r--mpm/lib/types/log_buf.cpp64
2 files changed, 65 insertions, 0 deletions
diff --git a/mpm/lib/types/CMakeLists.txt b/mpm/lib/types/CMakeLists.txt
index d4f52f7ed..10ce8a28c 100644
--- a/mpm/lib/types/CMakeLists.txt
+++ b/mpm/lib/types/CMakeLists.txt
@@ -1,5 +1,6 @@
SET(TYPES_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/lockable.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/log_buf.cpp
)
USRP_PERIPHS_ADD_OBJECT(types ${TYPES_SOURCES})
diff --git a/mpm/lib/types/log_buf.cpp b/mpm/lib/types/log_buf.cpp
new file mode 100644
index 000000000..d159ca6a2
--- /dev/null
+++ b/mpm/lib/types/log_buf.cpp
@@ -0,0 +1,64 @@
+//
+// Copyright 2017 Ettus Research, National Instruments Company
+//
+// SPDX-License-Identifier: GPL-3.0
+//
+
+#include <mpm/types/log_buf.hpp>
+
+using namespace mpm::types;
+
+void log_buf::post(
+ const log_level_t log_level,
+ const std::string &component,
+ const std::string &message
+) {
+ {
+ std::lock_guard<std::mutex> l(_buf_lock);
+ _buf.push_back(
+ log_message(log_level, component, message)
+ );
+ }
+
+ if (bool(_notify_callback)) {
+ _notify_callback();
+ }
+}
+
+void log_buf::set_notify_callback(
+ std::function<void(void)> callback
+) {
+ _notify_callback = callback;
+}
+
+std::tuple<log_level_t, std::string, std::string> log_buf::pop()
+{
+ std::lock_guard<std::mutex> l(_buf_lock);
+ if (_buf.empty()) {
+ return std::make_tuple(
+ log_level_t::NONE,
+ "",
+ ""
+ );
+ }
+
+ auto last_msg = _buf.front();
+ _buf.pop_front();
+ return std::make_tuple(
+ last_msg.log_level,
+ last_msg.component,
+ last_msg.message
+ );
+}
+
+log_buf::sptr log_buf::make()
+{
+ return std::make_shared<log_buf>();
+}
+
+log_buf::sptr log_buf::make_singleton()
+{
+ static auto log_sptr = log_buf::make();
+ return log_sptr;
+}
+