pythonlttngbabeltrace

how to print complete trace event using babeltrace-python api?


How do I print the complete trace using the python reader api of babeltrace ?

Using below I can get the fields of an event, but how can I print the complete trace as babeltrace does.

import babeltrace
import sys

trace_collection = babeltrace.TraceCollection()
trace_path = sys.argv[1]

trace_collection.add_traces_recursive(trace_path, 'ctf')

for event in trace_collection.events:
  print event.timestamp, event.cycles, event.name

And using the event dictionary, the fields can be fetched. But how to replicate the babeltrace output with python reader ?

for event in trace_collection.events:
  for field in event.items():
    print field

sample babeltrace output:

[2015-10-20 15:16:34.600508066] (+1.481059687) samplehost sample_trace_provider:INFO: { cpu_id = 1 }, { fileAndLine = "sampletest.cpp:330", msg = "Directing trace stream to channel #0" }

Let me know if any further information is needed.


Solution

  • You can't do this in a single statement as you would expect. This is because the Babeltrace Python bindings classes do not implement __str__ recursively.

    The default output format you're getting when running the babeltrace command is called ctf-text and is implemented in C. There is certainly a way to replicate ctf-text's output, but you would need to implement a pretty-printer manually in Python.