3 * @purpose Implement logging and error handling functions
10 // --- Custom headers --- //
15 // --- Static variables --- //
16 static const char * unspecified_funct = "???";
18 // --- Function implementations --- //
21 * Print a message to stderr
22 * @param level - Specify how severe the message is.
23 If level is higher (less urgent) than the program's verbosity (see options.h) no message will be printed
24 * @param funct - String indicating the function name from which this function was called.
25 If this is NULL, Log will show the unspecified_funct string instead
26 * @param fmt - A format string
27 * @param ... - Arguments to be printed according to the format string
29 void LogEx(int level, const char * funct, ...)
34 fmt = va_arg(va, const char*);
36 if (fmt == NULL) // sanity check
37 FatalEx("Log", "Format string is NULL");
39 // Don't print the message unless we need to
40 if (level > g_options.verbosity)
44 funct = unspecified_funct;
46 // Make a human readable severity string
67 // Print: Program name, PID, severity string, function name first
68 fprintf(stderr, "%s [%d] : %s : %s - ", g_options.program, getpid(), severity, funct);
70 // Then pass additional arguments with the format string to vfprintf for printing
71 vfprintf(stderr, fmt, va);
74 // End log messages with a newline
75 fprintf(stderr, "\n");
79 * 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");