Python bindings: Make examples PEP8 compliant
[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")
b0bb560c 30print("Clock name is \"{}\"".format(clock.name))
14001277 31clock.description = "Simple clock"
b0bb560c
JG
32print("Clock description is \"{}\"".format(clock.description))
33print("Clock frequency is {}".format(clock.frequency))
34print("Clock precision is {}".format(clock.precision))
35print("Clock offset_seconds is {}".format(clock.offset_seconds))
36print("Clock offset is {}".format(clock.offset))
37print("Clock is absolute: {}".format(clock.absolute))
38print("Clock time is {}".format(clock.time))
da64442f 39print("Clock UUID is {}".format(clock.uuid))
033fb0a7
JG
40
41writer.add_clock(clock)
42writer.add_environment_field("Python_version", str(sys.version_info))
43
44stream_class = CTFWriter.StreamClass("test_stream")
14001277 45stream_class.clock = clock
033fb0a7
JG
46
47event_class = CTFWriter.EventClass("SimpleEvent")
48
49# Create a int32_t equivalent type
14001277
JG
50int32_type = CTFWriter.IntegerFieldDeclaration(32)
51int32_type.signed = True
033fb0a7 52
821ca76c
JG
53# Create a uint16_t equivalent type
54uint16_type = CTFWriter.IntegerFieldDeclaration(16)
55uint16_type.signed = False
56
7df927c7
JG
57# Add a custom uint16_t field in the stream's packet context
58packet_context_type = stream_class.packet_context_type
59print("\nFields in default packet context:")
60for field in packet_context_type.fields:
a47ab64a 61 print(str(type(field[1])) + " " + field[0])
7df927c7
JG
62packet_context_type.add_field(uint16_type, "a_custom_packet_context_field")
63stream_class.packet_context_type = packet_context_type
64
033fb0a7 65# Create a string type
14001277 66string_type = CTFWriter.StringFieldDeclaration()
033fb0a7
JG
67
68# Create a structure type containing both an integer and a string
14001277 69structure_type = CTFWriter.StructureFieldDeclaration()
033fb0a7
JG
70structure_type.add_field(int32_type, "an_integer")
71structure_type.add_field(string_type, "a_string_field")
72event_class.add_field(structure_type, "structure_field")
73
74# Create a floating point type
14001277
JG
75floating_point_type = CTFWriter.FloatFieldDeclaration()
76floating_point_type.exponent_digits = CTFWriter.FloatFieldDeclaration.FLT_EXP_DIG
77floating_point_type.mantissa_digits = CTFWriter.FloatFieldDeclaration.FLT_MANT_DIG
033fb0a7
JG
78event_class.add_field(floating_point_type, "float_field")
79
80# Create an enumeration type
14001277
JG
81int10_type = CTFWriter.IntegerFieldDeclaration(10)
82enumeration_type = CTFWriter.EnumerationFieldDeclaration(int10_type)
033fb0a7
JG
83enumeration_type.add_mapping("FIRST_ENTRY", 0, 4)
84enumeration_type.add_mapping("SECOND_ENTRY", 5, 5)
85enumeration_type.add_mapping("THIRD_ENTRY", 6, 10)
86event_class.add_field(enumeration_type, "enum_field")
87
88# Create an array type
14001277 89array_type = CTFWriter.ArrayFieldDeclaration(int10_type, 5)
033fb0a7
JG
90event_class.add_field(array_type, "array_field")
91
821ca76c
JG
92# Create a sequence type
93sequence_type = CTFWriter.SequenceFieldDeclaration(int32_type, "sequence_len")
94event_class.add_field(uint16_type, "sequence_len")
95event_class.add_field(sequence_type, "sequence_field")
96
033fb0a7
JG
97stream_class.add_event_class(event_class)
98stream = writer.create_stream(stream_class)
99
100for i in range(100):
a47ab64a 101 event = CTFWriter.Event(event_class)
033fb0a7 102
a47ab64a
JG
103 clock.time = i * 1000
104 structure_field = event.payload("structure_field")
105 integer_field = structure_field.field("an_integer")
106 integer_field.value = i
033fb0a7 107
a47ab64a
JG
108 string_field = structure_field.field("a_string_field")
109 string_field.value = "Test string."
033fb0a7 110
a47ab64a
JG
111 float_field = event.payload("float_field")
112 float_field.value = float(i) + (float(i) / 100.0)
033fb0a7 113
a47ab64a
JG
114 array_field = event.payload("array_field")
115 for j in range(5):
116 element = array_field.field(j)
117 element.value = i + j
033fb0a7 118
a47ab64a
JG
119 event.payload("sequence_len").value = i % 10
120 sequence_field = event.payload("sequence_field")
121 sequence_field.length = event.payload("sequence_len")
122 for j in range(event.payload("sequence_len").value):
123 sequence_field.field(j).value = i + j
821ca76c 124
a47ab64a
JG
125 enumeration_field = event.payload("enum_field")
126 integer_field = enumeration_field.container
127 enumeration_field.value = i % 10
033fb0a7 128
a47ab64a 129 stream.append_event(event)
033fb0a7 130
7df927c7
JG
131# Populate custom packet context field before flushing
132packet_context = stream.packet_context
133field = packet_context.field("a_custom_packet_context_field")
134field.value = 42
135
033fb0a7 136stream.flush()
This page took 0.029021 seconds and 4 git commands to generate.