xref: /aosp_15_r20/external/pigweed/pw_system/log_backend.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2022 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 <array>
16 #include <cstddef>
17 #include <mutex>
18 
19 #include "pw_bytes/span.h"
20 #include "pw_chrono/system_clock.h"
21 #include "pw_log/proto_utils.h"
22 #include "pw_log_string/handler.h"
23 #include "pw_log_tokenized/handler.h"
24 #include "pw_log_tokenized/metadata.h"
25 #include "pw_metric/global.h"
26 #include "pw_multisink/multisink.h"
27 #include "pw_result/result.h"
28 #include "pw_string/string_builder.h"
29 #include "pw_sync/interrupt_spin_lock.h"
30 #include "pw_sync/lock_annotations.h"
31 #include "pw_system/config.h"
32 #include "pw_system/log.h"
33 
34 namespace pw::system {
35 namespace {
36 
37 // Sample metric usage.
38 PW_METRIC_GROUP_GLOBAL(log_metric_group, "log");
39 PW_METRIC(log_metric_group, total_created, "total_created", 0u);
40 PW_METRIC(log_metric_group, total_dropped, "total_dropped", 0u);
41 
42 // Buffer used to encode each log entry before saving into log buffer.
43 sync::InterruptSpinLock log_encode_lock;
44 std::array<std::byte, PW_SYSTEM_MAX_LOG_ENTRY_SIZE> log_encode_buffer
45     PW_GUARDED_BY(log_encode_lock);
46 
47 // String-only logs may need to be formatted first. This buffer is required
48 // so the format string may be passed to the proto log encode.
49 std::array<std::byte, PW_SYSTEM_MAX_LOG_ENTRY_SIZE> log_format_buffer
50     PW_GUARDED_BY(log_encode_lock);
51 
52 const int64_t boot_time_count =
53     pw::chrono::SystemClock::now().time_since_epoch().count();
54 
55 }  // namespace
56 
57 // Provides time since boot in units defined by the target's pw_chrono backend.
GetTimestamp()58 int64_t GetTimestamp() {
59   return pw::chrono::SystemClock::now().time_since_epoch().count() -
60          boot_time_count;
61 }
62 
63 // Implementation for tokenized log handling. This will be optimized out for
64 // devices that only use string logging.
pw_log_tokenized_HandleLog(uint32_t payload,const uint8_t message[],size_t size_bytes)65 extern "C" void pw_log_tokenized_HandleLog(uint32_t payload,
66                                            const uint8_t message[],
67                                            size_t size_bytes) {
68   log_tokenized::Metadata metadata = payload;
69   const int64_t timestamp = GetTimestamp();
70 
71   std::lock_guard lock(log_encode_lock);
72   Result<ConstByteSpan> encoded_log_result = log::EncodeTokenizedLog(
73       metadata, message, size_bytes, timestamp, log_encode_buffer);
74   if (!encoded_log_result.ok()) {
75     GetMultiSink().HandleDropped();
76     total_dropped.Increment();
77     return;
78   }
79   GetMultiSink().HandleEntry(encoded_log_result.value());
80   total_created.Increment();
81 }
82 
83 // Implementation for string log handling. This will be optimized out for
84 // devices that only use tokenized logging.
pw_log_string_HandleMessageVaList(int level,unsigned int flags,const char * module_name,const char * file_name,int line_number,const char * message,va_list args)85 extern "C" void pw_log_string_HandleMessageVaList(int level,
86                                                   unsigned int flags,
87                                                   const char* module_name,
88                                                   const char* file_name,
89                                                   int line_number,
90                                                   const char* message,
91                                                   va_list args) {
92   const int64_t timestamp = GetTimestamp();
93 
94   std::lock_guard lock(log_encode_lock);
95   StringBuilder message_builder(log_format_buffer);
96   message_builder.FormatVaList(message, args);
97 
98   Result<ConstByteSpan> encoded_log_result =
99       log::EncodeLog(level,
100                      flags,
101                      module_name,
102                      /*thread_name=*/{},
103                      file_name,
104                      line_number,
105                      timestamp,
106                      message_builder.view(),
107                      log_encode_buffer);
108   if (!encoded_log_result.ok()) {
109     GetMultiSink().HandleDropped();
110     total_dropped.Increment();
111     return;
112   }
113   GetMultiSink().HandleEntry(encoded_log_result.value());
114   total_created.Increment();
115 }
116 
117 }  // namespace pw::system
118