29 #ifndef TOMOGRAPHER_TOOLS_SIGNAL_HANDLER 30 #define TOMOGRAPHER_TOOLS_SIGNAL_HANDLER 67 virtual void handleSignal(
int sig_num) = 0;
72 #ifndef TOMOGRAPHER_SIG_HANDLER_REPEAT_EXIT_DELAY 73 #define TOMOGRAPHER_SIG_HANDLER_REPEAT_EXIT_DELAY 2 77 namespace tomo_internal {
83 static inline void format_string_with_signal_num(
char * buffer,
const std::size_t buffer_max_len,
84 int signum,
const char * msgstart,
const char * msgend)
93 assert(signum < 100) ;
94 assert(buffer_max_len >= msgstartlen + 2 + msgendlen + 1) ;
98 strcpy(buffer, msgstart);
100 buffer[msgstartlen] = (48 + (signum/10)%10);
101 buffer[msgstartlen+1] = 48 + (signum%10);
102 buffer[msgstartlen+2] =
'\0';
104 strcat(buffer, msgend);
107 static inline void write_string_stderr_ignore_result(
const char * msg)
109 #pragma GCC diagnostic push 110 #pragma GCC diagnostic ignored "-Wunused-result" 112 write(STDERR_FILENO, msg, strlen(msg));
114 #pragma GCC diagnostic pop 117 static std::time_t last_sig_hit_time[NSIG] = { 0 };
125 static void signal_dispatch_fn(
int signum)
135 assert(signum >= 0 && signum < NSIG &&
"signum out of range 0...NSIG !") ;
137 #if defined(__MINGW32__) || defined(__MINGW64__) 140 signal(signum, tomo_internal::signal_dispatch_fn);
146 {
const char * msgint1 =
"\n*** interrupt (";
147 const char * msgint2 =
")\n";
149 format_string_with_signal_num(msgint,
sizeof(msgint), signum, msgint1, msgint2);
150 write_string_stderr_ignore_result(msgint);
158 if ( (now - tomo_internal::last_sig_hit_time[signum]) < TOMOGRAPHER_SIG_HANDLER_REPEAT_EXIT_DELAY ) {
162 const char * bufferexit =
"\n*** Exit\n";
163 write_string_stderr_ignore_result(bufferexit);
168 tomo_internal::last_sig_hit_time[signum] = now;
170 if (signal_handler[signum] != NULL) {
174 const char * msghandle1 =
"Warning: sig_handle: no signal handler set (got signal ";
175 const char * msghandle2 =
")\n";
176 char msghandle[1024];
177 format_string_with_signal_num(msghandle,
sizeof(msghandle), signum, msghandle1, msghandle2) ;
178 write_string_stderr_ignore_result(msghandle);
192 tomo_internal::signal_handler[signum] = sobj;
193 signal(signum, tomo_internal::signal_dispatch_fn);
Base namespace for the Tomographer project.