Merge branch 'master' of git.ucc.asn.au:/ipdf/code
[ipdf/code.git] / src / log.cpp
1 /**
2  * @file log.cpp
3  * @brief Implement logging and error handling functions
4  */
5
6
7 // --- Custom headers --- //
8 #include "common.h"
9
10 #include <cstdio>
11 #include <unistd.h>
12 #include <stdarg.h>
13
14 #ifdef LOG_SYSLOG
15
16 static void InitSyslog()
17 {
18         static bool init = false;
19         if (!init)
20         {
21                 openlog(Options::program_name, LOG_PERROR | LOG_PID, LOG_USER);
22                 init = true;
23         }
24 }
25
26 #endif //LOG_SYSLOG
27
28 static const char * unspecified_funct = "???";
29
30
31
32 /**
33  * Print a message to stderr and log it via syslog. The message must be
34  * less than BUFSIZ characters long, or it will be truncated.
35  * @param level - Specify how severe the message is.
36         If level is higher (less urgent) than the program's verbosity (see options.h) no message will be printed
37  * @param funct - String indicating the function name from which this function was called.
38         If this is NULL, Log will show the unspecified_funct string instead
39  * @param file - Source file containing the function
40  * @param line - Line in the source file at which Log is called
41  * @param fmt - A format string
42  * @param ... - Arguments to be printed according to the format string
43  */
44 void LogEx(int level, const char * funct, const char * file, int line, ...)
45 {
46         //Todo: consider setlogmask(3) to filter messages
47         const char *fmt;
48         char buffer[BUFSIZ];
49         va_list va;
50
51         // Don't print the message unless we need to
52         //if (level > Globals::Verbosity())
53         //      return;
54
55         va_start(va, line);
56         fmt = va_arg(va, const char*);
57         
58         if (fmt == NULL) // sanity check
59                 Fatal("Format string is NULL");
60
61         vsnprintf(buffer, BUFSIZ, fmt, va);
62         va_end(va);
63
64         if (funct == NULL)
65                 funct = unspecified_funct;
66
67         // Make a human readable severity string
68         const char *severity;
69         switch (level)
70         {
71                 case LOG_ERR:
72                         severity = "ERROR";
73                         break;
74                 case LOG_WARNING:
75                         severity = "WARNING";
76                         break;
77                 case LOG_NOTICE:
78                         severity = "notice";
79                         break;
80                 case LOG_INFO:
81                         severity = "info";
82                         break;
83                 default:
84                         severity = "debug";
85                         break;
86         }
87
88         #ifdef LOG_SYSLOG
89                 InitSyslog();
90                 syslog(level, "%s: %s (%s:%d) - %s", severity, funct, file, line, buffer);
91         #else
92                 //fprintf(stderr, "%s[%d]: %s: %s (%s:%d) - %s\n", Options::program_name, getpid(), severity, funct, file, line, buffer);
93                 fprintf(stderr, "%s: %s (%s:%d) - %s\n", severity, funct, file, line, buffer);
94         #endif //LOG_SYSLOG
95         
96 }
97
98 /**
99  * Handle a Fatal error in the program by printing a message and exiting the program
100  *      CALLING THIS FUNCTION WILL CAUSE THE PROGAM TO EXIT
101  * @param funct - Name of the calling function
102  * @param file - Name of the source file containing the calling function
103  * @param line - Line in the source file at which Fatal is called
104  * @param fmt - A format string
105  * @param ... - Arguments to be printed according to the format string
106  */
107 void FatalEx(const char * funct, const char * file, int line, ...)
108 {
109         const char *fmt;
110         char buffer[BUFSIZ];
111         va_list va;
112         va_start(va, line);
113         fmt = va_arg(va, const char*);
114         
115         if (fmt == NULL)
116         {
117                 // Fatal error in the Fatal function.
118                 // (This really shouldn't happen unless someone does something insanely stupid)
119                 Fatal("Format string is NULL");
120                 return; // Should never get here
121         }
122
123         vsnprintf(buffer, BUFSIZ, fmt,va);
124         va_end(va);
125
126         if (funct == NULL)
127                 funct = unspecified_funct;
128
129         #ifdef LOG_SYSLOG
130                 InitSyslog();
131                 syslog(LOG_CRIT, "FATAL: %s (%s:%d) - %s", funct, file, line, buffer);
132         #else
133                 //fprintf(stderr, "%s[%d]: FATAL: %s (%s:%d) - %s\n", Options::program_name, getpid(), funct, file, line, buffer);
134                 fprintf(stderr, "FATAL: %s (%s:%d) - %s\n", funct, file, line, buffer);
135
136         #endif //LOG_SYSLOG
137
138         exit(EXIT_FAILURE);
139 }
140
141

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