Add Python bindings for CTF-IR event-field getters
[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))
033fb0a7
JG
39
40writer.add_clock(clock)
41writer.add_environment_field("Python_version", str(sys.version_info))
42
43stream_class = CTFWriter.StreamClass("test_stream")
14001277 44stream_class.clock = clock
033fb0a7
JG
45
46event_class = CTFWriter.EventClass("SimpleEvent")
47
48# Create a int32_t equivalent type
14001277
JG
49int32_type = CTFWriter.IntegerFieldDeclaration(32)
50int32_type.signed = True
033fb0a7 51
821ca76c
JG
52# Create a uint16_t equivalent type
53uint16_type = CTFWriter.IntegerFieldDeclaration(16)
54uint16_type.signed = False
55
033fb0a7 56# Create a string type
14001277 57string_type = CTFWriter.StringFieldDeclaration()
033fb0a7
JG
58
59# Create a structure type containing both an integer and a string
14001277 60structure_type = CTFWriter.StructureFieldDeclaration()
033fb0a7
JG
61structure_type.add_field(int32_type, "an_integer")
62structure_type.add_field(string_type, "a_string_field")
63event_class.add_field(structure_type, "structure_field")
64
65# Create a floating point type
14001277
JG
66floating_point_type = CTFWriter.FloatFieldDeclaration()
67floating_point_type.exponent_digits = CTFWriter.FloatFieldDeclaration.FLT_EXP_DIG
68floating_point_type.mantissa_digits = CTFWriter.FloatFieldDeclaration.FLT_MANT_DIG
033fb0a7
JG
69event_class.add_field(floating_point_type, "float_field")
70
71# Create an enumeration type
14001277
JG
72int10_type = CTFWriter.IntegerFieldDeclaration(10)
73enumeration_type = CTFWriter.EnumerationFieldDeclaration(int10_type)
033fb0a7
JG
74enumeration_type.add_mapping("FIRST_ENTRY", 0, 4)
75enumeration_type.add_mapping("SECOND_ENTRY", 5, 5)
76enumeration_type.add_mapping("THIRD_ENTRY", 6, 10)
77event_class.add_field(enumeration_type, "enum_field")
78
79# Create an array type
14001277 80array_type = CTFWriter.ArrayFieldDeclaration(int10_type, 5)
033fb0a7
JG
81event_class.add_field(array_type, "array_field")
82
821ca76c
JG
83# Create a sequence type
84sequence_type = CTFWriter.SequenceFieldDeclaration(int32_type, "sequence_len")
85event_class.add_field(uint16_type, "sequence_len")
86event_class.add_field(sequence_type, "sequence_field")
87
033fb0a7
JG
88stream_class.add_event_class(event_class)
89stream = writer.create_stream(stream_class)
90
91for i in range(100):
92 event = CTFWriter.Event(event_class)
93
14001277
JG
94 clock.time = i * 1000
95 structure_field = event.payload("structure_field")
96 integer_field = structure_field.field("an_integer")
97 integer_field.value = i
033fb0a7 98
14001277
JG
99 string_field = structure_field.field("a_string_field")
100 string_field.value = "Test string."
033fb0a7 101
14001277
JG
102 float_field = event.payload("float_field")
103 float_field.value = float(i) + (float(i) / 100.0)
033fb0a7 104
14001277 105 array_field = event.payload("array_field")
033fb0a7 106 for j in range(5):
14001277
JG
107 element = array_field.field(j)
108 element.value = i + j
033fb0a7 109
821ca76c
JG
110 event.payload("sequence_len").value = i % 10
111 sequence_field = event.payload("sequence_field")
112 sequence_field.length = event.payload("sequence_len")
113 for j in range(event.payload("sequence_len").value):
114 sequence_field.field(j).value = i + j
115
14001277
JG
116 enumeration_field = event.payload("enum_field")
117 integer_field = enumeration_field.container
821ca76c 118 enumeration_field.value = i % 10
033fb0a7
JG
119
120 stream.append_event(event)
121
122stream.flush()
This page took 0.02773 seconds and 4 git commands to generate.