3 * @purpose Implement logging and error handling functions
9 // --- Custom headers --- //
13 // --- Static variables --- //
14 static char * unspecified_funct = (char*)"???";
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 Log(int level, char * funct, char * fmt, ...)
29 if (fmt == NULL) // sanity check
30 Fatal("Log", "Format string is NULL");
32 // Don't print the message unless we need to
33 if (level > g_options.verbosity)
37 funct = unspecified_funct;
39 // Make a human readable severity string
40 char severity[BUFSIZ];
44 sprintf(severity, "ERROR");
47 sprintf(severity, "WARNING");
50 sprintf(severity, "NOTICE");
53 sprintf(severity, "INFO");
56 sprintf(severity, "DEBUG");
60 // Print: Program name, PID, severity string, function name first
61 fprintf(stderr, "%s [%d] : %s : %s - ", g_options.program, getpid(), severity, funct);
63 // Then pass additional arguments with the format string to vfprintf for printing
66 vfprintf(stderr, fmt, va);
69 // End log messages with a newline
70 fprintf(stderr, "\n");
74 * Handle a Fatal error in the program by printing a message and exiting the program
75 CALLING THIS FUNCTION WILL CAUSE THE PROGAM TO EXIT
76 * @param funct - Name of the calling function
77 * @param fmt - A format string
78 * @param ... - Arguments to be printed according to the format string
80 void Fatal(char * funct, char * fmt, ...)
85 // Fatal error in the Fatal function.
86 // (This really shouldn't happen unless someone does something insanely stupid)
87 Fatal("Fatal", "Format string is NULL");
88 return; // Should never get here
92 funct = unspecified_funct;
94 fprintf(stderr, "%s [%d] : %s : FATAL - ", g_options.program, getpid(), funct);
98 vfprintf(stderr, fmt, va);
100 fprintf(stderr, "\n");