3 * @purpose Implement logging and error handling functions
9 // --- Custom headers --- //
13 // --- Static variables --- //
14 static const char * unspecified_funct = "???";
16 // --- Function implementations --- //
19 * Print a message to stderr
20 * @param level - Specify how severe the message is.
21 If level is higher (less urgent) than the program's verbosity (see options.h) no message will be printed
22 * @param funct - String indicating the function name from which this function was called.
23 If this is NULL, Log will show the unspecified_funct string instead
24 * @param fmt - A format string
25 * @param ... - Arguments to be printed according to the format string
27 void LogEx(int level, const char * funct, ...)
32 fmt = va_arg(va, const char*);
34 if (fmt == NULL) // sanity check
35 FatalEx("Log", "Format string is NULL");
37 // Don't print the message unless we need to
38 if (level > g_options.verbosity)
42 funct = unspecified_funct;
44 // Make a human readable severity string
65 // Print: Program name, PID, severity string, function name first
66 fprintf(stderr, "%s [%d] : %s : %s - ", g_options.program, getpid(), severity, funct);
68 // Then pass additional arguments with the format string to vfprintf for printing
69 vfprintf(stderr, fmt, va);
72 // End log messages with a newline
73 fprintf(stderr, "\n");
77 * Handle a Fatal error in the program by printing a message and exiting the program
78 * CALLING THIS FUNCTION WILL CAUSE THE PROGAM TO EXIT
79 * @param funct - Name of the calling function
80 * @param fmt - A format string
81 * @param ... - Arguments to be printed according to the format string
83 void FatalEx(const char * funct, ...)
88 fmt = va_arg(va, const char*);
92 // Fatal error in the Fatal function.
93 // (This really shouldn't happen unless someone does something insanely stupid)
94 FatalEx("Fatal", "Format string is NULL");
95 return; // Should never get here
99 funct = unspecified_funct;
101 fprintf(stderr, "%s [%d] : %s : FATAL - ", g_options.program, getpid(), funct);
103 vfprintf(stderr, fmt, va);
105 fprintf(stderr, "\n");