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
34 class _TraceEnv(collections
.abc
.MutableMapping
):
35 def __init__(self
, trace
):
38 def __getitem__(self
, key
):
41 borrow_entry_fn
= native_bt
.trace_borrow_environment_entry_value_by_name_const
42 value_ptr
= borrow_entry_fn(self
._trace
._ptr
, key
)
47 return bt2
.value
._create
_from
_ptr
_and
_get
_ref
(value_ptr
)
49 def __setitem__(self
, key
, value
):
50 if isinstance(value
, str):
51 set_env_entry_fn
= native_bt
.trace_set_environment_entry_string
52 elif isinstance(value
, int):
53 set_env_entry_fn
= native_bt
.trace_set_environment_entry_integer
55 raise TypeError('expected str or int, got {}'.format(type(value
)))
57 status
= set_env_entry_fn(self
._trace
._ptr
, key
, value
)
58 utils
._handle
_func
_status
(status
, "cannot set trace object's environment entry")
60 def __delitem__(self
, key
):
61 raise NotImplementedError
64 count
= native_bt
.trace_get_environment_entry_count(self
._trace
._ptr
)
69 trace_ptr
= self
._trace
_env
._trace
._ptr
71 for idx
in range(len(self
)):
72 borrow_entry_fn
= native_bt
.trace_borrow_environment_entry_by_index_const
73 entry_name
, _
= borrow_entry_fn(trace_ptr
, idx
)
74 assert entry_name
is not None
78 def _trace_destruction_listener_from_native(user_listener
, trace_ptr
):
79 trace
= bt2
.trace
._Trace
._create
_from
_ptr
_and
_get
_ref
(trace_ptr
)
83 class _Trace(object._SharedObject
, collections
.abc
.Mapping
):
84 _get_ref
= staticmethod(native_bt
.trace_get_ref
)
85 _put_ref
= staticmethod(native_bt
.trace_put_ref
)
88 count
= native_bt
.trace_get_stream_count(self
._ptr
)
92 def __getitem__(self
, id):
93 utils
._check
_uint
64(id)
95 stream_ptr
= native_bt
.trace_borrow_stream_by_id_const(self
._ptr
, id)
97 if stream_ptr
is None:
100 return bt2
.stream
._Stream
._create
_from
_ptr
_and
_get
_ref
(stream_ptr
)
103 for idx
in range(len(self
)):
104 stream_ptr
= native_bt
.trace_borrow_stream_by_index_const(self
._ptr
, idx
)
105 assert stream_ptr
is not None
107 id = native_bt
.stream_get_id(stream_ptr
)
114 trace_class_ptr
= native_bt
.trace_borrow_class(self
._ptr
)
115 assert trace_class_ptr
is not None
116 return bt2
.trace_class
._TraceClass
._create
_from
_ptr
_and
_get
_ref
(trace_class_ptr
)
120 return native_bt
.trace_get_name(self
._ptr
)
122 def _name(self
, name
):
123 utils
._check
_str
(name
)
124 status
= native_bt
.trace_set_name(self
._ptr
, name
)
125 utils
._handle
_func
_status
(status
, "cannot set trace class object's name")
127 _name
= property(fset
=_name
)
131 uuid_bytes
= native_bt
.trace_get_uuid(self
._ptr
)
132 if uuid_bytes
is None:
135 return uuidp
.UUID(bytes
=uuid_bytes
)
137 def _uuid(self
, uuid
):
138 utils
._check
_type
(uuid
, uuidp
.UUID
)
139 native_bt
.trace_set_uuid(self
._ptr
, uuid
.bytes
)
141 _uuid
= property(fset
=_uuid
)
145 return _TraceEnv(self
)
147 def create_stream(self
, stream_class
, id=None, name
=None):
148 utils
._check
_type
(stream_class
, bt2
.stream_class
._StreamClass
)
150 if stream_class
.assigns_automatic_stream_id
:
153 "id provided, but stream class assigns automatic stream ids"
156 stream_ptr
= native_bt
.stream_create(stream_class
._ptr
, self
._ptr
)
160 "id not provided, but stream class does not assign automatic stream ids"
163 utils
._check
_uint
64(id)
164 stream_ptr
= native_bt
.stream_create_with_id(
165 stream_class
._ptr
, self
._ptr
, id
168 if stream_ptr
is None:
169 raise bt2
.MemoryError('cannot create stream object')
171 stream
= bt2
.stream
._Stream
._create
_from
_ptr
(stream_ptr
)
178 def add_destruction_listener(self
, listener
):
179 '''Add a listener to be called when the trace is destroyed.'''
180 if not callable(listener
):
181 raise TypeError("'listener' parameter is not callable")
183 fn
= native_bt
.bt2_trace_add_destruction_listener
184 listener_from_native
= functools
.partial(
185 _trace_destruction_listener_from_native
, listener
188 status
, listener_id
= fn(self
._ptr
, listener_from_native
)
189 utils
._handle
_func
_status
(
190 status
, 'cannot add destruction listener to trace object'
193 return bt2
._ListenerHandle(listener_id
, self
)