Update FastCGI code and restructure includes a bit
[matches/MCTX3420.git] / server / log.c
1 /**
2  * @file log.c
3  * @purpose Implement logging and error handling functions
4  */
5
6
7 #include <unistd.h>
8 #include <stdarg.h>
9
10 // --- Custom headers --- //
11 #include "common.h"
12 #include "log.h"
13 #include "options.h"
14
15 // --- Static variables --- //
16 static const char * unspecified_funct = "???";
17
18 // --- Function implementations --- //
19
20 /**
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
28  */
29 void LogEx(int level, const char * funct, ...)
30 {
31         const char *fmt;
32         va_list va;
33         va_start(va, funct);
34         fmt = va_arg(va, const char*);
35         
36         if (fmt == NULL) // sanity check
37                 FatalEx("Log", "Format string is NULL");
38
39         // Don't print the message unless we need to
40         if (level > g_options.verbosity)
41                 return;
42
43         if (funct == NULL)
44                 funct = unspecified_funct;
45
46         // Make a human readable severity string
47         const char *severity;
48         switch (level)
49         {
50                 case LOGERR:
51                         severity = "ERROR";
52                         break;
53                 case LOGWARN:
54                         severity = "WARNING";
55                         break;
56                 case LOGNOTE:
57                         severity = "NOTICE";
58                         break;
59                 case LOGINFO:
60                         severity = "INFO";
61                         break;
62                 default:
63                         severity = "DEBUG";
64                         break;
65         }
66
67         // Print: Program name, PID, severity string, function name first
68         fprintf(stderr, "%s [%d] : %s : %s - ", g_options.program, getpid(), severity, funct);
69
70         // Then pass additional arguments with the format string to vfprintf for printing
71         vfprintf(stderr, fmt, va);
72         va_end(va);
73
74         // End log messages with a newline
75         fprintf(stderr, "\n");
76 }
77
78 /**
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
84  */
85 void FatalEx(const char * funct, ...)
86 {
87         const char *fmt;
88         va_list va;
89         va_start(va, funct);
90         fmt = va_arg(va, const char*);
91         
92         if (fmt == NULL)
93         {
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
98         }
99
100         if (funct == NULL)
101                 funct = unspecified_funct;
102
103         fprintf(stderr, "%s [%d] : %s : FATAL - ", g_options.program, getpid(), funct);
104
105         vfprintf(stderr, fmt, va);
106         va_end(va);
107         fprintf(stderr, "\n");
108
109         exit(EXIT_FAILURE);
110 }
111
112

UCC git Repository :: git.ucc.asn.au