MCPcopy Create free account
hub / github.com/dobin/RedEdr / signal_handler

Function signal_handler

RedEdrShared/loguru.cpp:1964–2022  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1962 }
1963
1964 void signal_handler(int signal_number, siginfo_t*, void*)
1965 {
1966 const char* signal_name = "UNKNOWN SIGNAL";
1967
1968 if (signal_number == SIGABRT) { signal_name = "SIGABRT"; }
1969 if (signal_number == SIGBUS) { signal_name = "SIGBUS"; }
1970 if (signal_number == SIGFPE) { signal_name = "SIGFPE"; }
1971 if (signal_number == SIGILL) { signal_name = "SIGILL"; }
1972 if (signal_number == SIGINT) { signal_name = "SIGINT"; }
1973 if (signal_number == SIGSEGV) { signal_name = "SIGSEGV"; }
1974 if (signal_number == SIGTERM) { signal_name = "SIGTERM"; }
1975
1976 // --------------------------------------------------------------------
1977 /* There are few things that are safe to do in a signal handler,
1978 but writing to stderr is one of them.
1979 So we first print out what happened to stderr so we're sure that gets out,
1980 then we do the unsafe things, like logging the stack trace.
1981 */
1982
1983 if (g_colorlogtostderr && s_terminal_has_color) {
1984 write_to_stderr(terminal_reset());
1985 write_to_stderr(terminal_bold());
1986 write_to_stderr(terminal_light_red());
1987 }
1988 write_to_stderr("\n");
1989 write_to_stderr("Loguru caught a signal: ");
1990 write_to_stderr(signal_name);
1991 write_to_stderr("\n");
1992 if (g_colorlogtostderr && s_terminal_has_color) {
1993 write_to_stderr(terminal_reset());
1994 }
1995
1996 // --------------------------------------------------------------------
1997
1998 if (s_signal_options.unsafe_signal_handler) {
1999 // --------------------------------------------------------------------
2000 /* Now we do unsafe things. This can for example lead to deadlocks if
2001 the signal was triggered from the system's memory management functions
2002 and the code below tries to do allocations.
2003 */
2004
2005 flush();
2006 char preamble_buff[LOGURU_PREAMBLE_WIDTH];
2007 print_preamble(preamble_buff, sizeof(preamble_buff), Verbosity_FATAL, "", 0);
2008 auto message = Message{ Verbosity_FATAL, "", 0, preamble_buff, "", "Signal: ", signal_name };
2009 try {
2010 log_message(1, message, false, false);
2011 }
2012 catch (...) {
2013 // This can happed due to s_fatal_handler.
2014 write_to_stderr("Exception caught and ignored by Loguru signal handler.\n");
2015 }
2016 flush();
2017
2018 // --------------------------------------------------------------------
2019 }
2020
2021 call_default_signal_handler(signal_number);

Callers

nothing calls this directly

Calls 8

write_to_stderrFunction · 0.85
terminal_resetFunction · 0.85
terminal_boldFunction · 0.85
terminal_light_redFunction · 0.85
flushFunction · 0.85
print_preambleFunction · 0.85
log_messageFunction · 0.85

Tested by

no test coverage detected