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