3 * @brief Implement logging and error handling functions
7 // --- Custom headers --- //
17 static void InitSyslog()
19 static bool init = false;
22 openlog(Options::program_name, LOG_PERROR | LOG_PID, LOG_USER);
29 static const char * unspecified_funct = "???";
34 * Print a message to stderr and log it via syslog. The message must be
35 * less than BUFSIZ characters long, or it will be truncated.
36 * @param level - Specify how severe the message is.
37 If level is higher (less urgent) than the program's verbosity (see options.h) no message will be printed
38 * @param funct - String indicating the function name from which this function was called.
39 If this is NULL, Log will show the unspecified_funct string instead
40 * @param file - Source file containing the function
41 * @param line - Line in the source file at which Log is called
42 * @param fmt - A format string
43 * @param ... - Arguments to be printed according to the format string
45 void LogEx(int level, const char * funct, const char * file, int line, ...)
47 //Todo: consider setlogmask(3) to filter messages
52 // Don't print the message unless we need to
53 //if (level > Globals::Verbosity())
57 fmt = va_arg(va, const char*);
59 if (fmt == NULL) // sanity check
60 Fatal("Format string is NULL");
62 vsnprintf(buffer, BUFSIZ, fmt, va);
66 funct = unspecified_funct;
68 // Make a human readable severity string
91 syslog(level, "%s: %s (%s:%d) - %s", severity, funct, file, line, buffer);
93 //fprintf(stderr, "%s[%d]: %s: %s (%s:%d) - %s\n", Options::program_name, getpid(), severity, funct, file, line, buffer);
94 fprintf(stderr, "%s: %s (%s:%d) - %s\n", severity, funct, file, line, buffer);
100 * Handle a Fatal error in the program by printing a message and exiting the program
101 * CALLING THIS FUNCTION WILL CAUSE THE PROGAM TO EXIT
102 * @param funct - Name of the calling function
103 * @param file - Name of the source file containing the calling function
104 * @param line - Line in the source file at which Fatal is called
105 * @param fmt - A format string
106 * @param ... - Arguments to be printed according to the format string
108 void FatalEx(const char * funct, const char * file, int line, ...)
114 fmt = va_arg(va, const char*);
118 // Fatal error in the Fatal function.
119 // (This really shouldn't happen unless someone does something insanely stupid)
120 Fatal("Format string is NULL");
121 return; // Should never get here
124 vsnprintf(buffer, BUFSIZ, fmt,va);
128 funct = unspecified_funct;
132 syslog(LOG_CRIT, "FATAL: %s (%s:%d) - %s", funct, file, line, buffer);
134 //fprintf(stderr, "%s[%d]: FATAL: %s (%s:%d) - %s\n", Options::program_name, getpid(), funct, file, line, buffer);
135 fprintf(stderr, "FATAL: %s (%s:%d) - %s\n", funct, file, line, buffer);
146 void Backtrace(int size)
149 int actual_size = backtrace(buffer, size);
150 backtrace_symbols_fd(buffer, actual_size, fileno(stderr));