LlamaLib  v2.0.2
Cross-platform library for local LLMs
Loading...
Searching...
No Matches
error_handling.h
1#pragma once
2
3#include <string>
4#include <mutex>
5#include <vector>
6#include <setjmp.h>
7#include <signal.h>
8#include <functional>
9#include <iostream>
10
11#ifdef _WIN32
12#include <windows.h>
13#define sigjmp_buf jmp_buf
14#define sigsetjmp(jb, savemask) setjmp(jb)
15#define siglongjmp longjmp
16#endif
17
18using Hook = std::function<void(int)>;
19
22{
23 int status_code = 0;
24 std::string status_message = "";
25};
26
29{
30public:
34 static void inject_error_state(ErrorState *state)
35 {
36 custom_error_state_ = state;
37 }
38
42 {
43 if (custom_error_state_)
44 return *custom_error_state_;
45
46 static ErrorState error_state;
47 return error_state;
48 }
49
50private:
51 static ErrorState *custom_error_state_;
52};
53
54int &get_status_code();
55std::string &get_status_message();
56sigjmp_buf &get_sigjmp_buf_point();
57std::mutex &get_sigint_hook_mutex();
58sigjmp_buf* get_current_jump_point_ptr();
59void set_current_jump_point(sigjmp_buf* jump_point);
60std::vector<Hook> &get_sigint_hooks();
61
62void fail(std::string message, int code = 1);
63void handle_exception(int code = -1);
64sigjmp_buf &get_jump_point();
65#ifdef _WIN32
66BOOL WINAPI console_ctrl_handler(DWORD ctrl_type);
67#else
68void handle_signal(int sig, siginfo_t *, void *);
69#endif
70void set_error_handlers(bool crash_handlers = true, bool sigint_handlers = true);
71
72void crash_signal_handler(int sig);
73void sigint_signal_handler(int sig);
74void register_sigint_hook(Hook hook);
Error state registry for managing shared error state.
static void inject_error_state(ErrorState *state)
Inject a custom error state instance.
static ErrorState & get_error_state()
Get the error state instance.
Error state container for sharing between libraries.