d964e6151f652df7e667b4300089670882598886
[matches/MCTX3420.git] / server / log.c
1 /**
2  * @file log.c
3  * @brief 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 //TODO: Migrate to syslog (shouldn't be too hard; these functions basically do what syslog does)
21 //              Note that we will want to have a seperate log as well as syslog; give the user the option to view the log using the GUI
22
23 /**
24  * Print a message to stderr
25  * @param level - Specify how severe the message is.
26         If level is higher (less urgent) than the program's verbosity (see options.h) no message will be printed
27  * @param funct - String indicating the function name from which this function was called.
28         If this is NULL, Log will show the unspecified_funct string instead
29  * @param fmt - A format string
30  * @param ... - Arguments to be printed according to the format string
31  */
32 void LogEx(int level, const char * funct, ...)
33 {
34         const char *fmt;
35         va_list va;
36         va_start(va, funct);
37         fmt = va_arg(va, const char*);
38         
39         if (fmt == NULL) // sanity check
40                 FatalEx("Log", "Format string is NULL");
41
42         // Don't print the message unless we need to
43         if (level > g_options.verbosity)
44                 return;
45
46         if (funct == NULL)
47                 funct = unspecified_funct;
48
49         // Make a human readable severity string
50         const char *severity;
51         switch (level)
52         {
53                 case LOGERR:
54                         severity = "ERROR";
55                         break;
56                 case LOGWARN:
57                         severity = "WARNING";
58                         break;
59                 case LOGNOTE:
60                         severity = "NOTICE";
61                         break;
62                 case LOGINFO:
63                         severity = "INFO";
64                         break;
65                 default:
66                         severity = "DEBUG";
67                         break;
68         }
69
70         // Print: Program name, PID, severity string, function name first
71         fprintf(stderr, "%s [%d] : %s : %s - ", g_options.program, getpid(), severity, funct);
72
73         // Then pass additional arguments with the format string to vfprintf for printing
74         vfprintf(stderr, fmt, va);
75         va_end(va);
76
77         // End log messages with a newline
78         fprintf(stderr, "\n");
79 }
80
81 /**
82  * Handle a Fatal error in the program by printing a message and exiting the program
83  *      CALLING THIS FUNCTION WILL CAUSE THE PROGAM TO EXIT
84  * @param funct - Name of the calling function
85  * @param fmt - A format string
86  * @param ... - Arguments to be printed according to the format string
87  */
88 void FatalEx(const char * funct, ...)
89 {
90         const char *fmt;
91         va_list va;
92         va_start(va, funct);
93         fmt = va_arg(va, const char*);
94         
95         if (fmt == NULL)
96         {
97                 // Fatal error in the Fatal function.
98                 // (This really shouldn't happen unless someone does something insanely stupid)
99                 FatalEx("Fatal", "Format string is NULL");
100                 return; // Should never get here
101         }
102
103         if (funct == NULL)
104                 funct = unspecified_funct;
105
106         fprintf(stderr, "%s [%d] : %s : FATAL - ", g_options.program, getpid(), funct);
107
108         vfprintf(stderr, fmt, va);
109         va_end(va);
110         fprintf(stderr, "\n");
111
112         exit(EXIT_FAILURE);
113 }
114
115

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