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

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