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