Get Doxygen working nicely
[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
9 // --- Custom headers --- //
10 #include "log.h"
11 #include "options.h"
12
13 // --- Static variables --- //
14 static char * unspecified_funct = (char*)"???";
15
16 // --- Function implementations --- //
17
18 /**
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
26  */
27 void Log(int level, char * funct, char * fmt, ...)
28 {
29         if (fmt == NULL) // sanity check
30                 Fatal("Log", "Format string is NULL");
31
32         // Don't print the message unless we need to
33         if (level > g_options.verbosity) 
34                 return;
35
36         if (funct == NULL)
37                 funct = unspecified_funct;
38
39         // Make a human readable severity string
40         char severity[BUFSIZ];
41         switch (level)
42         {
43                 case LOGERR:
44                         sprintf(severity, "ERROR");
45                         break;
46                 case LOGWARN:
47                         sprintf(severity, "WARNING");
48                         break;
49                 case LOGNOTE:
50                         sprintf(severity, "NOTICE");
51                         break;
52                 case LOGINFO:
53                         sprintf(severity, "INFO");
54                         break;
55                 default:
56                         sprintf(severity, "DEBUG");
57                         break;
58         }
59
60         // Print: Program name, PID, severity string, function name first
61         fprintf(stderr, "%s [%d] : %s : %s - ", g_options.program, getpid(), severity, funct);
62
63         // Then pass additional arguments with the format string to vfprintf for printing
64         va_list va;
65         va_start(va, fmt);
66         vfprintf(stderr, fmt, va);
67         va_end(va);
68
69         // End log messages with a newline
70         fprintf(stderr, "\n");
71 }
72
73 /**
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
79  */
80 void Fatal(char * funct, char * fmt, ...)
81 {
82         
83         if (fmt == NULL)
84         {
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
89         }
90
91         if (funct == NULL)
92                 funct = unspecified_funct;
93
94         fprintf(stderr, "%s [%d] : %s : FATAL - ", g_options.program, getpid(), funct);
95
96         va_list va;
97         va_start(va, fmt);
98         vfprintf(stderr, fmt, va);
99         va_end(va);
100         fprintf(stderr, "\n");
101
102         exit(EXIT_FAILURE);
103 }
104
105

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