debug info: Call register_event_debug_infos for all events
[babeltrace.git] / bindings / python / babeltrace / examples / ctf_writer.py
... / ...
CommitLineData
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# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26# SOFTWARE.
27
28import sys
29import tempfile
30import babeltrace.writer as btw
31
32
33trace_path = tempfile.mkdtemp()
34
35print("Writing trace at {}".format(trace_path))
36writer = btw.Writer(trace_path)
37
38clock = btw.Clock("A_clock")
39print("Clock name is \"{}\"".format(clock.name))
40clock.description = "Simple clock"
41print("Clock description is \"{}\"".format(clock.description))
42print("Clock frequency is {}".format(clock.frequency))
43print("Clock precision is {}".format(clock.precision))
44print("Clock offset_seconds is {}".format(clock.offset_seconds))
45print("Clock offset is {}".format(clock.offset))
46print("Clock is absolute: {}".format(clock.absolute))
47print("Clock time is {}".format(clock.time))
48print("Clock UUID is {}".format(clock.uuid))
49
50writer.add_clock(clock)
51writer.add_environment_field("Python_version", str(sys.version_info))
52
53stream_class = btw.StreamClass("test_stream")
54stream_class.clock = clock
55
56event_class = btw.EventClass("SimpleEvent")
57
58# Create a int32_t equivalent type
59int32_type = btw.IntegerFieldDeclaration(32)
60int32_type.signed = True
61
62# Create a uint16_t equivalent type
63uint16_type = btw.IntegerFieldDeclaration(16)
64uint16_type.signed = False
65
66# Add a custom uint16_t field in the stream's packet context
67packet_context_type = stream_class.packet_context_type
68print("\nFields in default packet context:")
69for field in packet_context_type.fields:
70 print(str(type(field[1])) + " " + field[0])
71packet_context_type.add_field(uint16_type, "a_custom_packet_context_field")
72stream_class.packet_context_type = packet_context_type
73
74# Create a string type
75string_type = btw.StringFieldDeclaration()
76
77# Create a structure type containing both an integer and a string
78structure_type = btw.StructureFieldDeclaration()
79structure_type.add_field(int32_type, "an_integer")
80structure_type.add_field(string_type, "a_string_field")
81event_class.add_field(structure_type, "structure_field")
82
83# Create a floating point type
84floating_point_type = btw.FloatFieldDeclaration()
85floating_point_type.exponent_digits = btw.FloatFieldDeclaration.FLT_EXP_DIG
86floating_point_type.mantissa_digits = btw.FloatFieldDeclaration.FLT_MANT_DIG
87event_class.add_field(floating_point_type, "float_field")
88
89# Create an enumeration type
90int10_type = btw.IntegerFieldDeclaration(10)
91enumeration_type = btw.EnumerationFieldDeclaration(int10_type)
92enumeration_type.add_mapping("FIRST_ENTRY", 0, 4)
93enumeration_type.add_mapping("SECOND_ENTRY", 5, 5)
94enumeration_type.add_mapping("THIRD_ENTRY", 6, 10)
95event_class.add_field(enumeration_type, "enum_field")
96
97# Create an array type
98array_type = btw.ArrayFieldDeclaration(int10_type, 5)
99event_class.add_field(array_type, "array_field")
100
101# Create a sequence type
102sequence_type = btw.SequenceFieldDeclaration(int32_type, "sequence_len")
103event_class.add_field(uint16_type, "sequence_len")
104event_class.add_field(sequence_type, "sequence_field")
105
106stream_class.add_event_class(event_class)
107stream = writer.create_stream(stream_class)
108
109for i in range(100):
110 event = btw.Event(event_class)
111
112 clock.time = i * 1000
113 structure_field = event.payload("structure_field")
114 integer_field = structure_field.field("an_integer")
115 integer_field.value = i
116
117 string_field = structure_field.field("a_string_field")
118 string_field.value = "Test string."
119
120 float_field = event.payload("float_field")
121 float_field.value = float(i) + (float(i) / 100.0)
122
123 array_field = event.payload("array_field")
124 for j in range(5):
125 element = array_field.field(j)
126 element.value = i + j
127
128 event.payload("sequence_len").value = i % 10
129 sequence_field = event.payload("sequence_field")
130 sequence_field.length = event.payload("sequence_len")
131 for j in range(event.payload("sequence_len").value):
132 sequence_field.field(j).value = i + j
133
134 enumeration_field = event.payload("enum_field")
135 integer_field = enumeration_field.container
136 enumeration_field.value = i % 10
137
138 stream.append_event(event)
139
140# Populate custom packet context field before flushing
141packet_context = stream.packet_context
142field = packet_context.field("a_custom_packet_context_field")
143field.value = 42
144
145stream.flush()
This page took 0.025418 seconds and 4 git commands to generate.