3 * @brief Implement logging and error handling functions
7 // --- Custom headers --- //
19 static void InitSyslog()
21 static bool init = false;
24 openlog(Options::program_name, LOG_PERROR | LOG_PID, LOG_USER);
31 static const char * unspecified_funct = "???";
36 * Print a message to stderr and log it via syslog. The message must be
37 * less than BUFSIZ characters long, or it will be truncated.
38 * @param level - Specify how severe the message is.
39 If level is higher (less urgent) than the program's verbosity (see options.h) no message will be printed
40 * @param funct - String indicating the function name from which this function was called.
41 If this is NULL, Log will show the unspecified_funct string instead
42 * @param file - Source file containing the function
43 * @param line - Line in the source file at which Log is called
44 * @param fmt - A format string
45 * @param ... - Arguments to be printed according to the format string
47 void LogEx(int level, const char * funct, const char * file, int line, ...)
49 //Todo: consider setlogmask(3) to filter messages
54 // Don't print the message unless we need to
55 //if (level > Globals::Verbosity())
59 fmt = va_arg(va, const char*);
61 if (fmt == NULL) // sanity check
62 Fatal("Format string is NULL");
64 vsnprintf(buffer, BUFSIZ, fmt, va);
68 funct = unspecified_funct;
70 // Make a human readable severity string
93 syslog(level, "%s: %s (%s:%d) - %s", severity, funct, file, line, buffer);
95 //fprintf(stderr, "%s[%d]: %s: %s (%s:%d) - %s\n", Options::program_name, getpid(), severity, funct, file, line, buffer);
96 fprintf(stderr, "%s: %s (%s:%d) - %s\n", severity, funct, file, line, buffer);
102 * Handle a Fatal error in the program by printing a message and exiting the program
103 * CALLING THIS FUNCTION WILL CAUSE THE PROGAM TO EXIT
104 * @param funct - Name of the calling function
105 * @param file - Name of the source file containing the calling function
106 * @param line - Line in the source file at which Fatal is called
107 * @param fmt - A format string
108 * @param ... - Arguments to be printed according to the format string
110 void FatalEx(const char * funct, const char * file, int line, ...)
116 fmt = va_arg(va, const char*);
120 // Fatal error in the Fatal function.
121 // (This really shouldn't happen unless someone does something insanely stupid)
122 Fatal("Format string is NULL");
123 return; // Should never get here
126 vsnprintf(buffer, BUFSIZ, fmt,va);
130 funct = unspecified_funct;
134 syslog(LOG_CRIT, "FATAL: %s (%s:%d) - %s", funct, file, line, buffer);
136 //fprintf(stderr, "%s[%d]: FATAL: %s (%s:%d) - %s\n", Options::program_name, getpid(), funct, file, line, buffer);
137 fprintf(stderr, "FATAL: %s (%s:%d) - %s\n", funct, file, line, buffer);
148 void Backtrace(int size)
152 int actual_size = backtrace(buffer, size);
153 backtrace_symbols_fd(buffer, actual_size, fileno(stderr));
155 Error("Backtrace not supported by compiler");