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 <cstdint>
16 #include <cstring>
17 #include <memory>
18
19 #include "pw_assert/config.h"
20 #include "pw_assert_tokenized/handler.h"
21 #include "pw_base64/base64.h"
22 #include "pw_bytes/endian.h"
23 #include "pw_log/log.h"
24 #include "pw_log_tokenized/config.h"
25 #include "pw_log_tokenized/handler.h"
26 #include "pw_log_tokenized/log_tokenized.h"
27 #include "pw_log_tokenized/metadata.h"
28 #include "pw_span/span.h"
29
pw_assert_HandleFailure(void)30 extern "C" void pw_assert_HandleFailure(void) {
31 PW_HANDLE_LOG(PW_LOG_LEVEL_FATAL,
32 "pw_assert_tokenized",
33 PW_LOG_FLAGS,
34 #if PW_ASSERT_ENABLE_DEBUG
35 "PW_ASSERT() or PW_DASSERT() failure");
36 #else
37 "PW_ASSERT() failure. Note: PW_DASSERT disabled");
38 #endif // PW_ASSERT_ENABLE_DEBUG
39 PW_UNREACHABLE;
40 }
41
pw_assert_tokenized_HandleAssertFailure(uint32_t tokenized_file_name,int line_number)42 extern "C" void pw_assert_tokenized_HandleAssertFailure(
43 uint32_t tokenized_file_name, int line_number) {
44 // Buffer size for binary->base64 conversion with a null terminator.
45 constexpr size_t kBufferSize =
46 pw::base64::EncodedSize(sizeof(tokenized_file_name)) + 1;
47 std::byte* hash_buffer = reinterpret_cast<std::byte*>(&tokenized_file_name);
48 char base64_buffer[kBufferSize];
49
50 size_t len =
51 pw::base64::Encode(pw::span(hash_buffer, sizeof(tokenized_file_name)),
52 pw::span(base64_buffer));
53 base64_buffer[len] = '\0';
54 #if PW_ASSERT_ENABLE_DEBUG
55 PW_HANDLE_LOG(PW_LOG_LEVEL_FATAL,
56 "pw_assert_tokenized",
57 PW_LOG_FLAGS,
58 "PW_ASSERT() or PW_DASSERT() failure at $%s:%d",
59 base64_buffer,
60 line_number);
61 #else
62 PW_HANDLE_LOG(PW_LOG_LEVEL_FATAL,
63 "pw_assert_tokenized",
64 PW_LOG_FLAGS,
65 "PW_ASSERT() failure. Note: PW_DASSERT disabled $%s:%d",
66 base64_buffer,
67 line_number);
68 #endif // PW_ASSERT_ENABLE_DEBUG
69 PW_UNREACHABLE;
70 }
71
pw_assert_tokenized_HandleCheckFailure(uint32_t tokenized_message,int line_number)72 extern "C" void pw_assert_tokenized_HandleCheckFailure(
73 uint32_t tokenized_message, int line_number) {
74 // If line_number is too large to fit in the packed payload, the Metadata
75 // class will properly set it to 0, which is the expected value for line
76 // number values that would cause the bit field to overflow.
77 // See https://pigweed.dev/pw_log_tokenized/#c.PW_LOG_TOKENIZED_LINE_BITS for
78 // more info.
79 const uint32_t payload = pw::log_tokenized::Metadata(
80 PW_LOG_LEVEL_FATAL, 0, PW_LOG_FLAGS, line_number)
81 .value();
82 std::array<std::byte, sizeof(tokenized_message)> token_buffer =
83 pw::bytes::CopyInOrder(pw::endian::little, tokenized_message);
84
85 pw_log_tokenized_HandleLog(
86 payload,
87 reinterpret_cast<const uint8_t*>(token_buffer.data()),
88 token_buffer.size());
89 PW_UNREACHABLE;
90 }
91