16bool ProcessExecutor::commandLogging_ =
true;
27 if (commandLogging_ && !silent)
32 result.
exitCode = std::system(command.c_str());
38 Logger::error(
"[ProcessExecutor] system() failed to spawn shell for: {}", command);
50 if (!result.
success && commandLogging_ && !silent)
64 std::string bgCommand = command;
65 if (!bgCommand.empty() && bgCommand.find(
" &") == std::string::npos && bgCommand.back() !=
'&')
77 result.
exitCode = std::system(bgCommand.c_str());
83 Logger::error(
"[ProcessExecutor] system() failed to spawn shell for background command");
102 if (commandLogging_ && !silent)
119 std::ostringstream oss;
121 constexpr size_t kMaxOutput = 10u * 1024 * 1024;
123 while (std::fgets(buffer,
sizeof(buffer), pipe.
get()) !=
nullptr)
126 if (
static_cast<size_t>(oss.tellp()) > kMaxOutput)
130 result.
output = trimWhitespace(oss.str());
133 FILE* rawPipe = pipe.
release();
136 int status = ::pclose(rawPipe);
138 if (WIFEXITED(status))
140 result.
exitCode = WEXITSTATUS(status);
162 std::vector<std::string> lines;
172 size_t lineCount = 0;
174 while (std::fgets(buffer,
sizeof(buffer), pipe.
get()) !=
nullptr)
177 std::string line(buffer);
178 if (!line.empty() && line.back() ==
'\n')
183 lines.push_back(line);
186 if (maxLines > 0 && lineCount >= maxLines)
202 std::string command =
"tasklist /FI \"IMAGENAME eq " +
204 ".exe\" 2>NUL | find /I \"" +
218 std::string command =
"wmic process where name=\"" +
220 ".exe\" get ProcessId /format:list 2>NUL";
229 size_t pos = result.
output.find(
"ProcessId=");
230 if (pos != std::string::npos)
234 return std::stoi(result.
output.substr(pos + 10));
236 catch (
const std::exception& e)
238 Logger::warn(
"Failed to parse PID from wmic output: {}", e.what());
245 std::string command =
256 return std::stoi(result.
output);
258 catch (
const std::exception& e)
260 Logger::warn(
"Failed to parse PID from pgrep output: {}", e.what());
275 std::string command = force ?
"taskkill /F /PID " :
"taskkill /PID ";
276 command += std::to_string(pid) +
" >NUL 2>&1";
278 std::string signal = force ?
"SIGKILL" :
"SIGTERM";
279 std::string command =
"kill -" + signal +
" " + std::to_string(pid) +
" 2>/dev/null";
292 commandLogging_ = enabled;
299std::string ProcessExecutor::trimWhitespace(
const std::string& str)
306 size_t start = str.find_first_not_of(
" \t\n\r");
307 if (start == std::string::npos)
312 size_t end = str.find_last_not_of(
" \t\n\r");
313 return str.substr(start, end - start + 1);
static void debug(const std::string &message, Args &&... args)
Log debug message (verbose development info).
static void error(const std::string &message, Args &&... args)
Log error message (recoverable failure).
static void warn(const std::string &message, Args &&... args)
Log warning message (non-critical issue).
RAII wrapper for FILE* from popen().
FILE * release()
Release ownership of FILE* without closing.
FILE * get() const
Get raw FILE* pointer.
static ProcessResult executeWithOutput(const std::string &command, bool silent=false)
Execute command and capture stdout.
static bool killProcess(int pid, bool force=false)
Kill process by PID.
static void setCommandLogging(bool enabled)
Enable or disable command logging.
static ProcessResult executeBackground(const std::string &command)
Execute command in background (non-blocking).
static ProcessResult execute(const std::string &command, bool silent=false)
Execute command and check exit code.
static bool isProcessRunning(const std::string &processName)
Check if process is running by name.
static int getProcessPid(const std::string &processName)
Get PID of running process by name.
static std::vector< std::string > executeWithLines(const std::string &command, size_t maxLines=0)
Execute command and capture stdout as lines.
static std::string escapeArg(const std::string &arg)
Escape a string for safe use as a shell argument.
static std::string escapeForDoubleQuotes(const std::string &arg)
Escape a string for use in double quotes.
Result of process execution including exit code and output.
int exitCode
Process exit code (0 = success).
bool success
true if command executed successfully (exit code 0)
std::string output
Captured stdout (if requested).
std::string command
The command that was executed.