Modify logging functions to use inbuilt function naming identifiers
[matches/MCTX3420.git] / rpi / log.c
1 /**
2  * @file log.c
3  * @purpose Implement logging and error handling functions
4  */
5
6
7 #include <unistd.h>
8
9 // --- Custom headers --- //
10 #include "log.h"
11 #include "options.h"
12
13 // --- Static variables --- //
14 static const char * unspecified_funct = "???";
15
16 // --- Function implementations --- //
17
18 /**
19  * @funct LogEx
20  * @purpose Print a message to stderr
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         const char *fmt;
31         va_list va;
32         va_start(va, funct);
33         fmt = va_arg(va, const char*);
34         
35         if (fmt == NULL) // sanity check
36                 FatalEx("Log", "Format string is NULL");
37
38         // Don't print the message unless we need to
39         if (level > g_options.verbosity)
40                 return;
41
42         if (funct == NULL)
43                 funct = unspecified_funct;
44
45         // Make a human readable severity string
46         const char *severity;
47         switch (level)
48         {
49                 case LOGERR:
50                         severity = "ERROR";
51                         break;
52                 case LOGWARN:
53                         severity = "WARNING";
54                         break;
55                 case LOGNOTE:
56                         severity = "NOTICE";
57                         break;
58                 case LOGINFO:
59                         severity = "INFO";
60                         break;
61                 default:
62                         severity = "DEBUG";
63                         break;
64         }
65
66         // Print: Program name, PID, severity string, function name first
67         fprintf(stderr, "%s [%d] : %s : %s - ", g_options.program, getpid(), severity, funct);
68
69         // Then pass additional arguments with the format string to vfprintf for printing
70         vfprintf(stderr, fmt, va);
71         va_end(va);
72
73         // End log messages with a newline
74         fprintf(stderr, "\n");
75 }
76
77 /**
78  * @funct FatalEx
79  * @purpose Handle a Fatal error in the program by printing a message and exiting the program
80         CALLING THIS FUNCTION WILL CAUSE THE PROGAM TO EXIT
81  * @param funct - Name of the calling function
82  * @param fmt - A format string
83  * @param ... - Arguments to be printed according to the format string
84  */
85 void FatalEx(const char * funct, ...)
86 {
87         const char *fmt;
88         va_list va;
89         va_start(va, funct);
90         fmt = va_arg(va, const char*);
91         
92         if (fmt == NULL)
93         {
94                 // Fatal error in the Fatal function.
95                 // (This really shouldn't happen unless someone does something insanely stupid)
96                 FatalEx("Fatal", "Format string is NULL");
97                 return; // Should never get here
98         }
99
100         if (funct == NULL)
101                 funct = unspecified_funct;
102
103         fprintf(stderr, "%s [%d] : %s : FATAL - ", g_options.program, getpid(), funct);
104
105         vfprintf(stderr, fmt, va);
106         va_end(va);
107         fprintf(stderr, "\n");
108
109         exit(EXIT_FAILURE);
110 }
111
112

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