check.cpp raw

   1  // Copyright (c) 2022 The Limenka developers
   2  // Distributed under the MIT software license, see the accompanying
   3  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
   4  
   5  #include <util/check.h>
   6  
   7  #include <limenka-build-config.h> // IWYU pragma: keep
   8  
   9  #include <clientversion.h>
  10  #include <tinyformat.h>
  11  
  12  #include <cstdio>
  13  #include <cstdlib>
  14  #include <string>
  15  #include <string_view>
  16  
  17  std::string StrFormatInternalBug(std::string_view msg, std::string_view file, int line, std::string_view func)
  18  {
  19      return strprintf("Internal bug detected: %s\n%s:%d (%s)\n"
  20                       "%s %s\n"
  21                       "Please report this issue here: %s\n",
  22                       msg, file, line, func, CLIENT_NAME, FormatFullVersion(), CLIENT_BUGREPORT);
  23  }
  24  
  25  NonFatalCheckError::NonFatalCheckError(std::string_view msg, std::string_view file, int line, std::string_view func)
  26      : std::runtime_error{StrFormatInternalBug(msg, file, line, func)}
  27  {
  28  }
  29  
  30  bool g_detail_test_only_CheckFailuresAreExceptionsNotAborts{false};
  31  
  32  void assertion_fail(std::string_view file, int line, std::string_view func, std::string_view assertion)
  33  {
  34      if (g_detail_test_only_CheckFailuresAreExceptionsNotAborts) {
  35          throw NonFatalCheckError{assertion, file, line, func};
  36      }
  37      auto str = strprintf("%s:%s %s: Assertion `%s' failed.\n", file, line, func, assertion);
  38      fwrite(str.data(), 1, str.size(), stderr);
  39      std::abort();
  40  }
  41