Run catching functions
This commit is contained in:
parent
e5f8a08238
commit
e1e10a92f6
4
main.cpp
4
main.cpp
@ -1,12 +1,15 @@
|
|||||||
import std;
|
import std;
|
||||||
|
import ru.landgrafhomyak.BGTU.networks_1.exceptions;
|
||||||
import ru.landgrafhomyak.BGTU.networks_1.threads;
|
import ru.landgrafhomyak.BGTU.networks_1.threads;
|
||||||
import ru.landgrafhomyak.BGTU.networks_1.sockets;
|
import ru.landgrafhomyak.BGTU.networks_1.sockets;
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
LdH::run_catching_main([] {
|
||||||
LdH::Sockets::init_sockets();
|
LdH::Sockets::init_sockets();
|
||||||
|
|
||||||
std::cout << LdH::Sockets::Address::parse("google.com", "443").to_string() << std::endl;
|
std::cout << LdH::Sockets::Address::parse("google.com", "443").to_string() << std::endl;
|
||||||
|
std::cout << LdH::Sockets::Address::parse("localhost123123", "443").to_string() << std::endl;
|
||||||
|
|
||||||
auto server = LdH::Sockets::listen_udp(LdH::Sockets::Address::parse("127.0.0.2", "8081"));
|
auto server = LdH::Sockets::listen_udp(LdH::Sockets::Address::parse("127.0.0.2", "8081"));
|
||||||
|
|
||||||
@ -30,4 +33,5 @@ int main() {
|
|||||||
client_thread.destroy();
|
client_thread.destroy();
|
||||||
|
|
||||||
LdH::Sockets::deinit_sockets();
|
LdH::Sockets::deinit_sockets();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,11 +9,14 @@ namespace LdH {
|
|||||||
const std::string message;
|
const std::string message;
|
||||||
const std::vector<std::stacktrace_entry> stacktrace;
|
const std::vector<std::stacktrace_entry> stacktrace;
|
||||||
|
|
||||||
explicit inline Exception(char const *message) : message{message}, stacktrace{Exception::generateStacktrace(-1)} {}
|
explicit inline Exception(char const *message) : message{message}, stacktrace{Exception::generateStacktrace(-1)} {
|
||||||
|
}
|
||||||
|
|
||||||
explicit inline Exception(std::string const &message) : message{message}, stacktrace{Exception::generateStacktrace(-1)} {}
|
explicit inline Exception(std::string const &message) : message{message}, stacktrace{Exception::generateStacktrace(-1)} {
|
||||||
|
}
|
||||||
|
|
||||||
explicit inline Exception(std::string const &&message) : message{message}, stacktrace{Exception::generateStacktrace(-1)} {}
|
explicit inline Exception(std::string const &&message) : message{message}, stacktrace{Exception::generateStacktrace(-1)} {
|
||||||
|
}
|
||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
static std::vector<std::stacktrace_entry> generateStacktrace(std::int_fast16_t hidden_frames_count);
|
static std::vector<std::stacktrace_entry> generateStacktrace(std::int_fast16_t hidden_frames_count);
|
||||||
@ -36,6 +39,29 @@ namespace LdH {
|
|||||||
std::cerr << "Fatal error:\n" << (Exception{msg}).formatOutput() << std::endl;
|
std::cerr << "Fatal error:\n" << (Exception{msg}).formatOutput() << std::endl;
|
||||||
std::abort();
|
std::abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export
|
||||||
|
template<class prefix_printer_t, class routine_t>
|
||||||
|
requires std::invocable<routine_t> && std::same_as<std::invoke_result_t<routine_t>, void>
|
||||||
|
&& std::invocable<prefix_printer_t> && std::same_as<std::invoke_result_t<prefix_printer_t>, void>
|
||||||
|
void run_catching(prefix_printer_t const &prefix_printer, routine_t const &routine) {
|
||||||
|
try {
|
||||||
|
routine();
|
||||||
|
} catch (LdH::Exception const &e) {
|
||||||
|
prefix_printer();
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (std::exception const &e) {
|
||||||
|
prefix_printer();
|
||||||
|
std::cerr << e.what() << "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export
|
||||||
|
template<class routine_t>
|
||||||
|
requires std::invocable<routine_t> && std::same_as<std::invoke_result_t<routine_t>, void>
|
||||||
|
void run_catching_main(routine_t const &routine) {
|
||||||
|
LdH::run_catching([] { std::cerr << "Uncaught exception in main thread:\n"; }, routine);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module : private;
|
module : private;
|
||||||
|
|||||||
@ -38,8 +38,21 @@ void LdH::throwFromWindowsErrCode(DWORD code) {
|
|||||||
0,
|
0,
|
||||||
nullptr
|
nullptr
|
||||||
);
|
);
|
||||||
|
size_t trim_size = std::strlen(msg);
|
||||||
|
while (trim_size > 0) {
|
||||||
|
switch (msg[--trim_size]) {
|
||||||
|
case '\n':
|
||||||
|
case '\r':
|
||||||
|
msg[trim_size] = '\0';
|
||||||
|
continue;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
std::string msgpp{msg};
|
std::string msgpp{msg};
|
||||||
LocalFree(msg);
|
LocalFree(msg);
|
||||||
|
|
||||||
throw LdH::Exception{std::move(msgpp)};
|
throw LdH::Exception{std::move(msgpp)};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -104,14 +104,7 @@ namespace LdH {
|
|||||||
thread_routine_t thread_routine = std::move(init->thread_routine);
|
thread_routine_t thread_routine = std::move(init->thread_routine);
|
||||||
init->notifier.notify();
|
init->notifier.notify();
|
||||||
|
|
||||||
try {
|
LdH::run_catching([&] { std::cerr << "Uncaught exception in thread" << thread_name << ":\n"; }, thread_routine);
|
||||||
thread_routine();
|
|
||||||
} catch (LdH::Exception const &e) {
|
|
||||||
std::cerr << "Uncaught exception in thread" << thread_name << ":\n";
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (std::exception const &e) {
|
|
||||||
std::cerr << "Uncaught exception in thread" << thread_name << ":\n" << e.what() << "\n";
|
|
||||||
}
|
|
||||||
return native_wrapper_t::kernel_ret();
|
return native_wrapper_t::kernel_ret();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user