Merge remote-tracking branch 'upstream/master'
[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 const char * unspecified_funct = "???";
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 LogEx(int level, const char * funct, ...)
28 {
29         const char *fmt;
30         va_list va;
31         va_start(va, funct);
32         fmt = va_arg(va, const char*);
33         
34         if (fmt == NULL) // sanity check
35                 FatalEx("Log", "Format string is NULL");
36
37         // Don't print the message unless we need to
38         if (level > g_options.verbosity)
39                 return;
40
41         if (funct == NULL)
42                 funct = unspecified_funct;
43
44         // Make a human readable severity string
45         const char *severity;
46         switch (level)
47         {
48                 case LOGERR:
49                         severity = "ERROR";
50                         break;
51                 case LOGWARN:
52                         severity = "WARNING";
53                         break;
54                 case LOGNOTE:
55                         severity = "NOTICE";
56                         break;
57                 case LOGINFO:
58                         severity = "INFO";
59                         break;
60                 default:
61                         severity = "DEBUG";
62                         break;
63         }
64
65         // Print: Program name, PID, severity string, function name first
66         fprintf(stderr, "%s [%d] : %s : %s - ", g_options.program, getpid(), severity, funct);
67
68         // Then pass additional arguments with the format string to vfprintf for printing
69         vfprintf(stderr, fmt, va);
70         va_end(va);
71
72         // End log messages with a newline
73         fprintf(stderr, "\n");
74 }
75
76 /**
77  * Handle a Fatal error in the program by printing a message and exiting the program
78  *      CALLING THIS FUNCTION WILL CAUSE THE PROGAM TO EXIT
79  * @param funct - Name of the calling function
80  * @param fmt - A format string
81  * @param ... - Arguments to be printed according to the format string
82  */
83 void FatalEx(const char * funct, ...)
84 {
85         const char *fmt;
86         va_list va;
87         va_start(va, funct);
88         fmt = va_arg(va, const char*);
89         
90         if (fmt == NULL)
91         {
92                 // Fatal error in the Fatal function.
93                 // (This really shouldn't happen unless someone does something insanely stupid)
94                 FatalEx("Fatal", "Format string is NULL");
95                 return; // Should never get here
96         }
97
98         if (funct == NULL)
99                 funct = unspecified_funct;
100
101         fprintf(stderr, "%s [%d] : %s : FATAL - ", g_options.program, getpid(), funct);
102
103         vfprintf(stderr, fmt, va);
104         va_end(va);
105         fprintf(stderr, "\n");
106
107         exit(EXIT_FAILURE);
108 }
109
110

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