xref: /aosp_15_r20/external/pigweed/pw_system/log.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_system/log.h"
16 
17 #include <array>
18 #include <cstddef>
19 
20 #include "pw_log_rpc/rpc_log_drain.h"
21 #include "pw_log_rpc/rpc_log_drain_map.h"
22 #include "pw_multisink/multisink.h"
23 #include "pw_sync/lock_annotations.h"
24 #include "pw_sync/mutex.h"
25 #include "pw_system/config.h"
26 
27 namespace pw::system {
28 namespace {
29 
30 using log_rpc::RpcLogDrain;
31 
32 // Storage container for MultiSink used for deferred logging.
33 std::array<std::byte, PW_SYSTEM_LOG_BUFFER_SIZE> log_buffer;
34 
35 // To save RAM, share the mutex and buffer between drains, since drains are
36 // flushed sequentially.
37 sync::Mutex drains_mutex;
38 
39 // Buffer to decode and remove entries from log buffer, to send to a drain.
40 //
41 // TODO(amontanez): pw_log_rpc should provide a helper for this since there's
42 // proto encoding overhead unaccounted for here.
43 static_assert(rpc::MaxSafePayloadSize() >= PW_SYSTEM_MAX_LOG_ENTRY_SIZE);
44 std::array<std::byte, PW_SYSTEM_MAX_LOG_ENTRY_SIZE> log_decode_buffer
45     PW_GUARDED_BY(drains_mutex);
46 #if PW_SYSTEM_EXTRA_LOGGING_CHANNEL_ID != PW_SYSTEM_LOGGING_CHANNEL_ID
47 std::array<std::byte, PW_SYSTEM_MAX_LOG_ENTRY_SIZE> log_decode_buffer_extra
48     PW_GUARDED_BY(drains_mutex);
49 #endif
50 
51 #if PW_SYSTEM_EXTRA_LOGGING_CHANNEL_ID != PW_SYSTEM_LOGGING_CHANNEL_ID
52 constexpr size_t drain_count = 2;
53 #else
54 constexpr size_t drain_count = 1;
55 #endif
56 std::array<RpcLogDrain, drain_count> drains{{
57     RpcLogDrain(kLoggingRpcChannelId,
58                 log_decode_buffer,
59                 drains_mutex,
60                 RpcLogDrain::LogDrainErrorHandling::kIgnoreWriterErrors),
61 #if PW_SYSTEM_EXTRA_LOGGING_CHANNEL_ID != PW_SYSTEM_LOGGING_CHANNEL_ID
62     RpcLogDrain(kExtraLoggingRpcChannelId,
63                 log_decode_buffer_extra,
64                 drains_mutex,
65                 RpcLogDrain::LogDrainErrorHandling::kIgnoreWriterErrors),
66 #endif
67 }};
68 
69 log_rpc::RpcLogDrainMap drain_map(drains);
70 
71 constexpr size_t kMaxPackedLogMessagesSize = rpc::MaxSafePayloadSize();
72 std::array<std::byte, kMaxPackedLogMessagesSize> log_packing_buffer;
73 
74 }  // namespace
75 
76 // Deferred log buffer, for storing log entries while logging_thread_ streams
77 // them independently.
GetMultiSink()78 multisink::MultiSink& GetMultiSink() {
79   static multisink::MultiSink multisink(log_buffer);
80   return multisink;
81 }
82 
GetLogThread()83 log_rpc::RpcLogDrainThread& GetLogThread() {
84   static log_rpc::RpcLogDrainThread logging_thread(
85       GetMultiSink(), drain_map, log_packing_buffer);
86   return logging_thread;
87 }
88 
GetLogService()89 log_rpc::LogService& GetLogService() {
90   static log_rpc::LogService log_service(drain_map);
91   return log_service;
92 }
93 
94 }  // namespace pw::system
95