A private doorbell project to serve an Android app with webcam pictures and microphone recordings when someone presses the button. Information is being spread via MQTT. https://www.taibsu.de
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
heimdall/threadLauncher.cpp

64 lines
1.5 KiB

#include "threadLauncher.h"
#include "log.h"
namespace tai
{
void threadLauncher::go(const std::string& args)
{
_asyncThreads.emplace_back(std::make_unique<std::thread>(&threadLauncher::execShellCmd, this, args));
}
void threadLauncher::go(std::unique_ptr<std::thread> thread)
{
_asyncThreads.emplace_back(std::move(thread));
}
/**
* @brief Calls a command in the host shell.
* @return The stdout output of the command.
*/
auto threadLauncher::execShellCmd(std::string const& cmd) -> std::string
{
LOG_DBG("[TL] Executing shell command: " + cmd);
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe { popen(cmd.c_str(), "r"), pclose };
if (!pipe) {
LOG_ERR("[TL] Unable to execute the above command.");
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), static_cast<int>(buffer.size()), pipe.get()) != nullptr) {
result += buffer.data();
}
LOG_DBG("[TL] Thread [" + cmd + "] stopped.");
return result;
}
void threadLauncher::closeAllThreads()
{
for (auto& at : _asyncThreads) {
if (at) {
std::string const& threadIdStr = Log::threadIdToStr(at->get_id());
if (at->joinable()) {
LOG_DBG("[TL] Joining thread " + threadIdStr);
at->join();
}
LOG_DBG("[TL] Releasing pointer for thread " + threadIdStr);
at.release();
}
}
}
threadLauncher::~threadLauncher()
{
closeAllThreads();
}
}