1 # The MIT License (MIT)
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
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:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
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
23 from bt2
import native_bt
, object, utils
24 import collections
.abc
25 from bt2
import value
as bt2_value
26 from bt2
import stream
as bt2_stream
27 from bt2
import stream_class
as bt2_stream_class
33 def _bt2_trace_class():
34 from bt2
import trace_class
as bt2_trace_class
36 return bt2_trace_class
39 class _TraceEnvironmentConst(collections
.abc
.Mapping
):
40 _create_value_from_ptr_and_get_ref
= staticmethod(
41 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
44 def __init__(self
, trace
):
47 def __getitem__(self
, key
):
50 borrow_entry_fn
= native_bt
.trace_borrow_environment_entry_value_by_name_const
51 value_ptr
= borrow_entry_fn(self
._trace
._ptr
, key
)
56 return self
._create
_value
_from
_ptr
_and
_get
_ref
(value_ptr
)
59 count
= native_bt
.trace_get_environment_entry_count(self
._trace
._ptr
)
64 trace_ptr
= self
._trace
._ptr
66 for idx
in range(len(self
)):
67 borrow_entry_fn
= native_bt
.trace_borrow_environment_entry_by_index_const
68 entry_name
, _
= borrow_entry_fn(trace_ptr
, idx
)
69 assert entry_name
is not None
73 class _TraceEnvironment(_TraceEnvironmentConst
, collections
.abc
.MutableMapping
):
74 _create_value_from_ptr_and_get_ref
= staticmethod(
75 bt2_value
._create
_from
_ptr
_and
_get
_ref
78 def __setitem__(self
, key
, value
):
79 if isinstance(value
, str):
80 set_env_entry_fn
= native_bt
.trace_set_environment_entry_string
81 elif isinstance(value
, int):
82 set_env_entry_fn
= native_bt
.trace_set_environment_entry_integer
84 raise TypeError('expected str or int, got {}'.format(type(value
)))
86 status
= set_env_entry_fn(self
._trace
._ptr
, key
, value
)
87 utils
._handle
_func
_status
(status
, "cannot set trace object's environment entry")
89 def __delitem__(self
, key
):
90 raise NotImplementedError
93 class _TraceConst(object._SharedObject
, collections
.abc
.Mapping
):
94 _get_ref
= staticmethod(native_bt
.trace_get_ref
)
95 _put_ref
= staticmethod(native_bt
.trace_put_ref
)
96 _borrow_stream_ptr_by_id
= staticmethod(native_bt
.trace_borrow_stream_by_id_const
)
97 _borrow_stream_ptr_by_index
= staticmethod(
98 native_bt
.trace_borrow_stream_by_index_const
100 _borrow_class_ptr
= staticmethod(native_bt
.trace_borrow_class_const
)
101 _borrow_user_attributes_ptr
= staticmethod(
102 native_bt
.trace_borrow_user_attributes_const
104 _create_value_from_ptr_and_get_ref
= staticmethod(
105 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
107 _stream_pycls
= property(lambda _
: bt2_stream
._StreamConst
)
108 _trace_class_pycls
= property(lambda _
: _bt2_trace_class()._TraceClassConst
)
109 _trace_env_pycls
= property(lambda _
: _TraceEnvironmentConst
)
112 count
= native_bt
.trace_get_stream_count(self
._ptr
)
116 def __getitem__(self
, id):
117 utils
._check
_uint
64(id)
119 stream_ptr
= self
._borrow
_stream
_ptr
_by
_id
(self
._ptr
, id)
121 if stream_ptr
is None:
124 return self
._stream
_pycls
._create
_from
_ptr
_and
_get
_ref
(stream_ptr
)
127 for idx
in range(len(self
)):
128 stream_ptr
= self
._borrow
_stream
_ptr
_by
_index
(self
._ptr
, idx
)
129 assert stream_ptr
is not None
131 id = native_bt
.stream_get_id(stream_ptr
)
138 trace_class_ptr
= self
._borrow
_class
_ptr
(self
._ptr
)
139 assert trace_class_ptr
is not None
140 return self
._trace
_class
_pycls
._create
_from
_ptr
_and
_get
_ref
(trace_class_ptr
)
143 def user_attributes(self
):
144 ptr
= self
._borrow
_user
_attributes
_ptr
(self
._ptr
)
145 assert ptr
is not None
146 return self
._create
_value
_from
_ptr
_and
_get
_ref
(ptr
)
150 return native_bt
.trace_get_name(self
._ptr
)
154 uuid_bytes
= native_bt
.trace_get_uuid(self
._ptr
)
155 if uuid_bytes
is None:
158 return uuidp
.UUID(bytes
=uuid_bytes
)
161 def environment(self
):
162 return self
._trace
_env
_pycls
(self
)
164 def add_destruction_listener(self
, listener
):
165 '''Add a listener to be called when the trace is destroyed.'''
166 if not callable(listener
):
167 raise TypeError("'listener' parameter is not callable")
169 fn
= native_bt
.bt2_trace_add_destruction_listener
170 listener_from_native
= functools
.partial(
171 _trace_destruction_listener_from_native
, listener
174 status
, listener_id
= fn(self
._ptr
, listener_from_native
)
175 utils
._handle
_func
_status
(
176 status
, 'cannot add destruction listener to trace object'
179 return utils
._ListenerHandle
(listener_id
, self
)
181 def remove_destruction_listener(self
, listener_handle
):
182 utils
._check
_type
(listener_handle
, utils
._ListenerHandle
)
184 if listener_handle
._obj
.addr
!= self
.addr
:
186 'This trace destruction listener does not match the trace object.'
189 if listener_handle
._listener
_id
is None:
190 raise ValueError('This trace destruction listener was already removed.')
192 status
= native_bt
.trace_remove_destruction_listener(
193 self
._ptr
, listener_handle
._listener
_id
195 utils
._handle
_func
_status
(status
)
196 listener_handle
._listener
_id
= None
199 class _Trace(_TraceConst
):
200 _borrow_stream_ptr_by_id
= staticmethod(native_bt
.trace_borrow_stream_by_id
)
201 _borrow_stream_ptr_by_index
= staticmethod(native_bt
.trace_borrow_stream_by_index
)
202 _borrow_class_ptr
= staticmethod(native_bt
.trace_borrow_class
)
203 _borrow_user_attributes_ptr
= staticmethod(native_bt
.trace_borrow_user_attributes
)
204 _create_value_from_ptr_and_get_ref
= staticmethod(
205 bt2_value
._create
_from
_ptr
_and
_get
_ref
207 _stream_pycls
= property(lambda _
: bt2_stream
._Stream
)
208 _trace_class_pycls
= property(lambda _
: _bt2_trace_class()._TraceClass
)
209 _trace_env_pycls
= property(lambda _
: _TraceEnvironment
)
211 def _name(self
, name
):
212 utils
._check
_str
(name
)
213 status
= native_bt
.trace_set_name(self
._ptr
, name
)
214 utils
._handle
_func
_status
(status
, "cannot set trace class object's name")
216 _name
= property(fset
=_name
)
218 def _user_attributes(self
, user_attributes
):
219 value
= bt2_value
.create_value(user_attributes
)
220 utils
._check
_type
(value
, bt2_value
.MapValue
)
221 native_bt
.trace_set_user_attributes(self
._ptr
, value
._ptr
)
223 _user_attributes
= property(fset
=_user_attributes
)
225 def _uuid(self
, uuid
):
226 utils
._check
_type
(uuid
, uuidp
.UUID
)
227 native_bt
.trace_set_uuid(self
._ptr
, uuid
.bytes
)
229 _uuid
= property(fset
=_uuid
)
231 def create_stream(self
, stream_class
, id=None, name
=None, user_attributes
=None):
232 utils
._check
_type
(stream_class
, bt2_stream_class
._StreamClass
)
234 if stream_class
.assigns_automatic_stream_id
:
237 "id provided, but stream class assigns automatic stream ids"
240 stream_ptr
= native_bt
.stream_create(stream_class
._ptr
, self
._ptr
)
244 "id not provided, but stream class does not assign automatic stream ids"
247 utils
._check
_uint
64(id)
248 stream_ptr
= native_bt
.stream_create_with_id(
249 stream_class
._ptr
, self
._ptr
, id
252 if stream_ptr
is None:
253 raise bt2
._MemoryError('cannot create stream object')
255 stream
= bt2_stream
._Stream
._create
_from
_ptr
(stream_ptr
)
260 if user_attributes
is not None:
261 stream
._user
_attributes
= user_attributes
266 def _trace_destruction_listener_from_native(user_listener
, trace_ptr
):
267 trace
= _TraceConst
._create
_from
_ptr
_and
_get
_ref
(trace_ptr
)