debug: move warning code into warn header

This commit is contained in:
Danny Robson 2021-04-12 15:53:13 +10:00
parent 56ba3cb325
commit 2c7eb400c3
9 changed files with 137 additions and 110 deletions

View File

@ -315,6 +315,8 @@ list (
debug/system.hpp
debug/validate.cpp
debug/validate.hpp
debug/warn.cpp
debug/warn.hpp
encode/number.hpp
encode/base.cpp
encode/base.hpp

View File

@ -8,6 +8,7 @@
#include "chunked.hpp"
#include "../debug/warn.hpp"
#include "../log.hpp"
#include "../maths.hpp"
#include "../pointer.hpp"

View File

@ -37,29 +37,4 @@ void
cruft::debug::detail::unreachable (const char *msg)
{
panic (msg);
}
////////////////////////////////////////////////////////////////////////////////
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);
}
}

View File

@ -65,67 +65,6 @@ constexpr bool debug_enabled = false;
}
#define WARN(C) do { \
DEBUG_ONLY ( \
if (C) { \
std::cerr << __FILE__ \
<< ":" << __func__ \
<< ":" << __LINE__ \
<< ", " << #C \
<< '\n'; \
} \
); \
} while (0)
#define RETURN_FALSE_UNLESS(CONDITION) { \
if (const auto &__return_false_unless = (CONDITION); !__return_false_unless) { \
if constexpr (debug_enabled) { \
std::cerr << __FILE__ << ':' \
<< __LINE__ << ':' \
<< __PRETTY_FUNCTION__ << "; " \
<< #CONDITION << '\n'; \
breakpoint (); \
} \
\
return false; \
} \
} while (0)
#define WARN_RETURN(CONDITION,VALUE) do { \
if (const auto& __warn_return = (CONDITION); !!__warn_return) { \
if constexpr (debug_enabled) { \
std::cerr << __FILE__ << ':' \
<< __LINE__ << ':' \
<< __PRETTY_FUNCTION__ << "; " \
<< #CONDITION << '\n'; \
breakpoint (); \
} \
\
return (VALUE); \
} \
} while (0)
#define WARN_AND_RETURN_IF(COND, VALUE) WARN_RETURN((COND), (VALUE))
#define RETURN_UNLESS(VALUE,CONDITION) do { \
if (const auto &__return_unless = (CONDITION); !__return_unless) { \
if constexpr (debug_enabled) { \
std::cerr << __FILE__ << ':' \
<< __LINE__ << ':' \
<< __PRETTY_FUNCTION__ << "; " \
<< #CONDITION << '\n'; \
breakpoint (); \
} \
\
return (VALUE); \
} \
} while (0)
///////////////////////////////////////////////////////////////////////////////
#ifdef COMPILER_GCC
#define CHECK(C) do { \
@ -405,24 +344,3 @@ constexpr bool debug_enabled = false;
} \
); \
} while (0)
///////////////////////////////////////////////////////////////////////////////
#include <string>
#include <string_view>
void warn (void);
void warn (std::string const&);
void warn (std::string_view);
void warn (const char *);
template <typename ValueT>
decltype(auto)
warn_return (
char const *message,
ValueT &&value
) {
warn (message);
return std::forward<ValueT> (value);
}

36
debug/warn.cpp Normal file
View File

@ -0,0 +1,36 @@
/*
* 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/.
*
* Copyright 2010-2021 Danny Robson <danny@nerdcruft.net>
*/
#include "./warn.hpp"
#include "../log.hpp"
////////////////////////////////////////////////////////////////////////////////
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);
}

94
debug/warn.hpp Normal file
View File

@ -0,0 +1,94 @@
/*
* 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/.
*
* Copyright 2010-2021 Danny Robson <danny@nerdcruft.net>
*/
#pragma once
#include "./assert.hpp"
#include <iostream>
#define WARN(C) do { \
DEBUG_ONLY ( \
if (C) { \
std::cerr << __FILE__ \
<< ":" << __func__ \
<< ":" << __LINE__ \
<< ", " << #C \
<< '\n'; \
} \
); \
} while (0)
#define RETURN_FALSE_UNLESS(CONDITION) { \
if (const auto &__return_false_unless = (CONDITION); !__return_false_unless) { \
if constexpr (debug_enabled) { \
std::cerr << __FILE__ << ':' \
<< __LINE__ << ':' \
<< __PRETTY_FUNCTION__ << "; " \
<< #CONDITION << '\n'; \
breakpoint (); \
} \
\
return false; \
} \
} while (0)
#define WARN_RETURN(CONDITION,VALUE) do { \
if (const auto& __warn_return = (CONDITION); !!__warn_return) { \
if constexpr (debug_enabled) { \
std::cerr << __FILE__ << ':' \
<< __LINE__ << ':' \
<< __PRETTY_FUNCTION__ << "; " \
<< #CONDITION << '\n'; \
breakpoint (); \
} \
\
return (VALUE); \
} \
} while (0)
#define WARN_AND_RETURN_IF(COND, VALUE) WARN_RETURN((COND), (VALUE))
#define RETURN_UNLESS(VALUE,CONDITION) do { \
if (const auto &__return_unless = (CONDITION); !__return_unless) { \
if constexpr (debug_enabled) { \
std::cerr << __FILE__ << ':' \
<< __LINE__ << ':' \
<< __PRETTY_FUNCTION__ << "; " \
<< #CONDITION << '\n'; \
breakpoint (); \
} \
\
return (VALUE); \
} \
} while (0)
///////////////////////////////////////////////////////////////////////////////
#include <string>
#include <string_view>
void warn (void);
void warn (std::string const&);
void warn (std::string_view);
void warn (const char *);
template <typename ValueT>
decltype(auto)
warn_return (
char const *message,
ValueT &&value
) {
warn (message);
return std::forward<ValueT> (value);
}

View File

@ -12,7 +12,7 @@
#include "../packet.hpp"
#include "../../term.hpp"
#include "../../debug/assert.hpp"
#include "../../debug/warn.hpp"
#include "../../cast.hpp"
#include <iomanip>

View File

@ -11,6 +11,7 @@
#include "../level.hpp"
#include "../packet.hpp"
#include "../../paths.hpp"
#include "../../debug/warn.hpp"
using cruft::log::sink::path;

View File

@ -8,7 +8,7 @@
#include "description.hpp"
#include "../debug/assert.hpp"
#include "../debug/warn.hpp"
#include "../maths.hpp"