Switch to syslog for logging messages.
[matches/MCTX3420.git] / server / log.c
1 /**
2  * @file log.c
3  * @brief Implement logging and error handling functions
4  */
5
6
7 // --- Custom headers --- //
8 #include "common.h"
9 #include "log.h"
10 #include "options.h"
11
12 #include <unistd.h>
13 #include <syslog.h>
14 #include <stdarg.h>
15
16 static const char * unspecified_funct = "???";
17
18 /**
19  * Print a message to stderr and log it via syslog. The message must be
20  * less than BUFSIZ characters long, or it will be truncated.
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 LogEx(int level, const char * funct, ...)
29 {
30         //Todo: consider setlogmask(3) to filter messages
31         const char *fmt;
32         char buffer[BUFSIZ];
33         va_list va;
34
35         // Don't print the message unless we need to
36         if (level > g_options.verbosity)
37                 return;
38
39         va_start(va, funct);
40         fmt = va_arg(va, const char*);
41         
42         if (fmt == NULL) // sanity check
43                 Fatal("Format string is NULL");
44
45         vsnprintf(buffer, BUFSIZ, fmt, va);
46         va_end(va);
47
48         if (funct == NULL)
49                 funct = unspecified_funct;
50
51         // Make a human readable severity string
52         const char *severity;
53         switch (level)
54         {
55                 case LOGERR:
56                         level = LOG_ERR;
57                         severity = "ERROR";
58                         break;
59                 case LOGWARN:
60                         level = LOG_WARNING;
61                         severity = "WARNING";
62                         break;
63                 case LOGNOTE:
64                         level = LOG_NOTICE;
65                         severity = "NOTICE";
66                         break;
67                 case LOGINFO:
68                         level = LOG_INFO;
69                         severity = "INFO";
70                         break;
71                 default:
72                         level = LOG_DEBUG;
73                         severity = "DEBUG";
74                         break;
75         }
76
77         syslog(level, "%s: %s - %s", severity, funct, buffer);
78 }
79
80 /**
81  * Handle a Fatal error in the program by printing a message and exiting the program
82  *      CALLING THIS FUNCTION WILL CAUSE THE PROGAM TO EXIT
83  * @param funct - Name of the calling function
84  * @param fmt - A format string
85  * @param ... - Arguments to be printed according to the format string
86  */
87 void FatalEx(const char * funct, ...)
88 {
89         const char *fmt;
90         char buffer[BUFSIZ];
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                 Fatal("Format string is NULL");
100                 return; // Should never get here
101         }
102
103         vsnprintf(buffer, BUFSIZ, fmt,va);
104         va_end(va);
105
106         if (funct == NULL)
107                 funct = unspecified_funct;
108
109         syslog(LOG_CRIT, "FATAL: %s - %s", funct, buffer);
110         exit(EXIT_FAILURE);
111 }
112
113

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