Ewout van Bekkum | 5ddc01d | 2022-03-08 12:50:25 -0800 | [diff] [blame] | 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/metadata.h" |
| 24 | #include "pw_multisink/multisink.h" |
| 25 | #include "pw_result/result.h" |
| 26 | #include "pw_string/string_builder.h" |
| 27 | #include "pw_sync/interrupt_spin_lock.h" |
| 28 | #include "pw_sync/lock_annotations.h" |
| 29 | #include "pw_system/config.h" |
| 30 | #include "pw_system_private/log.h" |
| 31 | #include "pw_tokenizer/tokenize_to_global_handler_with_payload.h" |
| 32 | |
| 33 | namespace pw::system { |
| 34 | namespace { |
| 35 | |
| 36 | // Buffer used to encode each log entry before saving into log buffer. |
| 37 | sync::InterruptSpinLock log_encode_lock; |
| 38 | std::array<std::byte, PW_SYSTEM_MAX_LOG_ENTRY_SIZE> log_encode_buffer |
| 39 | PW_GUARDED_BY(log_encode_lock); |
| 40 | |
| 41 | // String-only logs may need to be formatted first. This buffer is required |
| 42 | // so the format string may be passed to the proto log encode. |
| 43 | std::array<std::byte, PW_SYSTEM_MAX_LOG_ENTRY_SIZE> log_format_buffer |
| 44 | PW_GUARDED_BY(log_encode_lock); |
| 45 | |
| 46 | const int64_t boot_time_count = |
| 47 | pw::chrono::SystemClock::now().time_since_epoch().count(); |
| 48 | |
| 49 | } // namespace |
| 50 | |
| 51 | // Provides time since boot in units defined by the target's pw_chrono backend. |
| 52 | int64_t GetTimestamp() { |
| 53 | return pw::chrono::SystemClock::now().time_since_epoch().count() - |
| 54 | boot_time_count; |
| 55 | } |
| 56 | |
| 57 | // Implementation for tokenized log handling. This will be optimized out for |
| 58 | // devices that only use string logging. |
| 59 | extern "C" void pw_tokenizer_HandleEncodedMessageWithPayload( |
| 60 | pw_tokenizer_Payload payload, const uint8_t message[], size_t size_bytes) { |
| 61 | log_tokenized::Metadata metadata = payload; |
| 62 | const int64_t timestamp = GetTimestamp(); |
| 63 | |
| 64 | std::lock_guard lock(log_encode_lock); |
| 65 | Result<ConstByteSpan> encoded_log_result = log::EncodeTokenizedLog( |
| 66 | metadata, message, size_bytes, timestamp, log_encode_buffer); |
| 67 | if (!encoded_log_result.ok()) { |
| 68 | GetMultiSink().HandleDropped(); |
| 69 | return; |
| 70 | } |
| 71 | GetMultiSink().HandleEntry(encoded_log_result.value()); |
| 72 | } |
| 73 | |
| 74 | // Implementation for string log handling. This will be optimized out for |
| 75 | // devices that only use tokenized logging. |
| 76 | extern "C" void pw_log_string_HandleMessageVaList(int level, |
| 77 | unsigned int flags, |
| 78 | const char* module_name, |
| 79 | const char* file_name, |
| 80 | int line_number, |
| 81 | const char* message, |
| 82 | va_list args) { |
| 83 | const int64_t timestamp = GetTimestamp(); |
| 84 | |
| 85 | std::lock_guard lock(log_encode_lock); |
| 86 | StringBuilder message_builder(log_format_buffer); |
| 87 | message_builder.FormatVaList(message, args); |
| 88 | |
| 89 | Result<ConstByteSpan> encoded_log_result = |
| 90 | log::EncodeLog(level, |
| 91 | flags, |
| 92 | module_name, |
Carlos Chinchilla | 72285b2 | 2022-02-25 18:00:03 -0800 | [diff] [blame] | 93 | /*thread_name=*/{}, |
Ewout van Bekkum | 5ddc01d | 2022-03-08 12:50:25 -0800 | [diff] [blame] | 94 | file_name, |
| 95 | line_number, |
| 96 | timestamp, |
| 97 | message_builder.view(), |
| 98 | log_encode_buffer); |
| 99 | if (!encoded_log_result.ok()) { |
| 100 | GetMultiSink().HandleDropped(); |
| 101 | return; |
| 102 | } |
| 103 | GetMultiSink().HandleEntry(encoded_log_result.value()); |
| 104 | } |
| 105 | |
| 106 | } // namespace pw::system |