1# Copyright 2021 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14"""Tools for working with tokenized logs.""" 15 16from dataclasses import dataclass 17 18 19def _mask(value: int, start: int, count: int) -> int: 20 mask = (1 << count) - 1 21 return (value & (mask << start)) >> start 22 23 24@dataclass(frozen=True) 25class Metadata: 26 """Parses the metadata payload sent by pw_log_tokenized.""" 27 _value: int 28 29 log_bits: int = 6 30 module_bits: int = 16 31 flag_bits: int = 10 32 33 def log_level(self) -> int: 34 return _mask(self._value, 0, self.log_bits) 35 36 def module_token(self) -> int: 37 return _mask(self._value, self.log_bits, self.module_bits) 38 39 def flags(self) -> int: 40 return _mask(self._value, self.log_bits + self.module_bits, 41 self.flag_bits) 42