From 9685ac3cb6fcfc9a8f7cf306c0a18089b4bacb5d Mon Sep 17 00:00:00 2001 From: Marko Budiselic Date: Sat, 21 Nov 2015 20:34:10 +0100 Subject: [PATCH] Initial code on crashes and exceptions logging. Related to T31. --- memgraph.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/memgraph.cpp b/memgraph.cpp index 2d4255777..036e96cb7 100644 --- a/memgraph.cpp +++ b/memgraph.cpp @@ -12,8 +12,41 @@ #include "threading/pool.hpp" #include "threading/task.hpp" +#include + +// TODO: move to separate header or source file +// TODO: log to local file or remote database +void stacktrace() noexcept +{ + void *array[50]; + int size = backtrace(array, 50); + std::cout << __FUNCTION__ << " backtrace returned " << size << " frames\n\n"; + char **messages = backtrace_symbols(array, size); + for (int i = 0; i < size && messages != NULL; ++i) { + std::cout << "[bt]: (" << i << ") " << messages[i] << std::endl; + } + std::cout << std::endl; + free(messages); +} + +// TODO: log to local file or remote database +void on_terminate() noexcept +{ + if (auto exc = std::current_exception()) { + try { + std::rethrow_exception(exc); + } catch (std::exception &ex) { + std::cout << ex.what() << std::endl << std::endl; + stacktrace(); + } + } + std::_Exit(EXIT_FAILURE); +} + int main() { + std::set_terminate(&on_terminate); + ioc::Container container; container.singleton();