xref: /aosp_15_r20/external/pigweed/pw_log/proto_utils.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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 
15 #include "pw_log/proto_utils.h"
16 
17 #include <string_view>
18 
19 #include "pw_bytes/endian.h"
20 #include "pw_log/levels.h"
21 #include "pw_log_tokenized/metadata.h"
22 #include "pw_protobuf/wire_format.h"
23 #include "pw_span/span.h"
24 
25 namespace pw::log {
26 
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)27 Result<ConstByteSpan> EncodeLog(int level,
28                                 unsigned int flags,
29                                 std::string_view module_name,
30                                 std::string_view thread_name,
31                                 std::string_view file_name,
32                                 int line_number,
33                                 int64_t ticks_since_epoch,
34                                 std::string_view message,
35                                 ByteSpan encode_buffer) {
36   // Encode message to the LogEntry protobuf.
37   pwpb::LogEntry::MemoryEncoder encoder(encode_buffer);
38 
39   if (message.empty()) {
40     return Status::InvalidArgument();
41   }
42 
43   // Defer status checks until the end.
44   Status status = encoder.WriteMessage(as_bytes(span<const char>(message)));
45   status = encoder.WriteLineLevel(PackLineLevel(line_number, level));
46   if (flags != 0) {
47     status = encoder.WriteFlags(flags);
48   }
49   status = encoder.WriteTimestamp(ticks_since_epoch);
50 
51   // Module name and file name may or may not be present.
52   if (!module_name.empty()) {
53     status = encoder.WriteModule(as_bytes(span<const char>(module_name)));
54   }
55   if (!file_name.empty()) {
56     status = encoder.WriteFile(as_bytes(span<const char>(file_name)));
57   }
58   if (!thread_name.empty()) {
59     status = encoder.WriteThread(as_bytes(span<const char>(thread_name)));
60   }
61   PW_TRY(encoder.status());
62   return ConstByteSpan(encoder);
63 }
64 
CreateEncoderAndEncodeTokenizedLog(pw::log_tokenized::Metadata metadata,ConstByteSpan tokenized_data,int64_t ticks_since_epoch,ByteSpan encode_buffer)65 pwpb::LogEntry::MemoryEncoder CreateEncoderAndEncodeTokenizedLog(
66     pw::log_tokenized::Metadata metadata,
67     ConstByteSpan tokenized_data,
68     int64_t ticks_since_epoch,
69     ByteSpan encode_buffer) {
70   // Encode message to the LogEntry protobuf.
71   pwpb::LogEntry::MemoryEncoder encoder(encode_buffer);
72 
73   // Defer status checks until the end.
74   Status status = encoder.WriteMessage(tokenized_data);
75   status = encoder.WriteLineLevel(
76       PackLineLevel(metadata.line_number(), metadata.level()));
77   if (metadata.flags() != 0) {
78     status = encoder.WriteFlags(metadata.flags());
79   }
80   status = encoder.WriteTimestamp(ticks_since_epoch);
81   if (metadata.module() != 0) {
82     const uint32_t little_endian_module =
83         bytes::ConvertOrderTo(endian::little, metadata.module());
84     status = encoder.WriteModule(as_bytes(span(&little_endian_module, 1)));
85   }
86   return encoder;
87 }
88 
89 }  // namespace pw::log
90