• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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_assert_test/fake_backend.h"
16 
17 #include <cstring>
18 #include <span>
19 
20 #include "pw_string/string_builder.h"
21 
22 // Global that's publicly accessible to read captured assert contents.
23 struct pw_CapturedAssert pw_captured_assert;
24 
IsDirSeparator(char c)25 bool IsDirSeparator(char c) { return c == '/' || c == '\\'; }
26 
GetFileBasename(const char * filename)27 const char* GetFileBasename(const char* filename) {
28   int length = std::strlen(filename);
29   if (length == 0) {
30     return filename;
31   }
32 
33   // Start on the last character, find the parent directory.
34   const char* basename = filename + std::strlen(filename) - 1;
35   while (basename != filename && !IsDirSeparator(*basename)) {
36     basename--;
37   }
38   if (IsDirSeparator(*basename)) {
39     basename++;
40   }
41   return basename;
42 }
43 
pw_CaptureAssert(const char * file_name,int line_number,const char * function_name,const char * message,...)44 void pw_CaptureAssert(const char* file_name,
45                       int line_number,
46                       const char* function_name,
47                       const char* message,
48                       ...) {
49   // Triggered
50   pw_captured_assert.triggered = 1;
51 
52   // Filename
53   pw_captured_assert.file_name = file_name;
54 
55   // Line number
56   pw_captured_assert.line_number = line_number;
57 
58   // Function name
59   pw_captured_assert.function_name = function_name;
60 
61   // Message
62   pw::StringBuilder builder(pw_captured_assert.message);
63   va_list args;
64   va_start(args, message);
65   builder.FormatVaList(message, args);
66   va_end(args);
67 }
68