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) &
38 ~static_cast<uint32_t>(PW_LOG_LEVEL_BITMASK));
39}
40
43constexpr inline std::tuple<uint32_t, uint8_t> UnpackLineLevel(
44 uint32_t line_and_level) {
45 return std::make_tuple(
46 (line_and_level & ~static_cast<uint32_t>(PW_LOG_LEVEL_BITMASK)) >>
48 line_and_level & PW_LOG_LEVEL_BITMASK);
49}
50
61 unsigned int flags,
62 std::string_view module_name,
63 std::string_view thread_name,
64 std::string_view file_name,
65 int line_number,
66 int64_t ticks_since_epoch,
67 std::string_view message,
68 ByteSpan encode_buffer);
69
73pwpb::LogEntry::MemoryEncoder CreateEncoderAndEncodeTokenizedLog(
75 ConstByteSpan tokenized_data,
76 int64_t ticks_since_epoch,
77 ByteSpan encode_buffer);
78
89 ConstByteSpan tokenized_data,
90 int64_t ticks_since_epoch,
91 ByteSpan encode_buffer) {
92 pwpb::LogEntry::MemoryEncoder encoder = CreateEncoderAndEncodeTokenizedLog(
93 metadata, tokenized_data, ticks_since_epoch, encode_buffer);
94 PW_TRY(encoder.status());
95 return ConstByteSpan(encoder);
96}
97
100 const uint8_t* tokenized_data,
101 size_t tokenized_data_size,
102 int64_t ticks_since_epoch,
103 ByteSpan encode_buffer) {
104 return EncodeTokenizedLog(metadata,
105 as_bytes(span(tokenized_data, tokenized_data_size)),
106 ticks_since_epoch,
107 encode_buffer);
108}
109
120 const uint8_t* tokenized_data,
121 size_t tokenized_data_size,
122 int64_t ticks_since_epoch,
123 ConstByteSpan thread_name,
124 ByteSpan encode_buffer) {
125 pwpb::LogEntry::MemoryEncoder encoder = CreateEncoderAndEncodeTokenizedLog(
126 metadata,
127 as_bytes(span(tokenized_data, tokenized_data_size)),
128 ticks_since_epoch,
129 encode_buffer);
130 if (!thread_name.empty()) {
131 encoder.WriteThread(thread_name).IgnoreError();
132 }
133 PW_TRY(encoder.status());
134 return ConstByteSpan(encoder);
135}
136
147 ConstByteSpan tokenized_data,
148 int64_t ticks_since_epoch,
149 ConstByteSpan thread_name,
150 ByteSpan encode_buffer) {
151 pwpb::LogEntry::MemoryEncoder encoder = CreateEncoderAndEncodeTokenizedLog(
152 metadata, tokenized_data, ticks_since_epoch, encode_buffer);
153 if (!thread_name.empty()) {
154 encoder.WriteThread(thread_name).IgnoreError();
155 }
156 PW_TRY(encoder.status());
157 return ConstByteSpan(encoder);
158}
159
161
162} // namespace pw::log
Definition: result.h:145
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:87
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:43
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