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 bt2
.field_class
25 import collections
.abc
28 import bt2
.trace_class
33 def _trace_destruction_listener_from_native(user_listener
, trace_ptr
):
34 trace
= bt2
.trace
._Trace
._create
_from
_ptr
_and
_get
_ref
(trace_ptr
)
38 class _Trace(object._SharedObject
, collections
.abc
.Mapping
):
39 _get_ref
= staticmethod(native_bt
.trace_get_ref
)
40 _put_ref
= staticmethod(native_bt
.trace_put_ref
)
43 count
= native_bt
.trace_get_stream_count(self
._ptr
)
47 def __getitem__(self
, id):
48 utils
._check
_uint
64(id)
50 stream_ptr
= native_bt
.trace_borrow_stream_by_id_const(self
._ptr
, id)
52 if stream_ptr
is None:
55 return bt2
.stream
._Stream
._create
_from
_ptr
_and
_get
_ref
(stream_ptr
)
58 for idx
in range(len(self
)):
59 stream_ptr
= native_bt
.trace_borrow_stream_by_index_const(self
._ptr
, idx
)
60 assert stream_ptr
is not None
62 id = native_bt
.stream_get_id(stream_ptr
)
69 trace_class_ptr
= native_bt
.trace_borrow_class(self
._ptr
)
70 assert trace_class_ptr
is not None
71 return bt2
.trace_class
._TraceClass
._create
_from
_ptr
_and
_get
_ref
(trace_class_ptr
)
75 return native_bt
.trace_get_name(self
._ptr
)
77 def _name(self
, name
):
78 utils
._check
_str
(name
)
79 status
= native_bt
.trace_set_name(self
._ptr
, name
)
80 utils
._handle
_func
_status
(status
,
81 "cannot set trace class object's name")
83 _name
= property(fset
=_name
)
85 def create_stream(self
, stream_class
, id=None, name
=None):
86 utils
._check
_type
(stream_class
, bt2
.stream_class
._StreamClass
)
88 if stream_class
.assigns_automatic_stream_id
:
90 raise ValueError("id provided, but stream class assigns automatic stream ids")
92 stream_ptr
= native_bt
.stream_create(stream_class
._ptr
, self
._ptr
)
95 raise ValueError("id not provided, but stream class does not assign automatic stream ids")
97 utils
._check
_uint
64(id)
98 stream_ptr
= native_bt
.stream_create_with_id(stream_class
._ptr
, self
._ptr
, id)
100 if stream_ptr
is None:
101 raise bt2
.CreationError('cannot create stream object')
103 stream
= bt2
.stream
._Stream
._create
_from
_ptr
(stream_ptr
)
110 def add_destruction_listener(self
, listener
):
111 '''Add a listener to be called when the trace is destroyed.'''
112 if not callable(listener
):
113 raise TypeError("'listener' parameter is not callable")
115 fn
= native_bt
.bt2_trace_add_destruction_listener
116 listener_from_native
= functools
.partial(_trace_destruction_listener_from_native
,
119 listener_id
= fn(self
._ptr
, listener_from_native
)
120 if listener_id
is None:
121 utils
._raise
_bt
2_error
('cannot add destruction listener to trace object')
123 return bt2
._ListenerHandle(listener_id
, self
)