C/C++ API Reference
Loading...
Searching...
No Matches
proto_utils.h
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#pragma once
15
16#include <string_view>
17
18#include "pw_bytes/span.h"
19#include "pw_log/levels.h"
20#include "pw_log/proto/log.pwpb.h"
21#include "pw_log_tokenized/metadata.h"
22#include "pw_result/result.h"
23#include "pw_status/try.h"
24
25namespace pw::log {
26
28
35constexpr inline uint32_t PackLineLevel(uint32_t line_number, uint8_t level) {
36 return (level & PW_LOG_LEVEL_BITMASK) |
37 ((line_number << PW_LOG_LEVEL_BITS) & ~PW_LOG_LEVEL_BITMASK);
38}
39
42constexpr inline std::tuple<uint32_t, uint8_t> UnpackLineLevel(
43 uint32_t line_and_level) {
44 return std::make_tuple(
45 (line_and_level & ~PW_LOG_LEVEL_BITMASK) >> PW_LOG_LEVEL_BITS,
46 line_and_level & PW_LOG_LEVEL_BITMASK);
47}
48
59 unsigned int flags,
60 std::string_view module_name,
61 std::string_view thread_name,
62 std::string_view file_name,
63 int line_number,
64 int64_t ticks_since_epoch,
65 std::string_view message,
66 ByteSpan encode_buffer);
67
71pwpb::LogEntry::MemoryEncoder CreateEncoderAndEncodeTokenizedLog(
73 ConstByteSpan tokenized_data,
74 int64_t ticks_since_epoch,
75 ByteSpan encode_buffer);
76
87 ConstByteSpan tokenized_data,
88 int64_t ticks_since_epoch,
89 ByteSpan encode_buffer) {
90 pwpb::LogEntry::MemoryEncoder encoder = CreateEncoderAndEncodeTokenizedLog(
91 metadata, tokenized_data, ticks_since_epoch, encode_buffer);
92 PW_TRY(encoder.status());
93 return ConstByteSpan(encoder);
94}
95
98 const uint8_t* tokenized_data,
99 size_t tokenized_data_size,
100 int64_t ticks_since_epoch,
101 ByteSpan encode_buffer) {
102 return EncodeTokenizedLog(metadata,
103 as_bytes(span(tokenized_data, tokenized_data_size)),
104 ticks_since_epoch,
105 encode_buffer);
106}
107
118 const uint8_t* tokenized_data,
119 size_t tokenized_data_size,
120 int64_t ticks_since_epoch,
121 ConstByteSpan thread_name,
122 ByteSpan encode_buffer) {
123 pwpb::LogEntry::MemoryEncoder encoder = CreateEncoderAndEncodeTokenizedLog(
124 metadata,
125 as_bytes(span(tokenized_data, tokenized_data_size)),
126 ticks_since_epoch,
127 encode_buffer);
128 if (!thread_name.empty()) {
129 encoder.WriteThread(thread_name).IgnoreError();
130 }
131 PW_TRY(encoder.status());
132 return ConstByteSpan(encoder);
133}
134
145 ConstByteSpan tokenized_data,
146 int64_t ticks_since_epoch,
147 ConstByteSpan thread_name,
148 ByteSpan encode_buffer) {
149 pwpb::LogEntry::MemoryEncoder encoder = CreateEncoderAndEncodeTokenizedLog(
150 metadata, tokenized_data, ticks_since_epoch, encode_buffer);
151 if (!thread_name.empty()) {
152 encoder.WriteThread(thread_name).IgnoreError();
153 }
154 PW_TRY(encoder.status());
155 return ConstByteSpan(encoder);
156}
157
159
160} // namespace pw::log
Definition: poll.h:25
Definition: metadata.h:62
Result< ConstByteSpan > EncodeTokenizedLog(log_tokenized::Metadata metadata, ConstByteSpan tokenized_data, int64_t ticks_since_epoch, ByteSpan encode_buffer)
Definition: proto_utils.h:85
constexpr uint32_t PackLineLevel(uint32_t line_number, uint8_t level)
Definition: proto_utils.h:35
Result< ConstByteSpan > EncodeLog(int level, unsigned int flags, std::string_view module_name, std::string_view thread_name, std::string_view file_name, int line_number, int64_t ticks_since_epoch, std::string_view message, ByteSpan encode_buffer)
constexpr std::tuple< uint32_t, uint8_t > UnpackLineLevel(uint32_t line_and_level)
Definition: proto_utils.h:42
pwpb::LogEntry::MemoryEncoder CreateEncoderAndEncodeTokenizedLog(log_tokenized::Metadata metadata, ConstByteSpan tokenized_data, int64_t ticks_since_epoch, ByteSpan encode_buffer)
#define PW_LOG_LEVEL_BITS
Number of bits required to represent the log level.
Definition: levels.h:32
#define PW_TRY(expr)
Returns early if expr is a non-OK Status or Result.
Definition: try.h:27