-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from zauguin/logging2
Error logging
- Loading branch information
Showing
4 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#include "errors.h" | ||
|
||
#include <sqlite3.h> | ||
|
||
#include <utility> | ||
#include <tuple> | ||
#include <type_traits> | ||
|
||
namespace sqlite { | ||
namespace detail { | ||
template<class> | ||
using void_t = void; | ||
template<class T, class = void> | ||
struct is_callable : std::false_type {}; | ||
template<class Functor, class ...Arguments> | ||
struct is_callable<Functor(Arguments...), void_t<decltype(std::declval<Functor>()(std::declval<Arguments>()...))>> : std::true_type {}; | ||
template<class Functor, class ...Functors> | ||
class FunctorOverload: public Functor, public FunctorOverload<Functors...> { | ||
public: | ||
template<class Functor1, class ...Remaining> | ||
FunctorOverload(Functor1 &&functor, Remaining &&... remaining): | ||
Functor(std::forward<Functor1>(functor)), | ||
FunctorOverload<Functors...>(std::forward<Remaining>(remaining)...) {} | ||
using Functor::operator(); | ||
using FunctorOverload<Functors...>::operator(); | ||
}; | ||
template<class Functor> | ||
class FunctorOverload<Functor>: public Functor { | ||
public: | ||
template<class Functor1> | ||
FunctorOverload(Functor1 &&functor): | ||
Functor(std::forward<Functor1>(functor)) {} | ||
using Functor::operator(); | ||
}; | ||
template<class Functor> | ||
class WrapIntoFunctor: public Functor { | ||
public: | ||
template<class Functor1> | ||
WrapIntoFunctor(Functor1 &&functor): | ||
Functor(std::forward<Functor1>(functor)) {} | ||
using Functor::operator(); | ||
}; | ||
template<class ReturnType, class ...Arguments> | ||
class WrapIntoFunctor<ReturnType(*)(Arguments...)> { | ||
ReturnType(*ptr)(Arguments...); | ||
public: | ||
WrapIntoFunctor(ReturnType(*ptr)(Arguments...)): ptr(ptr) {} | ||
ReturnType operator()(Arguments... arguments) { return (*ptr)(std::forward<Arguments>(arguments)...); } | ||
}; | ||
inline void store_error_log_data_pointer(std::shared_ptr<void> ptr) { | ||
static std::shared_ptr<void> stored; | ||
stored = std::move(ptr); | ||
} | ||
template<class T> | ||
std::shared_ptr<typename std::decay<T>::type> make_shared_inferred(T &&t) { | ||
return std::make_shared<typename std::decay<T>::type>(std::forward<T>(t)); | ||
} | ||
} | ||
template<class Handler> | ||
typename std::enable_if<!detail::is_callable<Handler(const sqlite_exception&)>::value>::type | ||
error_log(Handler &&handler); | ||
template<class Handler> | ||
typename std::enable_if<detail::is_callable<Handler(const sqlite_exception&)>::value>::type | ||
error_log(Handler &&handler); | ||
template<class ...Handler> | ||
typename std::enable_if<sizeof...(Handler)>=2>::type | ||
error_log(Handler &&...handler) { | ||
return error_log(detail::FunctorOverload<detail::WrapIntoFunctor<typename std::decay<Handler>::type>...>(std::forward<Handler>(handler)...)); | ||
} | ||
template<class Handler> | ||
typename std::enable_if<!detail::is_callable<Handler(const sqlite_exception&)>::value>::type | ||
error_log(Handler &&handler) { | ||
return error_log(std::forward<Handler>(handler), [](const sqlite_exception&) {}); | ||
} | ||
template<class Handler> | ||
typename std::enable_if<detail::is_callable<Handler(const sqlite_exception&)>::value>::type | ||
error_log(Handler &&handler) { | ||
auto ptr = detail::make_shared_inferred([handler = std::forward<Handler>(handler)](int error_code, const char *errstr) mutable { | ||
switch(error_code & 0xFF) { | ||
#define SQLITE_MODERN_CPP_ERROR_CODE(NAME,name,derived) \ | ||
case SQLITE_ ## NAME: switch(error_code) { \ | ||
derived \ | ||
default: handler(errors::name(errstr, "", error_code)); \ | ||
};break; | ||
#define SQLITE_MODERN_CPP_ERROR_CODE_EXTENDED(BASE,SUB,base,sub) \ | ||
case SQLITE_ ## BASE ## _ ## SUB: \ | ||
handler(errors::base ## _ ## sub(errstr, "", error_code)); \ | ||
break; | ||
#include "lists/error_codes.h" | ||
#undef SQLITE_MODERN_CPP_ERROR_CODE_EXTENDED | ||
#undef SQLITE_MODERN_CPP_ERROR_CODE | ||
default: handler(sqlite_exception(errstr, "", error_code)); \ | ||
} | ||
}); | ||
|
||
sqlite3_config(SQLITE_CONFIG_LOG, (void(*)(void*,int,const char*))[](void *functor, int error_code, const char *errstr) { | ||
(*static_cast<decltype(ptr.get())>(functor))(error_code, errstr); | ||
}, ptr.get()); | ||
detail::store_error_log_data_pointer(std::move(ptr)); | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include <iostream> | ||
#include <iomanip> | ||
#include <string> | ||
#include <memory> | ||
#include <stdexcept> | ||
#include <sqlite_modern_cpp.h> | ||
#include <sqlite_modern_cpp/log.h> | ||
using namespace sqlite; | ||
using namespace std; | ||
|
||
|
||
int main() { | ||
bool error_detected = false; | ||
error_log( | ||
[&](errors::constraint) { | ||
cerr << "Wrong error detected!" << endl; | ||
}, | ||
[&](errors::constraint_primarykey e) { | ||
cerr << e.get_code() << '/' << e.get_extended_code() << ": " << e.what() << endl; | ||
error_detected = true; | ||
} | ||
); | ||
database db(":memory:"); | ||
db << "CREATE TABLE person (id integer primary key not null, name TEXT);"; | ||
|
||
try { | ||
db << "INSERT INTO person (id,name) VALUES (?,?)" << 1 << "jack"; | ||
// inserting again to produce error | ||
db << "INSERT INTO person (id,name) VALUES (?,?)" << 1 << "jack"; | ||
} catch (errors::constraint& e) { | ||
} | ||
|
||
if(!error_detected) { | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
exit(EXIT_SUCCESS); | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include <iostream> | ||
#include <iomanip> | ||
#include <string> | ||
#include <memory> | ||
#include <stdexcept> | ||
#include <sqlite_modern_cpp.h> | ||
#include <sqlite_modern_cpp/log.h> | ||
using namespace sqlite; | ||
using namespace std; | ||
|
||
|
||
int main() { | ||
bool error_detected = false; | ||
error_log( | ||
[&](errors::constraint e) { | ||
cerr << e.get_code() << '/' << e.get_extended_code() << ": " << e.what() << endl; | ||
error_detected = true; | ||
} | ||
); | ||
database db(":memory:"); | ||
db << "CREATE TABLE person (id integer primary key not null, name TEXT);"; | ||
|
||
try { | ||
db << "INSERT INTO person (id,name) VALUES (?,?)" << 1 << "jack"; | ||
// inserting again to produce error | ||
db << "INSERT INTO person (id,name) VALUES (?,?)" << 1 << "jack"; | ||
} catch (errors::constraint& e) { | ||
} | ||
|
||
if(!error_detected) { | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
exit(EXIT_SUCCESS); | ||
} |