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