third_party.pigweed.src/pw_assert_basic/assert_basic.cc
Keir Mierle 3cee879769 pw_assert: Initial assert module
This starts the assert and assert_basic modules, which are the beginning
of our assert foundation. Much more is needed; in particular the "tests"
don't really do much other than check for compilation.

Screenshot
==========

   ▄████▄      ██▀███      ▄▄▄           ██████     ██░ ██
  ▒██▀ ▀█     ▓██ ▒ ██▒   ▒████▄       ▒██    ▒    ▓██░ ██▒
  ▒▓█ 💥 ▄    ▓██ ░▄█ ▒   ▒██  ▀█▄     ░ ▓██▄      ▒██▀▀██░
  ▒▓▓▄ ▄██▒   ▒██▀▀█▄     ░██▄▄▄▄██      ▒   ██▒   ░▓█ ░██
  ▒ ▓███▀ ░   ░██▓ ▒██▒    ▓█   ▓██▒   ▒██████▒▒   ░▓█▒░██▓
  ░ ░▒ ▒  ░   ░ ▒▓ ░▒▓░    ▒▒   ▓▒█░   ▒ ▒▓▒ ▒ ░    ▒ ░░▒░▒
    ░  ▒        ░▒ ░ ▒░     ▒   ▒▒ ░   ░ ░▒  ░ ░    ▒ ░▒░ ░
  ░             ░░   ░      ░   ▒      ░  ░  ░      ░  ░░ ░
  ░ ░            ░              ░  ░         ░      ░  ░  ░
  ░

  Welp, that didn't go as planned. It seems we crashed. Terribly sorry!

  CRASH MESSAGE

     Check failed: x (=50) > y (=51300): You SHOULD see this message

  CRASH FILE & LINE

     ../../pw_assert/assert_test.cc:46

  CRASH FUNCTION

     PigweedTestBody

==========

Change-Id: I9dbb460b3b33040d8009749f44b9fc174e2c8138
2020-02-07 23:18:07 +00:00

149 lines
5.0 KiB
C++

// Copyright 2020 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
// This is a very basic direct output log implementation with no buffering.
//#define PW_LOG_MODULE_NAME "ASRT"
//#include "pw_log/log.h"
#include "pw_assert_basic/assert_basic.h"
#include <cstring>
#include "pw_dumb_io/dumb_io.h"
#include "pw_preprocessor/util.h"
#include "pw_string/string_builder.h"
// TODO(pwbug/17): Expose these through the config system.
#define PW_ASSERT_BASIC_SHOW_BANNER 1
#define PW_ASSERT_BASIC_USE_COLORS 1
// ANSI color constants to control the terminal. Not Windows compatible.
// clang-format off
#if PW_ASSERT_BASIC_USE_COLORS
#define MAGENTA "\033[35m"
#define YELLOW "\033[33m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define BLUE "\033[96m"
#define BLACK "\033[30m"
#define YELLOW_BG "\033[43m"
#define WHITE_BG "\033[47m"
#define RED_BG "\033[41m"
#define BOLD "\033[1m"
#define RESET "\033[0m"
#else
#define MAGENTA ""
#define YELLOW ""
#define RED ""
#define GREEN ""
#define BLUE ""
#define BLACK ""
#define YELLOW_BG ""
#define WHITE_BG ""
#define RED_BG ""
#define BOLD ""
#define RESET ""
#endif // PW_ASSERT_BASIC_USE_COLORS
// clang-format on
static const char* kCrashBanner[] = {
" ",
" ▄████▄ ██▀███ ▄▄▄ ██████ ██░ ██ ",
" ▒██▀ ▀█ ▓██ ▒ ██▒ ▒████▄ ▒██ ▒ ▓██░ ██▒ ",
" ▒▓█ 💥 ▄ ▓██ ░▄█ ▒ ▒██ ▀█▄ ░ ▓██▄ ▒██▀▀██░ ",
" ▒▓▓▄ ▄██▒ ▒██▀▀█▄ ░██▄▄▄▄██ ▒ ██▒ ░▓█ ░██ ",
" ▒ ▓███▀ ░ ░██▓ ▒██▒ ▓█ ▓██▒ ▒██████▒▒ ░▓█▒░██▓ ",
" ░ ░▒ ▒ ░ ░ ▒▓ ░▒▓░ ▒▒ ▓▒█░ ▒ ▒▓▒ ▒ ░ ▒ ░░▒░▒ ",
" ░ ▒ ░▒ ░ ▒░ ▒ ▒▒ ░ ░ ░▒ ░ ░ ▒ ░▒░ ░ ",
" ░ ░░ ░ ░ ▒ ░ ░ ░ ░ ░░ ░ ",
" ░ ░ ░ ░ ░ ░ ░ ░ ░ ",
"",
" ",
};
using pw::dumb_io::WriteLine;
typedef pw::StringBuffer<150> Buffer;
extern "C" void pw_Crash(const char* file_name,
int line_number,
const char* function_name,
const char* message,
...) {
// As a matter of usability, crashes should be visible; make it so.
#if PW_ASSERT_BASIC_SHOW_BANNER
WriteLine(RED);
for (const char* line : kCrashBanner) {
WriteLine(line);
}
WriteLine(RESET);
#endif // PW_ASSERT_BASIC_SHOW_BANNER
WriteLine(
" Welp, that didn't go as planned. "
"It seems we crashed. Terribly sorry!");
WriteLine("");
WriteLine(YELLOW " CRASH MESSAGE" RESET);
WriteLine("");
{
Buffer buffer;
buffer << " ";
va_list args;
va_start(args, message);
buffer.FormatVaList(message, args);
va_end(args);
pw::dumb_io::WriteLine(buffer.view());
}
WriteLine("");
WriteLine(YELLOW " CRASH FILE & LINE" RESET);
WriteLine("");
{
Buffer buffer;
buffer.Format(" %s:%d", file_name, line_number);
WriteLine(buffer.view());
}
WriteLine("");
WriteLine(YELLOW " CRASH FUNCTION" RESET);
WriteLine("");
{
Buffer buffer;
buffer.Format(" %s", function_name);
WriteLine(buffer.view());
}
WriteLine("");
// TODO(pwbug/95): Perhaps surprisingly, this doesn't actually crash the
// device. At some point we'll have a reboot BSP function or similar, but for
// now this is acceptable since no one is using this basic backend.
if (!PW_ASSERT_BASIC_DISABLE_NORETURN) {
WriteLine(MAGENTA " HANG TIME" RESET);
WriteLine("");
WriteLine(
" ... until a debugger joins. System is waiting in a while(1)");
while (1) {
}
PW_UNREACHABLE;
} else {
WriteLine(MAGENTA " NOTE: YOU ARE IN ASSERT BASIC TEST MODE" RESET);
WriteLine("");
WriteLine(" This build returns from the crash handler for testing.");
WriteLine(" If you see this message in production, your build is ");
WriteLine(" incorrectly configured. Search for");
WriteLine(" PW_ASSERT_BASIC_DISABLE_NORETURN to fix it.");
WriteLine("");
}
}