Commit | Line | Data |
---|---|---|
81447b5b PP |
1 | # The MIT License (MIT) |
2 | # | |
f6a5e476 | 3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> |
81447b5b PP |
4 | # |
5 | # Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | # of this software and associated documentation files (the "Software"), to deal | |
7 | # in the Software without restriction, including without limitation the rights | |
8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | # copies of the Software, and to permit persons to whom the Software is | |
10 | # furnished to do so, subject to the following conditions: | |
11 | # | |
12 | # The above copyright notice and this permission notice shall be included in | |
13 | # all copies or substantial portions of the Software. | |
14 | # | |
15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
21 | # THE SOFTWARE. | |
22 | ||
23 | from bt2 import native_bt, object, utils | |
c946c9de | 24 | from bt2 import field_class as bt2_field_class |
81447b5b | 25 | import collections.abc |
c946c9de PP |
26 | from bt2 import value as bt2_value |
27 | from bt2 import stream as bt2_stream | |
28 | from bt2 import trace_class as bt2_trace_class | |
29 | from bt2 import stream_class as bt2_stream_class | |
81447b5b | 30 | import bt2 |
f377a958 | 31 | import functools |
cd03c43c PP |
32 | import uuid as uuidp |
33 | ||
34 | ||
35 | class _TraceEnv(collections.abc.MutableMapping): | |
36 | def __init__(self, trace): | |
37 | self._trace = trace | |
38 | ||
39 | def __getitem__(self, key): | |
40 | utils._check_str(key) | |
41 | ||
42 | borrow_entry_fn = native_bt.trace_borrow_environment_entry_value_by_name_const | |
43 | value_ptr = borrow_entry_fn(self._trace._ptr, key) | |
44 | ||
45 | if value_ptr is None: | |
46 | raise KeyError(key) | |
47 | ||
c946c9de | 48 | return bt2_value._create_from_ptr_and_get_ref(value_ptr) |
cd03c43c PP |
49 | |
50 | def __setitem__(self, key, value): | |
51 | if isinstance(value, str): | |
52 | set_env_entry_fn = native_bt.trace_set_environment_entry_string | |
53 | elif isinstance(value, int): | |
54 | set_env_entry_fn = native_bt.trace_set_environment_entry_integer | |
55 | else: | |
56 | raise TypeError('expected str or int, got {}'.format(type(value))) | |
57 | ||
58 | status = set_env_entry_fn(self._trace._ptr, key, value) | |
61d96b89 | 59 | utils._handle_func_status(status, "cannot set trace object's environment entry") |
cd03c43c PP |
60 | |
61 | def __delitem__(self, key): | |
62 | raise NotImplementedError | |
63 | ||
64 | def __len__(self): | |
65 | count = native_bt.trace_get_environment_entry_count(self._trace._ptr) | |
66 | assert count >= 0 | |
67 | return count | |
68 | ||
69 | def __iter__(self): | |
70 | trace_ptr = self._trace_env._trace._ptr | |
71 | ||
72 | for idx in range(len(self)): | |
73 | borrow_entry_fn = native_bt.trace_borrow_environment_entry_by_index_const | |
74 | entry_name, _ = borrow_entry_fn(trace_ptr, idx) | |
75 | assert entry_name is not None | |
76 | yield entry_name | |
81447b5b PP |
77 | |
78 | ||
f377a958 | 79 | def _trace_destruction_listener_from_native(user_listener, trace_ptr): |
c946c9de | 80 | trace = _Trace._create_from_ptr_and_get_ref(trace_ptr) |
f377a958 | 81 | user_listener(trace) |
81447b5b | 82 | |
81447b5b | 83 | |
78668ecd | 84 | class _Trace(object._SharedObject, collections.abc.Mapping): |
f377a958 SM |
85 | _get_ref = staticmethod(native_bt.trace_get_ref) |
86 | _put_ref = staticmethod(native_bt.trace_put_ref) | |
f6a5e476 PP |
87 | |
88 | def __len__(self): | |
f377a958 SM |
89 | count = native_bt.trace_get_stream_count(self._ptr) |
90 | assert count >= 0 | |
f6a5e476 PP |
91 | return count |
92 | ||
f377a958 SM |
93 | def __getitem__(self, id): |
94 | utils._check_uint64(id) | |
81447b5b | 95 | |
f377a958 | 96 | stream_ptr = native_bt.trace_borrow_stream_by_id_const(self._ptr, id) |
81447b5b | 97 | |
f377a958 SM |
98 | if stream_ptr is None: |
99 | raise KeyError(id) | |
81447b5b | 100 | |
c946c9de | 101 | return bt2_stream._Stream._create_from_ptr_and_get_ref(stream_ptr) |
81447b5b PP |
102 | |
103 | def __iter__(self): | |
f377a958 SM |
104 | for idx in range(len(self)): |
105 | stream_ptr = native_bt.trace_borrow_stream_by_index_const(self._ptr, idx) | |
106 | assert stream_ptr is not None | |
81447b5b | 107 | |
f377a958 SM |
108 | id = native_bt.stream_get_id(stream_ptr) |
109 | assert id >= 0 | |
81447b5b | 110 | |
f377a958 | 111 | yield id |
81447b5b | 112 | |
a7f0580d PP |
113 | @property |
114 | def cls(self): | |
115 | trace_class_ptr = native_bt.trace_borrow_class(self._ptr) | |
116 | assert trace_class_ptr is not None | |
c946c9de | 117 | return bt2_trace_class._TraceClass._create_from_ptr_and_get_ref(trace_class_ptr) |
a7f0580d | 118 | |
81447b5b PP |
119 | @property |
120 | def name(self): | |
839d52a5 | 121 | return native_bt.trace_get_name(self._ptr) |
81447b5b | 122 | |
f377a958 | 123 | def _name(self, name): |
81447b5b | 124 | utils._check_str(name) |
fb25b9e3 | 125 | status = native_bt.trace_set_name(self._ptr, name) |
61d96b89 | 126 | utils._handle_func_status(status, "cannot set trace class object's name") |
81447b5b | 127 | |
f377a958 | 128 | _name = property(fset=_name) |
f6a5e476 | 129 | |
cd03c43c PP |
130 | @property |
131 | def uuid(self): | |
132 | uuid_bytes = native_bt.trace_get_uuid(self._ptr) | |
133 | if uuid_bytes is None: | |
134 | return | |
135 | ||
136 | return uuidp.UUID(bytes=uuid_bytes) | |
137 | ||
138 | def _uuid(self, uuid): | |
139 | utils._check_type(uuid, uuidp.UUID) | |
140 | native_bt.trace_set_uuid(self._ptr, uuid.bytes) | |
141 | ||
142 | _uuid = property(fset=_uuid) | |
143 | ||
144 | @property | |
145 | def env(self): | |
146 | return _TraceEnv(self) | |
147 | ||
f377a958 | 148 | def create_stream(self, stream_class, id=None, name=None): |
c946c9de | 149 | utils._check_type(stream_class, bt2_stream_class._StreamClass) |
81447b5b | 150 | |
f377a958 SM |
151 | if stream_class.assigns_automatic_stream_id: |
152 | if id is not None: | |
61d96b89 FD |
153 | raise ValueError( |
154 | "id provided, but stream class assigns automatic stream ids" | |
155 | ) | |
81447b5b | 156 | |
f377a958 SM |
157 | stream_ptr = native_bt.stream_create(stream_class._ptr, self._ptr) |
158 | else: | |
159 | if id is None: | |
61d96b89 FD |
160 | raise ValueError( |
161 | "id not provided, but stream class does not assign automatic stream ids" | |
162 | ) | |
81447b5b | 163 | |
f377a958 | 164 | utils._check_uint64(id) |
61d96b89 FD |
165 | stream_ptr = native_bt.stream_create_with_id( |
166 | stream_class._ptr, self._ptr, id | |
167 | ) | |
81447b5b | 168 | |
f377a958 | 169 | if stream_ptr is None: |
614743a5 | 170 | raise bt2._MemoryError('cannot create stream object') |
81447b5b | 171 | |
c946c9de | 172 | stream = bt2_stream._Stream._create_from_ptr(stream_ptr) |
81447b5b | 173 | |
f377a958 SM |
174 | if name is not None: |
175 | stream._name = name | |
81447b5b | 176 | |
f377a958 | 177 | return stream |
81447b5b | 178 | |
f377a958 SM |
179 | def add_destruction_listener(self, listener): |
180 | '''Add a listener to be called when the trace is destroyed.''' | |
181 | if not callable(listener): | |
182 | raise TypeError("'listener' parameter is not callable") | |
81447b5b | 183 | |
fb25b9e3 | 184 | fn = native_bt.bt2_trace_add_destruction_listener |
61d96b89 FD |
185 | listener_from_native = functools.partial( |
186 | _trace_destruction_listener_from_native, listener | |
187 | ) | |
81447b5b | 188 | |
48e95b3d SM |
189 | status, listener_id = fn(self._ptr, listener_from_native) |
190 | utils._handle_func_status( | |
191 | status, 'cannot add destruction listener to trace object' | |
192 | ) | |
81447b5b | 193 | |
c946c9de | 194 | return utils._ListenerHandle(listener_id, self) |