| 13 | #include "cudaq/runtime/logger/logger.h" |
| 14 | |
| 15 | void cudaq::RecordLogParser::parse(const std::string &outputLog) { |
| 16 | ScopedTraceWithContext(cudaq::TIMING_RUN, "RecordLogParser::parse"); |
| 17 | CUDAQ_DBG("Parsing log:\n{}", outputLog); |
| 18 | std::vector<std::string> lines = cudaq::split(outputLog, '\n'); |
| 19 | if (lines.empty()) |
| 20 | return; |
| 21 | |
| 22 | // Collect log from a single shot and process it only if it is successful. |
| 23 | bool processingShot = false; |
| 24 | // Maintain the starting index of each shot's data |
| 25 | std::size_t shotStart = 0; |
| 26 | |
| 27 | for (std::size_t idx = 0; idx < lines.size(); ++idx) { |
| 28 | const auto &line = lines[idx]; |
| 29 | std::vector<std::string> entries = cudaq::split(line, '\t'); |
| 30 | if (entries.empty()) |
| 31 | continue; |
| 32 | |
| 33 | const std::string &recordType = entries[0]; |
| 34 | if (recordType == "HEADER") |
| 35 | handleHeader(entries); |
| 36 | else if (recordType == "METADATA") |
| 37 | handleMetadata(entries); |
| 38 | else if (recordType == "START") { |
| 39 | processingShot = true; |
| 40 | shotStart = 0; |
| 41 | } else if (recordType == "OUTPUT") { |
| 42 | if (processingShot) |
| 43 | shotStart = shotStart == 0 ? idx : shotStart; |
| 44 | else |
| 45 | handleOutput(entries); |
| 46 | } else if (recordType == "END") { |
| 47 | if (entries.size() < 2) |
| 48 | throw std::runtime_error("Missing shot status"); |
| 49 | if (entries[1] == "0") { |
| 50 | if (processingShot) { |
| 51 | // Successful shot, process it |
| 52 | for (std::size_t j = shotStart; j < idx; ++j) |
| 53 | handleOutput(cudaq::split(lines[j], '\t')); |
| 54 | } |
| 55 | } else { |
| 56 | CUDAQ_DBG("Discarding shot data due to non-zero END status."); |
| 57 | } |
| 58 | processingShot = false; |
| 59 | shotStart = 0; |
| 60 | containerMeta.reset(); |
| 61 | } else { |
| 62 | throw std::runtime_error("Invalid record type: " + recordType); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void cudaq::RecordLogParser::handleHeader( |
| 68 | const std::vector<std::string> &entries) { |