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

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