Add Python bindings for CTF-IR clock's getters
[babeltrace.git] / bindings / python / examples / ctf_writer.py
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
20 import sys
21 import tempfile
22 from babeltrace import *
23
24 trace_path = tempfile.mkdtemp()
25
26 print("Writing trace at {}".format(trace_path))
27 writer = CTFWriter.Writer(trace_path)
28
29 clock = CTFWriter.Clock("A_clock")
30 print("Clock name is \"{}\"".format(clock.name))
31 clock.description = "Simple clock"
32 print("Clock description is \"{}\"".format(clock.description))
33 print("Clock frequency is {}".format(clock.frequency))
34 print("Clock precision is {}".format(clock.precision))
35 print("Clock offset_seconds is {}".format(clock.offset_seconds))
36 print("Clock offset is {}".format(clock.offset))
37 print("Clock is absolute: {}".format(clock.absolute))
38 print("Clock time is {}".format(clock.time))
39
40 writer.add_clock(clock)
41 writer.add_environment_field("Python_version", str(sys.version_info))
42
43 stream_class = CTFWriter.StreamClass("test_stream")
44 stream_class.clock = clock
45
46 event_class = CTFWriter.EventClass("SimpleEvent")
47
48 # Create a int32_t equivalent type
49 int32_type = CTFWriter.IntegerFieldDeclaration(32)
50 int32_type.signed = True
51
52 # Create a string type
53 string_type = CTFWriter.StringFieldDeclaration()
54
55 # Create a structure type containing both an integer and a string
56 structure_type = CTFWriter.StructureFieldDeclaration()
57 structure_type.add_field(int32_type, "an_integer")
58 structure_type.add_field(string_type, "a_string_field")
59 event_class.add_field(structure_type, "structure_field")
60
61 # Create a floating point type
62 floating_point_type = CTFWriter.FloatFieldDeclaration()
63 floating_point_type.exponent_digits = CTFWriter.FloatFieldDeclaration.FLT_EXP_DIG
64 floating_point_type.mantissa_digits = CTFWriter.FloatFieldDeclaration.FLT_MANT_DIG
65 event_class.add_field(floating_point_type, "float_field")
66
67 # Create an enumeration type
68 int10_type = CTFWriter.IntegerFieldDeclaration(10)
69 enumeration_type = CTFWriter.EnumerationFieldDeclaration(int10_type)
70 enumeration_type.add_mapping("FIRST_ENTRY", 0, 4)
71 enumeration_type.add_mapping("SECOND_ENTRY", 5, 5)
72 enumeration_type.add_mapping("THIRD_ENTRY", 6, 10)
73 event_class.add_field(enumeration_type, "enum_field")
74
75 # Create an array type
76 array_type = CTFWriter.ArrayFieldDeclaration(int10_type, 5)
77 event_class.add_field(array_type, "array_field")
78
79 stream_class.add_event_class(event_class)
80 stream = writer.create_stream(stream_class)
81
82 for i in range(100):
83 event = CTFWriter.Event(event_class)
84
85 clock.time = i * 1000
86 structure_field = event.payload("structure_field")
87 integer_field = structure_field.field("an_integer")
88 integer_field.value = i
89
90 string_field = structure_field.field("a_string_field")
91 string_field.value = "Test string."
92
93 float_field = event.payload("float_field")
94 float_field.value = float(i) + (float(i) / 100.0)
95
96 array_field = event.payload("array_field")
97 for j in range(5):
98 element = array_field.field(j)
99 element.value = i + j
100
101 enumeration_field = event.payload("enum_field")
102 integer_field = enumeration_field.container
103 integer_field.value = i % 10
104
105 stream.append_event(event)
106
107 stream.flush()
This page took 0.032328 seconds and 4 git commands to generate.