| 55 | } // namespace |
| 56 | |
| 57 | void cudaq::bindTrace(nanobind::module_ &mod) { |
| 58 | auto trace = mod.def_submodule("trace"); |
| 59 | |
| 60 | nanobind::class_<TraceSpan>(trace, "span") |
| 61 | .def("__init__", |
| 62 | [](TraceSpan *self, const std::string &name, |
| 63 | nanobind::kwargs kwargs) { |
| 64 | new (self) TraceSpan(name, formatKwargs(kwargs)); |
| 65 | }) |
| 66 | .def("__enter__", [](TraceSpan &self) { self.enter(); }) |
| 67 | .def( |
| 68 | "__exit__", |
| 69 | [](TraceSpan &self, nanobind::object, nanobind::object, |
| 70 | nanobind::object) { self.exit(); }, |
| 71 | nanobind::arg("type").none(), nanobind::arg("value").none(), |
| 72 | nanobind::arg("traceback").none()); |
| 73 | |
| 74 | // Abstract base used only for upcasting in set_backend / get_backend. |
| 75 | nanobind::class_<cudaq::TraceBackend>(trace, "TraceBackend"); |
| 76 | |
| 77 | nanobind::class_<cudaq::ChromeTraceBackend, cudaq::TraceBackend>( |
| 78 | trace, "ChromeBackend") |
| 79 | .def(nanobind::new_([](std::string path) { |
| 80 | return std::make_shared<cudaq::ChromeTraceBackend>( |
| 81 | std::move(path)); |
| 82 | }), |
| 83 | nanobind::arg("path") = std::string{}, |
| 84 | "Construct a Chrome backend. If `path` is empty, the backend is " |
| 85 | "pure in-memory and the destructor does not write a file; call " |
| 86 | "to_json() / to_dict() / write_file() to retrieve events. If " |
| 87 | "`path` is set, the destructor writes Chrome Trace Event Format " |
| 88 | "JSON to `path`.") |
| 89 | .def("to_json", &cudaq::ChromeTraceBackend::toJson, |
| 90 | "Return captured events as a Chrome Trace Event Format JSON " |
| 91 | "string (same bytes the destructor would write to file).") |
| 92 | .def( |
| 93 | "to_dict", |
| 94 | [](cudaq::ChromeTraceBackend &self) { |
| 95 | auto jsonMod = nanobind::module_::import_("json"); |
| 96 | return jsonMod.attr("loads")(self.toJson()); |
| 97 | }, |
| 98 | "Return captured events as a parsed Python dict.") |
| 99 | .def("write_file", &cudaq::ChromeTraceBackend::writeFile, |
| 100 | nanobind::arg("path") = nanobind::none(), |
| 101 | "Write the current buffer as JSON to `path`, or to the ctor path " |
| 102 | "if omitted. Non-destructive: the buffer stays intact and the " |
| 103 | "backend keeps capturing.") |
| 104 | .def("clear", &cudaq::ChromeTraceBackend::clear, |
| 105 | "Drop all buffered events."); |
| 106 | |
| 107 | nanobind::class_<cudaq::SpdlogTraceBackend, cudaq::TraceBackend>( |
| 108 | trace, "SpdlogBackend") |
| 109 | .def(nanobind::new_( |
| 110 | [] { return std::make_shared<cudaq::SpdlogTraceBackend>(); }), |
| 111 | "Route trace events through spdlog. Output respects the current " |
| 112 | "CUDAQ log level."); |
| 113 | |
| 114 | trace.def( |
nothing calls this directly
no test coverage detected