Commit | Line | Data |
---|---|---|
81447b5b PP |
1 | # The MIT License (MIT) |
2 | # | |
811644b8 | 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 | |
81447b5b | 24 | import collections.abc |
3fb99a22 PP |
25 | from bt2 import value as bt2_value |
26 | from bt2 import stream as bt2_stream | |
27 | from bt2 import trace_class as bt2_trace_class | |
28 | from bt2 import stream_class as bt2_stream_class | |
81447b5b | 29 | import bt2 |
8c2367b8 | 30 | import functools |
335a2da5 PP |
31 | import uuid as uuidp |
32 | ||
33 | ||
34 | class _TraceEnv(collections.abc.MutableMapping): | |
35 | def __init__(self, trace): | |
36 | self._trace = trace | |
37 | ||
38 | def __getitem__(self, key): | |
39 | utils._check_str(key) | |
40 | ||
41 | borrow_entry_fn = native_bt.trace_borrow_environment_entry_value_by_name_const | |
42 | value_ptr = borrow_entry_fn(self._trace._ptr, key) | |
43 | ||
44 | if value_ptr is None: | |
45 | raise KeyError(key) | |
46 | ||
3fb99a22 | 47 | return bt2_value._create_from_ptr_and_get_ref(value_ptr) |
335a2da5 PP |
48 | |
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 | |
54 | else: | |
55 | raise TypeError('expected str or int, got {}'.format(type(value))) | |
56 | ||
57 | status = set_env_entry_fn(self._trace._ptr, key, value) | |
cfbd7cf3 | 58 | utils._handle_func_status(status, "cannot set trace object's environment entry") |
335a2da5 PP |
59 | |
60 | def __delitem__(self, key): | |
61 | raise NotImplementedError | |
62 | ||
63 | def __len__(self): | |
64 | count = native_bt.trace_get_environment_entry_count(self._trace._ptr) | |
65 | assert count >= 0 | |
66 | return count | |
67 | ||
68 | def __iter__(self): | |
69 | trace_ptr = self._trace_env._trace._ptr | |
70 | ||
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 | |
75 | yield entry_name | |
81447b5b PP |
76 | |
77 | ||
8c2367b8 | 78 | def _trace_destruction_listener_from_native(user_listener, trace_ptr): |
3fb99a22 | 79 | trace = _Trace._create_from_ptr_and_get_ref(trace_ptr) |
8c2367b8 | 80 | user_listener(trace) |
81447b5b | 81 | |
81447b5b | 82 | |
2c6f8520 | 83 | class _Trace(object._SharedObject, collections.abc.Mapping): |
8c2367b8 SM |
84 | _get_ref = staticmethod(native_bt.trace_get_ref) |
85 | _put_ref = staticmethod(native_bt.trace_put_ref) | |
811644b8 PP |
86 | |
87 | def __len__(self): | |
8c2367b8 SM |
88 | count = native_bt.trace_get_stream_count(self._ptr) |
89 | assert count >= 0 | |
811644b8 PP |
90 | return count |
91 | ||
8c2367b8 SM |
92 | def __getitem__(self, id): |
93 | utils._check_uint64(id) | |
81447b5b | 94 | |
8c2367b8 | 95 | stream_ptr = native_bt.trace_borrow_stream_by_id_const(self._ptr, id) |
81447b5b | 96 | |
8c2367b8 SM |
97 | if stream_ptr is None: |
98 | raise KeyError(id) | |
81447b5b | 99 | |
3fb99a22 | 100 | return bt2_stream._Stream._create_from_ptr_and_get_ref(stream_ptr) |
81447b5b PP |
101 | |
102 | def __iter__(self): | |
8c2367b8 SM |
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 | |
81447b5b | 106 | |
8c2367b8 SM |
107 | id = native_bt.stream_get_id(stream_ptr) |
108 | assert id >= 0 | |
81447b5b | 109 | |
8c2367b8 | 110 | yield id |
81447b5b | 111 | |
05abc762 PP |
112 | @property |
113 | def cls(self): | |
114 | trace_class_ptr = native_bt.trace_borrow_class(self._ptr) | |
115 | assert trace_class_ptr is not None | |
3fb99a22 | 116 | return bt2_trace_class._TraceClass._create_from_ptr_and_get_ref(trace_class_ptr) |
05abc762 | 117 | |
81447b5b PP |
118 | @property |
119 | def name(self): | |
50842bdc | 120 | return native_bt.trace_get_name(self._ptr) |
81447b5b | 121 | |
8c2367b8 | 122 | def _name(self, name): |
81447b5b | 123 | utils._check_str(name) |
d24d5663 | 124 | status = native_bt.trace_set_name(self._ptr, name) |
cfbd7cf3 | 125 | utils._handle_func_status(status, "cannot set trace class object's name") |
81447b5b | 126 | |
8c2367b8 | 127 | _name = property(fset=_name) |
811644b8 | 128 | |
335a2da5 PP |
129 | @property |
130 | def uuid(self): | |
131 | uuid_bytes = native_bt.trace_get_uuid(self._ptr) | |
132 | if uuid_bytes is None: | |
133 | return | |
134 | ||
135 | return uuidp.UUID(bytes=uuid_bytes) | |
136 | ||
137 | def _uuid(self, uuid): | |
138 | utils._check_type(uuid, uuidp.UUID) | |
139 | native_bt.trace_set_uuid(self._ptr, uuid.bytes) | |
140 | ||
141 | _uuid = property(fset=_uuid) | |
142 | ||
143 | @property | |
144 | def env(self): | |
145 | return _TraceEnv(self) | |
146 | ||
8c2367b8 | 147 | def create_stream(self, stream_class, id=None, name=None): |
3fb99a22 | 148 | utils._check_type(stream_class, bt2_stream_class._StreamClass) |
81447b5b | 149 | |
8c2367b8 SM |
150 | if stream_class.assigns_automatic_stream_id: |
151 | if id is not None: | |
cfbd7cf3 FD |
152 | raise ValueError( |
153 | "id provided, but stream class assigns automatic stream ids" | |
154 | ) | |
81447b5b | 155 | |
8c2367b8 SM |
156 | stream_ptr = native_bt.stream_create(stream_class._ptr, self._ptr) |
157 | else: | |
158 | if id is None: | |
cfbd7cf3 FD |
159 | raise ValueError( |
160 | "id not provided, but stream class does not assign automatic stream ids" | |
161 | ) | |
81447b5b | 162 | |
8c2367b8 | 163 | utils._check_uint64(id) |
cfbd7cf3 FD |
164 | stream_ptr = native_bt.stream_create_with_id( |
165 | stream_class._ptr, self._ptr, id | |
166 | ) | |
81447b5b | 167 | |
8c2367b8 | 168 | if stream_ptr is None: |
694c792b | 169 | raise bt2._MemoryError('cannot create stream object') |
81447b5b | 170 | |
3fb99a22 | 171 | stream = bt2_stream._Stream._create_from_ptr(stream_ptr) |
81447b5b | 172 | |
8c2367b8 SM |
173 | if name is not None: |
174 | stream._name = name | |
81447b5b | 175 | |
8c2367b8 | 176 | return stream |
81447b5b | 177 | |
8c2367b8 SM |
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") | |
81447b5b | 182 | |
d24d5663 | 183 | fn = native_bt.bt2_trace_add_destruction_listener |
cfbd7cf3 FD |
184 | listener_from_native = functools.partial( |
185 | _trace_destruction_listener_from_native, listener | |
186 | ) | |
81447b5b | 187 | |
ee2cad25 SM |
188 | status, listener_id = fn(self._ptr, listener_from_native) |
189 | utils._handle_func_status( | |
190 | status, 'cannot add destruction listener to trace object' | |
191 | ) | |
81447b5b | 192 | |
3fb99a22 | 193 | return utils._ListenerHandle(listener_id, self) |