Merge pull request #2599 from unxed/iterm_fix
[far2l.git] / utils / src / Panic.cpp
blob4cd7a20f47de87964f6729fc4f6c86c895276c63
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include <time.h>
5 #include "utils.h"
7 extern "C" void
8 __attribute__ ((visibility("default")))
9 __attribute__((weak))
10 FN_NORETURN
11 CustomPanic(const char *format, va_list args) noexcept
13 // default weak implementation
14 // to be overridden by WinPort implementation if process is hosting it
15 abort();
18 void FN_NORETURN Panic(const char *format, ...) noexcept
20 va_list args, args4log, args4cust;
21 va_start(args, format);
22 va_copy(args4log, args);
23 va_copy(args4cust, args);
24 vfprintf(stderr, format, args);
25 fflush(stderr);
26 va_end(args);
28 FILE *flog = fopen(InMyConfig("crash.log").c_str(), "a");
29 if (flog) {
30 time_t now = time(NULL);
31 struct tm t{};
32 localtime_r(&now, &t);
33 fprintf(flog, "[%u/%02u/%02u %02u:%02u] ",
34 t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min);
35 vfprintf(flog, format, args4log);
36 fputc('\n', flog);
37 fclose(flog);
39 va_end(args4log);
41 CustomPanic(format, args4cust);
42 // --- CustomPanic doesn't return --- va_end(args4cust);