diff --git a/dbc.cpp b/dbc.cpp index 13bafde..4653617 100644 --- a/dbc.cpp +++ b/dbc.cpp @@ -1,44 +1,47 @@ #include "dbc.hpp" #include -void dbc::log(const string &message) { - std::cerr << "!!!!!!!!!!" << message << std::endl; +void dbc::log(const string &message, const std::source_location location) { + std::clog << '[' << location.file_name() << ':' + << location.line() << "|" + << location.function_name() << "] " + << message << std::endl; } -void dbc::sentinel(const string &message) { +void dbc::sentinel(const string &message, const std::source_location location) { string err = fmt::format("[SENTINEL!] {}", message); - dbc::log(err); + dbc::log(err, location); throw dbc::SentinelError{err}; } -void dbc::pre(const string &message, bool test) { +void dbc::pre(const string &message, bool test, const std::source_location location) { if(!test) { string err = fmt::format("[PRE!] {}", message); - dbc::log(err); + dbc::log(err, location); throw dbc::PreCondError{err}; } } -void dbc::pre(const string &message, std::function tester) { - dbc::pre(message, tester()); +void dbc::pre(const string &message, std::function tester, const std::source_location location) { + dbc::pre(message, tester(), location); } -void dbc::post(const string &message, bool test) { +void dbc::post(const string &message, bool test, const std::source_location location) { if(!test) { string err = fmt::format("[POST!] {}", message); - dbc::log(err); + dbc::log(err, location); throw dbc::PostCondError{err}; } } -void dbc::post(const string &message, std::function tester) { - dbc::post(message, tester()); +void dbc::post(const string &message, std::function tester, const std::source_location location) { + dbc::post(message, tester(), location); } -void dbc::check(bool test, const string &message) { +void dbc::check(bool test, const string &message, const std::source_location location) { if(!test) { string err = fmt::format("[CHECK!] {}\n", message); - dbc::log(err); + dbc::log(err, location); throw dbc::CheckError{err}; } } diff --git a/dbc.hpp b/dbc.hpp index bb55122..87e4fb0 100644 --- a/dbc.hpp +++ b/dbc.hpp @@ -3,6 +3,7 @@ #include #include #include +#include using std::string; @@ -19,11 +20,31 @@ namespace dbc { class PreCondError : public Error {}; class PostCondError : public Error {}; - void log(const string &message); - [[noreturn]] void sentinel(const string &message); - void pre(const string &message, bool test); - void pre(const string &message, std::function tester); - void post(const string &message, bool test); - void post(const string &message, std::function tester); - void check(bool test, const string &message); + void log(const string &message, + const std::source_location location = + std::source_location::current()); + + [[noreturn]] void sentinel(const string &message, + const std::source_location location = + std::source_location::current()); + + void pre(const string &message, bool test, + const std::source_location location = + std::source_location::current()); + + void pre(const string &message, std::function tester, + const std::source_location location = + std::source_location::current()); + + void post(const string &message, bool test, + const std::source_location location = + std::source_location::current()); + + void post(const string &message, std::function tester, + const std::source_location location = + std::source_location::current()); + + void check(bool test, const string &message, + const std::source_location location = + std::source_location::current()); } diff --git a/tests/dbc.cpp b/tests/dbc.cpp index 1d43905..5c7263d 100644 --- a/tests/dbc.cpp +++ b/tests/dbc.cpp @@ -3,12 +3,15 @@ using namespace dbc; -TEST_CASE("basic feature tests", "[utils]") { +TEST_CASE("basic feature tests", "[dbc]") { log("Logging a message."); pre("confirm positive cases work", 1 == 1); + pre("confirm positive lambda", [&]{ return 1 == 1;}); + post("confirm positive post", 1 == 1); + post("confirm postitive post with lamdba", [&]{ return 1 == 1;}); check(1 == 1, "one equals 1");