Merge branch 'fix_hardwarecode'
[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 file - Source file containing the function
26  * @param line - Line in the source file at which Log is called
27  * @param fmt - A format string
28  * @param ... - Arguments to be printed according to the format string
29  */
30 void LogEx(int level, const char * funct, const char * file, int line, ...)
31 {
32         //Todo: consider setlogmask(3) to filter messages
33         const char *fmt;
34         char buffer[BUFSIZ];
35         va_list va;
36
37         // Don't print the message unless we need to
38         if (level > g_options.verbosity)
39                 return;
40
41         va_start(va, line);
42         fmt = va_arg(va, const char*);
43         
44         if (fmt == NULL) // sanity check
45                 Fatal("Format string is NULL");
46
47         vsnprintf(buffer, BUFSIZ, fmt, va);
48         va_end(va);
49
50         if (funct == NULL)
51                 funct = unspecified_funct;
52
53         // Make a human readable severity string
54         const char *severity;
55         switch (level)
56         {
57                 case LOGERR:
58                         level = LOG_ERR;
59                         severity = "ERROR";
60                         break;
61                 case LOGWARN:
62                         level = LOG_WARNING;
63                         severity = "WARNING";
64                         break;
65                 case LOGNOTE:
66                         level = LOG_NOTICE;
67                         severity = "NOTICE";
68                         break;
69                 case LOGINFO:
70                         level = LOG_INFO;
71                         severity = "INFO";
72                         break;
73                 default:
74                         level = LOG_DEBUG;
75                         severity = "DEBUG";
76                         break;
77         }
78
79         syslog(level, "%s: %s (%s:%d) - %s", severity, funct, file, line, buffer);
80 }
81
82 /**
83  * Handle a Fatal error in the program by printing a message and exiting the program
84  *      CALLING THIS FUNCTION WILL CAUSE THE PROGAM TO EXIT
85  * @param funct - Name of the calling function
86  * @param file - Name of the source file containing the calling function
87  * @param line - Line in the source file at which Fatal is called
88  * @param fmt - A format string
89  * @param ... - Arguments to be printed according to the format string
90  */
91 void FatalEx(const char * funct, const char * file, int line, ...)
92 {
93         const char *fmt;
94         char buffer[BUFSIZ];
95         va_list va;
96         va_start(va, line);
97         fmt = va_arg(va, const char*);
98         
99         if (fmt == NULL)
100         {
101                 // Fatal error in the Fatal function.
102                 // (This really shouldn't happen unless someone does something insanely stupid)
103                 Fatal("Format string is NULL");
104                 return; // Should never get here
105         }
106
107         vsnprintf(buffer, BUFSIZ, fmt,va);
108         va_end(va);
109
110         if (funct == NULL)
111                 funct = unspecified_funct;
112
113         syslog(LOG_CRIT, "FATAL: %s (%s:%d) - %s", funct, file, line, buffer);
114         exit(EXIT_FAILURE);
115 }
116
117

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