3 * @brief Implement logging and error handling functions
7 // --- Custom headers --- //
16 static void InitSyslog()
18 static bool init = false;
21 openlog(Options::program_name, LOG_PERROR | LOG_PID, LOG_USER);
28 static const char * unspecified_funct = "???";
33 * Print a message to stderr and log it via syslog. The message must be
34 * less than BUFSIZ characters long, or it will be truncated.
35 * @param level - Specify how severe the message is.
36 If level is higher (less urgent) than the program's verbosity (see options.h) no message will be printed
37 * @param funct - String indicating the function name from which this function was called.
38 If this is NULL, Log will show the unspecified_funct string instead
39 * @param file - Source file containing the function
40 * @param line - Line in the source file at which Log is called
41 * @param fmt - A format string
42 * @param ... - Arguments to be printed according to the format string
44 void LogEx(int level, const char * funct, const char * file, int line, ...)
46 //Todo: consider setlogmask(3) to filter messages
51 // Don't print the message unless we need to
52 //if (level > Globals::Verbosity())
56 fmt = va_arg(va, const char*);
58 if (fmt == NULL) // sanity check
59 Fatal("Format string is NULL");
61 vsnprintf(buffer, BUFSIZ, fmt, va);
65 funct = unspecified_funct;
67 // Make a human readable severity string
90 syslog(level, "%s: %s (%s:%d) - %s", severity, funct, file, line, buffer);
92 //fprintf(stderr, "%s[%d]: %s: %s (%s:%d) - %s\n", Options::program_name, getpid(), severity, funct, file, line, buffer);
93 fprintf(stderr, "%s: %s (%s:%d) - %s\n", severity, funct, file, line, buffer);
99 * Handle a Fatal error in the program by printing a message and exiting the program
100 * CALLING THIS FUNCTION WILL CAUSE THE PROGAM TO EXIT
101 * @param funct - Name of the calling function
102 * @param file - Name of the source file containing the calling function
103 * @param line - Line in the source file at which Fatal is called
104 * @param fmt - A format string
105 * @param ... - Arguments to be printed according to the format string
107 void FatalEx(const char * funct, const char * file, int line, ...)
113 fmt = va_arg(va, const char*);
117 // Fatal error in the Fatal function.
118 // (This really shouldn't happen unless someone does something insanely stupid)
119 Fatal("Format string is NULL");
120 return; // Should never get here
123 vsnprintf(buffer, BUFSIZ, fmt,va);
127 funct = unspecified_funct;
131 syslog(LOG_CRIT, "FATAL: %s (%s:%d) - %s", funct, file, line, buffer);
133 //fprintf(stderr, "%s[%d]: FATAL: %s (%s:%d) - %s\n", Options::program_name, getpid(), funct, file, line, buffer);
134 fprintf(stderr, "FATAL: %s (%s:%d) - %s\n", funct, file, line, buffer);