diff --git a/fetch-indexes-cpp/main.cpp b/fetch-indexes-cpp/main.cpp index 7d4e20c..1039a69 100644 --- a/fetch-indexes-cpp/main.cpp +++ b/fetch-indexes-cpp/main.cpp @@ -38,30 +38,40 @@ namespace fs = std::filesystem; // Function prototypes void printHelp(const char* programName); -void processRelease(const std::string& release, const YAML::Node& config); +void processRelease(const std::string& release, const YAML::Node& config, const std::string& logFileName); void refresh(const std::string& url, const std::string& pocket, const std::string& britneyCache, std::mutex& logMutex); -int executeAndLog(const std::string& command); +int executeAndLog(const std::string& command, const std::string& logFile); -// Execute a command and stream its output to std::cout in real time -int executeAndLog(const std::string& command) { +int executeAndLog(const std::string& command, const std::string& logFile) { std::string fullCommand = command + " 2>&1"; // Redirect stderr to stdout FILE* pipe = popen(fullCommand.c_str(), "r"); if (!pipe) { - std::cout << "Failed to run command: " << command << std::endl; + std::cerr << "Failed to run command: " << command << std::endl; + return -1; + } + + std::ofstream logStream(logFile, std::ios::app); + if (!logStream.is_open()) { + std::cerr << "Error: Unable to open log file: " << logFile << std::endl; + pclose(pipe); return -1; } char buffer[256]; while (fgets(buffer, sizeof(buffer), pipe) != nullptr) { std::cout << buffer; - std::cout.flush(); // Ensure real-time logging + logStream << buffer; // Write to the log file + logStream.flush(); // Ensure the log file gets updated in real-time + std::cout.flush(); // Ensure real-time output to console } + logStream.close(); + int exitCode = pclose(pipe); if (WIFEXITED(exitCode)) { return WEXITSTATUS(exitCode); } else { - return -1; // Abnormal termination + return -1; } } @@ -143,7 +153,7 @@ int main(int argc, char* argv[]) { std::time_t now_c = std::time(nullptr); char timestamp[20]; std::strftime(timestamp, sizeof(timestamp), "%Y%m%dT%H%M%S", std::gmtime(&now_c)); - std::string logFileName = LOG_DIR + "/" + timestamp + "_" + release + ".log"; + const std::string logFileName = LOG_DIR + "/" + timestamp + "_" + release + ".log"; // Open log file std::ofstream logFile(logFileName, std::ios::app); @@ -164,7 +174,7 @@ int main(int argc, char* argv[]) { std::cout << startTime << " - Running Britney for " << release << std::endl; // Process the release - processRelease(release, config); + processRelease(release, config, logFileName); // Restore stdout and stderr std::cout.rdbuf(coutBuf); @@ -177,7 +187,7 @@ int main(int argc, char* argv[]) { return 0; } -void processRelease(const std::string& RELEASE, const YAML::Node& config) { +void processRelease(const std::string& RELEASE, const YAML::Node& config, const std::string& logFileName) { // Extract configuration variables std::string MAIN_ARCHIVE = config["MAIN_ARCHIVE"].as(); std::string PORTS_ARCHIVE = config["PORTS_ARCHIVE"].as(); @@ -207,7 +217,7 @@ void processRelease(const std::string& RELEASE, const YAML::Node& config) { // Execute pending-packages script and capture its output std::string pendingCmd = "./pending-packages " + RELEASE; - int pendingResult = executeAndLog(pendingCmd); + int pendingResult = executeAndLog(pendingCmd, logFileName); if (pendingResult != 0) { std::cerr << "Error: pending-packages script failed for release " << RELEASE << std::endl; return; @@ -399,7 +409,7 @@ void processRelease(const std::string& RELEASE, const YAML::Node& config) { // Run britney.py std::string britneyCmd = BRITNEY_LOC + " -v --config britney.conf --series " + RELEASE; - int britneyResult = executeAndLog(britneyCmd); + int britneyResult = executeAndLog(britneyCmd, logFileName); if (britneyResult != 0) { std::cerr << "Error: Britney execution failed for release " << RELEASE << std::endl; return; @@ -445,11 +455,11 @@ void processRelease(const std::string& RELEASE, const YAML::Node& config) { " --to ppa:" + LP_TEAM + "/ubuntu/" + SOURCE_PPA + " --version " + packageInfo[1] + " " + PACKAGE; std::string removeCmd = REMOVE + " -y -s " + RELEASE + " --archive ppa:" + LP_TEAM + "/ubuntu/" + DEST_PPA + " --version " + packageInfo[1] + " --removal-comment=\"demoted to proposed\" " + PACKAGE; - int copyResult = executeAndLog(copyCmd); + int copyResult = executeAndLog(copyCmd, logFileName); if (copyResult != 0) { std::cerr << "Error: Copy command failed for package " << PACKAGE << std::endl; } - int removeResult = executeAndLog(removeCmd); + int removeResult = executeAndLog(removeCmd, logFileName); if (removeResult != 0) { std::cerr << "Error: Remove command failed for package " << PACKAGE << std::endl; } @@ -459,11 +469,11 @@ void processRelease(const std::string& RELEASE, const YAML::Node& config) { " --to ppa:" + LP_TEAM + "/ubuntu/" + DEST_PPA + " --version " + packageInfo[1] + " " + packageInfo[0]; std::string removeCmd = REMOVE + " -y -s " + RELEASE + " --archive ppa:" + LP_TEAM + "/ubuntu/" + SOURCE_PPA + " --version " + packageInfo[1] + " --removal-comment=\"moved to release\" " + packageInfo[0]; - int copyResult = executeAndLog(copyCmd); + int copyResult = executeAndLog(copyCmd, logFileName); if (copyResult != 0) { std::cerr << "Error: Copy command failed for package " << packageInfo[0] << std::endl; } - int removeResult = executeAndLog(removeCmd); + int removeResult = executeAndLog(removeCmd, logFileName); if (removeResult != 0) { std::cerr << "Error: Remove command failed for package " << packageInfo[0] << std::endl; } @@ -476,7 +486,7 @@ void processRelease(const std::string& RELEASE, const YAML::Node& config) { std::cout << "Run the grim reaper..." << std::endl; std::string grimCmd = "./grim-reaper " + RELEASE; - int grimResult = executeAndLog(grimCmd); + int grimResult = executeAndLog(grimCmd, logFileName); if (grimResult != 0) { std::cerr << "Error: Grim reaper execution failed for release " << RELEASE << std::endl; return;