libcruft-util/debug.cpp

152 lines
3.6 KiB
C++
Raw Normal View History

2011-05-23 17:18:52 +10:00
/*
2018-08-04 15:14:06 +10:00
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
2011-05-23 17:18:52 +10:00
*
* Copyright 2010-2018 Danny Robson <danny@nerdcruft.net>
2011-05-23 17:18:52 +10:00
*/
#include "backtrace.hpp"
#include "debug.hpp"
2012-05-08 15:03:03 +10:00
#include "log.hpp"
#include "preprocessor.hpp"
2011-05-23 17:18:52 +10:00
#include <cstdlib>
#include <iostream>
using namespace cruft::debug;
2011-05-23 17:18:52 +10:00
////////////////////////////////////////////////////////////////////////////////
2011-05-23 17:18:52 +10:00
void
cruft::debug::detail::panic (const char *msg)
{
std::cerr << "PANIC: " << msg << "\n" << ::debug::backtrace () << std::endl;
breakpoint ();
2011-05-23 17:18:52 +10:00
abort ();
}
////////////////////////////////////////////////////////////////////////////////
2011-05-23 17:18:52 +10:00
void
cruft::debug::detail::not_implemented (const char *msg)
{
panic (msg);
2011-05-23 17:18:52 +10:00
}
//-----------------------------------------------------------------------------
2011-05-23 17:18:52 +10:00
void
cruft::debug::detail::unreachable (const char *msg)
{
panic (msg);
2011-05-23 17:18:52 +10:00
}
////////////////////////////////////////////////////////////////////////////////
void
warn (void)
{
warn ("Unusual code path found.");
}
//-----------------------------------------------------------------------------
void
warn (const std::string &msg)
{
warn (msg.c_str ());
}
//-----------------------------------------------------------------------------
void
warn (const char *msg)
{
LOG_WARN (msg);
}
////////////////////////////////////////////////////////////////////////////////
static void abort_with_info (void)
{
LOG_ALERT ("aborting: %!", debug::backtrace {});
}
2018-08-27 14:16:55 +10:00
//-----------------------------------------------------------------------------
2012-11-09 15:16:48 +11:00
void
cruft::debug::init [[gnu::constructor]] (void)
{
std::set_terminate (abort_with_info);
2012-11-09 15:16:48 +11:00
if (!debug_enabled && !getenv ("DEBUG"))
return;
LOG_DEBUG ("setting debug environment");
//enable_fpe ();
force_console ();
prepare_debugger ();
if (getenv ("DEBUG_WAIT"))
await_debugger ();
}
///////////////////////////////////////////////////////////////////////////////
static void
debug_wait [[gnu::constructor]] (void)
{
if (auto val = getenv ("DEBUG_WAIT")) {
LOG_NOTICE ("awaiting debugger");
if (std::string ("0") != val)
await_debugger ();
}
}
2018-04-19 20:32:30 +10:00
///////////////////////////////////////////////////////////////////////////////
template <>
bool
cruft::debug::validator<float>::is_valid (const float &val) noexcept
2018-04-19 20:32:30 +10:00
{
return !std::isnan (val);
}
//-----------------------------------------------------------------------------
template <>
bool
cruft::debug::validator<double>::is_valid (const double &val) noexcept
{
return !std::isnan (val);
}
///////////////////////////////////////////////////////////////////////////////
#include "std.hpp"
//-----------------------------------------------------------------------------
#define INSTANTIATE(KLASS) \
template <> \
struct cruft::debug::validator<KLASS> { \
static bool is_valid(KLASS const&); \
}; \
\
bool \
cruft::debug::validator<KLASS>::is_valid(KLASS const&) \
{ \
return true; \
}
//-----------------------------------------------------------------------------
MAP0(INSTANTIATE,
u08, u16, u32, u64,
i08, i16, i32, i64,
char
)