ENH: CLI: use json insteadof string for progress pipe

Change-Id: I1fdecfa6198d06d0c9745f3cbeb02dece9d8e20f
This commit is contained in:
lane.wei 2022-11-07 19:40:48 +08:00 committed by Lane.Wei
parent 6be79946f4
commit b44f3ae3e6

View file

@ -27,6 +27,10 @@
#include <condition_variable> #include <condition_variable>
#include <mutex> #include <mutex>
#include <boost/thread.hpp> #include <boost/thread.hpp>
//add json logic
#include "nlohmann/json.hpp"
using namespace nlohmann;
#endif #endif
#include <boost/algorithm/string/predicate.hpp> #include <boost/algorithm/string/predicate.hpp>
@ -132,7 +136,7 @@ std::map<int, std::string> cli_errors = {
}; };
#if defined(__linux__) || defined(__LINUX__) #if defined(__linux__) || defined(__LINUX__)
#define PIPE_BUFFER_SIZE 64 #define PIPE_BUFFER_SIZE 128
typedef struct _cli_callback_mgr { typedef struct _cli_callback_mgr {
int m_plate_count {0}; int m_plate_count {0};
@ -174,8 +178,15 @@ typedef struct _cli_callback_mgr {
if (m_pipe_fd < 0) if (m_pipe_fd < 0)
return; return;
std::string notify_message; json j;
notify_message = "Plate "+ std::to_string(m_plate_index) + "/" +std::to_string(m_plate_count)+ ": Percent " + std::to_string(m_progress) + ": "+m_message; //record the headers
j["plate_index"] = m_plate_index;
j["plate_count"] = m_plate_count;
j["percent"] = m_progress;
j["message"] = m_message;
std::string notify_message = j.dump();
//notify_message = "Plate "+ std::to_string(m_plate_index) + "/" +std::to_string(m_plate_count)+ ": Percent " + std::to_string(m_progress) + ": "+m_message;
char pipe_message[PIPE_BUFFER_SIZE] = {0}; char pipe_message[PIPE_BUFFER_SIZE] = {0};
strncpy(pipe_message, notify_message.c_str(), PIPE_BUFFER_SIZE); strncpy(pipe_message, notify_message.c_str(), PIPE_BUFFER_SIZE);
@ -239,7 +250,7 @@ typedef struct _cli_callback_mgr {
BOOST_LOG_TRIVIAL(info) << "cli_callback_mgr_t::start enter."; BOOST_LOG_TRIVIAL(info) << "cli_callback_mgr_t::start enter.";
m_pipe_fd = open(pipe_name.c_str(),O_WRONLY|O_NONBLOCK); m_pipe_fd = open(pipe_name.c_str(),O_WRONLY|O_NONBLOCK);
if (m_pipe_fd < 0) { if (m_pipe_fd < 0) {
BOOST_LOG_TRIVIAL(error) << "could not create pipe for "<<pipe_name; BOOST_LOG_TRIVIAL(warning) << "could not create pipe for "<<pipe_name;
return false; return false;
} }
std::unique_lock<std::mutex> lck(m_mutex); std::unique_lock<std::mutex> lck(m_mutex);
@ -261,6 +272,7 @@ typedef struct _cli_callback_mgr {
} }
if (!m_started) { if (!m_started) {
lck.unlock(); lck.unlock();
BOOST_LOG_TRIVIAL(info) << "cli_callback_mgr_t::stop not started before, return directly.";
return; return;
} }
m_exit = true; m_exit = true;