-
Notifications
You must be signed in to change notification settings - Fork 2
Optimize and add ability to configure error printing function #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rayhamel
wants to merge
14
commits into
cjdb:main
Choose a base branch
from
rayhamel:ConfigurableErrorPrinting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a020752
Optimize and add ability to configure error printing function
rayhamel ba09c4b
Make parameters to print functions const, for consistency
rayhamel 732627c
Use operator<< instead of ostream::write for string_view
rayhamel c369831
'invert' and eliminate repeated code in the stdio and iostream print_…
rayhamel e006adb
Switch back to ostream::write
rayhamel 0d28933
Change error printing customization point to a variable (function poi…
rayhamel 92914b2
Add tests for iostream output
rayhamel 4264c17
Make clang-tidy happy with namespace closing comment
rayhamel 09d4baf
Make print_error an inline variable to silence clang-tidy warning
rayhamel 43b4f9a
Define a stub for cjdb::print_error even when CJDB_SKIP_STDIO is defi…
rayhamel 6977cc1
Update documentation
rayhamel d553310
Minor formatting change
rayhamel e2bc28d
Another formatting change
rayhamel 539b14e
Deleted asterisk, misc formatting
rayhamel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,17 @@ | |
#ifndef CJDB_CONTRACTS_HPP | ||
#define CJDB_CONTRACTS_HPP | ||
|
||
#include <cstdio> | ||
#include <string_view> | ||
#include <type_traits> | ||
|
||
#ifdef CJDB_USE_IOSTREAM | ||
#include <iostream> | ||
#elif !defined(CJDB_SKIP_STDIO) | ||
#include <cerrno> | ||
#include <cstdio> | ||
#include <system_error> | ||
#endif // CJDB_USE_IOSTREAM | ||
|
||
// clang-tidy doesn't yet support this | ||
// | ||
// #ifndef __cpp_lib_is_constant_evaluated | ||
|
@@ -24,7 +31,21 @@ | |
#define CJDB_PRETTY_FUNCTION __PRETTY_FUNCTION__ | ||
#endif // _MSC_VER | ||
|
||
namespace cjdb::contracts_detail { | ||
namespace cjdb { | ||
using print_error_fn = void(std::string_view); | ||
inline print_error_fn* print_error = [](std::string_view message) { | ||
#ifdef CJDB_USE_IOSTREAM | ||
std::cerr.write(message.data(), static_cast<std::streamsize>(message.size())); | ||
#elif !defined(CJDB_SKIP_STDIO) | ||
|
||
if (auto const len = message.size(); | ||
std::fwrite(message.data(), sizeof(char), len, stderr) < len) [[unlikely]] | ||
{ | ||
throw std::system_error{errno, std::system_category()}; | ||
} | ||
#endif // CJDB_USE_IOSTREAM | ||
}; | ||
|
||
namespace contracts_detail { | ||
#ifdef NDEBUG | ||
inline constexpr auto is_debug = false; | ||
#else | ||
|
@@ -34,12 +55,19 @@ namespace cjdb::contracts_detail { | |
struct contract_impl_fn { | ||
constexpr void operator()(bool const result, | ||
std::string_view const message, | ||
std::string_view const function) const noexcept | ||
std::string_view const function) const noexcept(!is_debug) | ||
{ | ||
if (not result) { | ||
if (not std::is_constant_evaluated()) { | ||
if constexpr (is_debug) { | ||
std::fprintf(stderr, "%s in `%s`\n", message.data(), function.data()); | ||
#ifdef _WIN32 | ||
constexpr auto& suffix = "`\r\n"; | ||
#else | ||
constexpr auto& suffix = "`\n"; | ||
#endif // _WIN32 | ||
::cjdb::print_error(message); | ||
::cjdb::print_error(function); | ||
::cjdb::print_error(suffix); | ||
} | ||
} | ||
#ifdef _MSC_VER | ||
|
@@ -65,11 +93,12 @@ namespace cjdb::contracts_detail { | |
} | ||
}; | ||
inline constexpr auto matches_bool = matches_bool_fn{}; | ||
} // namespace cjdb::contracts_detail | ||
} // namespace contracts_detail | ||
} // namespace cjdb | ||
|
||
#define CJDB_CONTRACT_IMPL(CJDB_KIND, ...) \ | ||
::cjdb::contracts_detail::contract_impl(::cjdb::contracts_detail::matches_bool(__VA_ARGS__), \ | ||
__FILE__ ":" CJDB_TO_STRING(__LINE__) ": " CJDB_KIND " `" #__VA_ARGS__ "` failed", \ | ||
__FILE__ ":" CJDB_TO_STRING(__LINE__) ": " CJDB_KIND " `" #__VA_ARGS__ "` failed in `", \ | ||
CJDB_PRETTY_FUNCTION) | ||
|
||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.