88#include < mirror/sync_scheduler/JobManager.hpp>
99
1010// System Includes
11+ #include < sys/ioctl.h>
1112#include < sys/types.h>
1213#include < sys/wait.h>
1314#include < unistd.h>
2627#include < fstream>
2728#include < iterator>
2829#include < mutex>
30+ #include < random>
2931#include < stop_token>
3032#include < string>
3133#include < thread>
@@ -196,9 +198,14 @@ auto JobManager::reap_processes() -> std::vector<::pid_t>
196198 }
197199 else if (isKnownJob && exitStatus != EXIT_SUCCESS)
198200 {
201+ const auto logFileName
202+ = this ->write_stderr_to_file (childProcessID);
203+
199204 spdlog::warn (
200- " Project {} failed to sync! Exit code: {} (pid: {})" ,
205+ " Project {} failed to sync! The contents of STDERR for the "
206+ " process have been written to {}! Exit code: {} (pid: {})" ,
201207 m_ActiveJobs.at (childProcessID).jobName ,
208+ logFileName,
202209 exitStatus,
203210 childProcessID
204211 );
@@ -225,6 +232,40 @@ auto JobManager::reap_processes() -> std::vector<::pid_t>
225232 return completedJobs;
226233}
227234
235+ auto JobManager::write_stderr_to_file (const ::pid_t processID) -> std::string
236+ {
237+ static std::random_device randomDevice;
238+ static std::mt19937 randomGenerator (randomDevice ());
239+ static std::uniform_int_distribution distribution (1 , 10000 );
240+
241+ const auto logNumber = distribution (randomGenerator);
242+ const auto errorLogPath = std::filesystem::relative (" error-logs" );
243+
244+ auto logFileName = std::format (
245+ " {}-{}-stderr.log" ,
246+ m_ActiveJobs.at (processID).jobName ,
247+ logNumber
248+ );
249+
250+ std::ofstream logFile (errorLogPath / logFileName);
251+
252+ int bytesAvailable = 0 ;
253+ ::ioctl (m_ActiveJobs.at(processID).stderrPipe, FIONREAD, &bytesAvailable);
254+
255+ std::string stderrContents;
256+ stderrContents.resize (bytesAvailable + 1 );
257+
258+ ::read (
259+ m_ActiveJobs.at(processID).stderrPipe,
260+ stderrContents.data(),
261+ bytesAvailable
262+ );
263+
264+ logFile << stderrContents;
265+
266+ return logFileName;
267+ }
268+
228269// NOLINTNEXTLINE(*-no-recursion)
229270auto JobManager::interrupt_job (const ::pid_t processID) -> void
230271{
0 commit comments