Add a usage example for the CTF Writer Python bindings
[babeltrace.git] / bindings / python / examples / ctf_writer.py
CommitLineData
033fb0a7
JG
1#!/usr/bin/env python3
2# ctf_writer.py
3#
4# Babeltrace CTF Writer example script.
5#
6# Copyright 2013 EfficiOS Inc.
7#
8# Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9#
10# Permission is hereby granted, free of charge, to any person obtaining a copy
11# of this software and associated documentation files (the "Software"), to deal
12# in the Software without restriction, including without limitation the rights
13# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14# copies of the Software, and to permit persons to whom the Software is
15# furnished to do so, subject to the following conditions:
16#
17# The above copyright notice and this permission notice shall be included in
18# all copies or substantial portions of the Software.
19
20import sys
21import tempfile
22from babeltrace import *
23
24trace_path = tempfile.mkdtemp()
25
26print("Writing trace at {}".format(trace_path))
27writer = CTFWriter.Writer(trace_path)
28
29clock = CTFWriter.Clock("A_clock")
30clock.set_description("Simple clock")
31
32writer.add_clock(clock)
33writer.add_environment_field("Python_version", str(sys.version_info))
34
35stream_class = CTFWriter.StreamClass("test_stream")
36stream_class.set_clock(clock)
37
38event_class = CTFWriter.EventClass("SimpleEvent")
39
40# Create a int32_t equivalent type
41int32_type = CTFWriter.FieldTypeInteger(32)
42int32_type.set_signed(True)
43
44# Create a string type
45string_type = CTFWriter.FieldTypeString()
46
47# Create a structure type containing both an integer and a string
48structure_type = CTFWriter.FieldTypeStructure()
49structure_type.add_field(int32_type, "an_integer")
50structure_type.add_field(string_type, "a_string_field")
51event_class.add_field(structure_type, "structure_field")
52
53# Create a floating point type
54floating_point_type = CTFWriter.FieldTypeFloatingPoint()
55floating_point_type.set_exponent_digits(CTFWriter.FieldTypeFloatingPoint.FLT_EXP_DIG)
56floating_point_type.set_mantissa_digits(CTFWriter.FieldTypeFloatingPoint.FLT_MANT_DIG)
57event_class.add_field(floating_point_type, "float_field")
58
59# Create an enumeration type
60int10_type = CTFWriter.FieldTypeInteger(10)
61enumeration_type = CTFWriter.FieldTypeEnumeration(int10_type)
62enumeration_type.add_mapping("FIRST_ENTRY", 0, 4)
63enumeration_type.add_mapping("SECOND_ENTRY", 5, 5)
64enumeration_type.add_mapping("THIRD_ENTRY", 6, 10)
65event_class.add_field(enumeration_type, "enum_field")
66
67# Create an array type
68array_type = CTFWriter.FieldTypeArray(int10_type, 5)
69event_class.add_field(array_type, "array_field")
70
71stream_class.add_event_class(event_class)
72stream = writer.create_stream(stream_class)
73
74for i in range(100):
75 event = CTFWriter.Event(event_class)
76
77 structure_field = event.get_payload("structure_field")
78 integer_field = structure_field.get_field("an_integer")
79 integer_field.set_value(i)
80
81 string_field = structure_field.get_field("a_string_field")
82 string_field.set_value("Test string.")
83
84 float_field = event.get_payload("float_field")
85 float_field.set_value(float(i) + (float(i) / 100.0))
86
87 array_field = event.get_payload("array_field")
88 for j in range(5):
89 element = array_field.get_field(j)
90 element.set_value(i+j)
91
92 enumeration_field = event.get_payload("enum_field")
93 integer_field = enumeration_field.get_container()
94 integer_field.set_value(i % 10)
95
96 stream.append_event(event)
97
98stream.flush()
This page took 0.025648 seconds and 4 git commands to generate.