mirror of
https://github.com/usatiuk/dhfs.git
synced 2025-10-28 12:37:48 +01:00
a little exception handling
This commit is contained in:
@@ -28,6 +28,7 @@ if (NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
add_link_options(-O3)
|
||||
endif ()
|
||||
|
||||
add_subdirectory(utils)
|
||||
add_subdirectory(libjvm_wrapper)
|
||||
add_subdirectory(backend)
|
||||
add_subdirectory(gui)
|
||||
|
||||
@@ -6,4 +6,4 @@ add_library(backend
|
||||
target_include_directories(backend PRIVATE include)
|
||||
target_include_directories(backend PUBLIC include_public)
|
||||
|
||||
target_link_libraries(backend PUBLIC libjvm_wrapper)
|
||||
target_link_libraries(backend PUBLIC libjvm_wrapper utils)
|
||||
|
||||
@@ -35,7 +35,7 @@ void DhfsInstance::start() {
|
||||
args.options = options.data();
|
||||
args.ignoreUnrecognized = false;
|
||||
|
||||
LibjvmWrapper::instance().JNI_CreateJavaVM(&_jvm, (void**) &_env, &args);
|
||||
LibjvmWrapper::instance().WJNI_CreateJavaVM(&_jvm, (void**) &_env, &args);
|
||||
}
|
||||
|
||||
void DhfsInstance::stop() {
|
||||
|
||||
@@ -11,4 +11,4 @@ add_executable(launcher
|
||||
)
|
||||
|
||||
target_link_libraries(launcher ${wxWidgets_LIBRARIES})
|
||||
target_link_libraries(launcher backend)
|
||||
target_link_libraries(launcher backend utils)
|
||||
@@ -31,3 +31,12 @@ bool LauncherApp::OnInit() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LauncherApp::OnExceptionInMainLoop() {
|
||||
try {
|
||||
std::rethrow_exception(std::current_exception());
|
||||
} catch (const std::exception& e) {
|
||||
wxMessageBox(e.what(), "Error", wxOK | wxICON_ERROR | wxCENTRE, GetTopWindow());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -9,7 +9,9 @@
|
||||
// containing a statusbar with the text "Hello World"
|
||||
class LauncherApp : public wxApp {
|
||||
public:
|
||||
virtual bool OnInit();
|
||||
virtual bool OnInit() override;
|
||||
|
||||
virtual bool OnExceptionInMainLoop() override;
|
||||
};
|
||||
|
||||
DECLARE_APP(LauncherApp)
|
||||
|
||||
@@ -7,4 +7,6 @@ target_include_directories(libjvm_wrapper PRIVATE include)
|
||||
target_include_directories(libjvm_wrapper PUBLIC include_public)
|
||||
|
||||
find_package(JNI REQUIRED)
|
||||
target_include_directories(libjvm_wrapper PUBLIC ${JNI_INCLUDE_DIRS})
|
||||
target_include_directories(libjvm_wrapper PUBLIC ${JNI_INCLUDE_DIRS})
|
||||
|
||||
target_link_libraries(libjvm_wrapper PUBLIC utils)
|
||||
@@ -4,14 +4,14 @@
|
||||
|
||||
#ifndef LIBJVMWRAPPER_HPP
|
||||
#define LIBJVMWRAPPER_HPP
|
||||
#include <jni.h>
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
class LibjvmWrapper {
|
||||
public:
|
||||
static LibjvmWrapper& instance();
|
||||
|
||||
decltype(JNI_CreateJavaVM)* JNI_CreateJavaVM;
|
||||
decltype(JNI_CreateJavaVM)* WJNI_CreateJavaVM;
|
||||
|
||||
private:
|
||||
LibjvmWrapper();
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
#include <dlfcn.h>
|
||||
#include <jni.h>
|
||||
|
||||
#include "Exception.h"
|
||||
|
||||
static constexpr auto LIBJVM_PATH =
|
||||
"/Library/Java/JavaVirtualMachines/zulu-21.jdk/Contents/Home/lib/server/libjvm.dylib";
|
||||
|
||||
@@ -16,8 +18,12 @@ LibjvmWrapper& LibjvmWrapper::instance() {
|
||||
|
||||
LibjvmWrapper::LibjvmWrapper() {
|
||||
_lib_handle = dlopen(LIBJVM_PATH, RTLD_NOW | RTLD_GLOBAL);
|
||||
JNI_CreateJavaVM = reinterpret_cast<decltype(JNI_CreateJavaVM)>(
|
||||
if (_lib_handle == nullptr)
|
||||
throw Exception(dlerror());
|
||||
WJNI_CreateJavaVM = reinterpret_cast<decltype(WJNI_CreateJavaVM)>(
|
||||
dlsym(_lib_handle, "JNI_CreateJavaVM"));
|
||||
if (WJNI_CreateJavaVM == nullptr)
|
||||
throw Exception(dlerror());
|
||||
}
|
||||
|
||||
LibjvmWrapper::~LibjvmWrapper() {
|
||||
|
||||
7
launcher/utils/CMakeLists.txt
Normal file
7
launcher/utils/CMakeLists.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
add_library(utils
|
||||
src/Exception.cpp
|
||||
include_public/Exception.h
|
||||
)
|
||||
|
||||
target_include_directories(utils PRIVATE include)
|
||||
target_include_directories(utils PUBLIC include_public)
|
||||
30
launcher/utils/include_public/Exception.h
Normal file
30
launcher/utils/include_public/Exception.h
Normal file
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 01.05.2023.
|
||||
//
|
||||
|
||||
#ifndef SEMBACKUP_EXCEPTION_H
|
||||
#define SEMBACKUP_EXCEPTION_H
|
||||
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
/// Custom exception class that uses execinfo to append a stacktrace to the exception message
|
||||
class Exception : public std::runtime_error {
|
||||
public:
|
||||
Exception(const std::string& text);
|
||||
Exception(const char* text);
|
||||
|
||||
private:
|
||||
/// Static function to get the current stacktrace
|
||||
static std::string getStacktrace();
|
||||
};
|
||||
|
||||
class ErrnoException : public Exception {
|
||||
public:
|
||||
ErrnoException(const std::string& text) : Exception(text + " " + std::strerror(errno)) {}
|
||||
ErrnoException(const char* text) : Exception(std::string(text) + " " + std::strerror(errno)) {}
|
||||
};
|
||||
|
||||
#endif // SEMBACKUP_EXCEPTION_H
|
||||
37
launcher/utils/src/Exception.cpp
Normal file
37
launcher/utils/src/Exception.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 01.05.2023.
|
||||
//
|
||||
|
||||
#include "Exception.h"
|
||||
|
||||
#include <execinfo.h>
|
||||
#include <sstream>
|
||||
|
||||
#include <openssl/err.h>
|
||||
|
||||
Exception::Exception(const std::string& text) : runtime_error(text + "\n" + getStacktrace()) {
|
||||
}
|
||||
|
||||
Exception::Exception(const char* text) : runtime_error(std::string(text) + "\n" + getStacktrace()) {
|
||||
}
|
||||
|
||||
// Based on: https://www.gnu.org/software/libc/manual/html_node/Backtraces.html
|
||||
std::string Exception::getStacktrace() {
|
||||
std::vector<void*> functions(50);
|
||||
char** strings;
|
||||
int n;
|
||||
|
||||
n = backtrace(functions.data(), 50);
|
||||
strings = backtrace_symbols(functions.data(), n);
|
||||
|
||||
std::stringstream out;
|
||||
|
||||
if (strings != nullptr) {
|
||||
out << "Stacktrace:" << std::endl;
|
||||
for (int i = 0; i < n; i++)
|
||||
out << strings[i] << std::endl;
|
||||
}
|
||||
|
||||
free(strings);
|
||||
return out.str();
|
||||
}
|
||||
Reference in New Issue
Block a user