Modify logging functions to use inbuilt function naming identifiers
authorJeremy Tan <[email protected]>
Thu, 15 Aug 2013 01:10:54 +0000 (09:10 +0800)
committerJeremy Tan <[email protected]>
Thu, 15 Aug 2013 01:10:54 +0000 (09:10 +0800)
.gitignore [new file with mode: 0644]
rpi/log.c
rpi/log.h
rpi/main.c

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..baf45a8
--- /dev/null
@@ -0,0 +1,12 @@
+*.so
+*.o
+*.exe
+*.dll
+
+.DS_Store
+.DS_Store?
+._*
+.Spotlight-V100
+.Trashes
+ehthumbs.db
+Thumbs.db
\ No newline at end of file
index bd41936..412a34c 100644 (file)
--- a/rpi/log.c
+++ b/rpi/log.c
 #include "options.h"
 
 // --- Static variables --- //
-static char * unspecified_funct = (char*)"???";
+static const char * unspecified_funct = "???";
 
 // --- Function implementations --- //
 
 /**
- * @funct Log
+ * @funct LogEx
  * @purpose Print a message to stderr
  * @param level - Specify how severe the message is.
        If level is higher (less urgent) than the program's verbosity (see options.h) no message will be printed
@@ -25,36 +25,41 @@ static char * unspecified_funct = (char*)"???";
  * @param fmt - A format string
  * @param ... - Arguments to be printed according to the format string
  */
-void Log(int level, char * funct, char * fmt, ...)
+void LogEx(int level, const char * funct, ...)
 {
+       const char *fmt;
+       va_list va;
+       va_start(va, funct);
+       fmt = va_arg(va, const char*);
+       
        if (fmt == NULL) // sanity check
-               Fatal("Log", "Format string is NULL");
+               FatalEx("Log", "Format string is NULL");
 
        // Don't print the message unless we need to
-       if (level > g_options.verbosity) 
+       if (level > g_options.verbosity)
                return;
 
        if (funct == NULL)
                funct = unspecified_funct;
 
        // Make a human readable severity string
-       char severity[BUFSIZ];
+       const char *severity;
        switch (level)
        {
                case LOGERR:
-                       sprintf(severity, "ERROR");
+                       severity = "ERROR";
                        break;
                case LOGWARN:
-                       sprintf(severity, "WARNING");
+                       severity = "WARNING";
                        break;
                case LOGNOTE:
-                       sprintf(severity, "NOTICE");
+                       severity = "NOTICE";
                        break;
                case LOGINFO:
-                       sprintf(severity, "INFO");
+                       severity = "INFO";
                        break;
                default:
-                       sprintf(severity, "DEBUG");
+                       severity = "DEBUG";
                        break;
        }
 
@@ -62,8 +67,6 @@ void Log(int level, char * funct, char * fmt, ...)
        fprintf(stderr, "%s [%d] : %s : %s - ", g_options.program, getpid(), severity, funct);
 
        // Then pass additional arguments with the format string to vfprintf for printing
-       va_list va;
-       va_start(va, fmt);
        vfprintf(stderr, fmt, va);
        va_end(va);
 
@@ -72,21 +75,25 @@ void Log(int level, char * funct, char * fmt, ...)
 }
 
 /**
- * @funct Fatal
+ * @funct FatalEx
  * @purpose Handle a Fatal error in the program by printing a message and exiting the program
        CALLING THIS FUNCTION WILL CAUSE THE PROGAM TO EXIT
  * @param funct - Name of the calling function
  * @param fmt - A format string
  * @param ... - Arguments to be printed according to the format string
  */
-void Fatal(char * funct, char * fmt, ...)
+void FatalEx(const char * funct, ...)
 {
+       const char *fmt;
+       va_list va;
+       va_start(va, funct);
+       fmt = va_arg(va, const char*);
        
        if (fmt == NULL)
        {
                // Fatal error in the Fatal function.
                // (This really shouldn't happen unless someone does something insanely stupid)
-               Fatal("Fatal", "Format string is NULL");
+               FatalEx("Fatal", "Format string is NULL");
                return; // Should never get here
        }
 
@@ -95,8 +102,6 @@ void Fatal(char * funct, char * fmt, ...)
 
        fprintf(stderr, "%s [%d] : %s : FATAL - ", g_options.program, getpid(), funct);
 
-       va_list va;
-       va_start(va, fmt);
        vfprintf(stderr, fmt, va);
        va_end(va);
        fprintf(stderr, "\n");
index 99d54d0..6db52f6 100644 (file)
--- a/rpi/log.h
+++ b/rpi/log.h
 #include <stdbool.h>
 #include <stdarg.h>
 
+//To get around a 'pedantic' C99 rule that you must have at least 1 variadic arg, combine fmt into that.
+#define Log(level, ...) LogEx(level, __func__, __VA_ARGS__)
+#define Fatal(...) FatalEx(__func__, __VA_ARGS__)
 
 // An enum to make the severity of log messages human readable in code
 enum {LOGERR=0, LOGWARN=1, LOGNOTE=2, LOGINFO=3,LOGDEBUG=4};
 
-extern void Log(int level, char * funct, char * fmt,...); // General function for printing log messages to stderr
-extern void Fatal(char * funct, char * fmt, ...); // Function that deals with a fatal error (prints a message, then exits the program).
+extern void LogEx(int level, const char * funct, ...); // General function for printing log messages to stderr
+extern void FatalEx(const char * funct, ...); // Function that deals with a fatal error (prints a message, then exits the program).
 
 #endif //_LOG_H
 
index bb3b5ac..22fad61 100644 (file)
@@ -31,7 +31,7 @@ void ParseArguments(int argc, char ** argv, Options * opts)
 {
        opts->program = argv[0]; // program name
        opts->verbosity = LOGDEBUG; // default log level
-       Log(LOGDEBUG, "ParseArguments", "Called as %s with %d arguments.", opts->program, argc);
+       Log(LOGDEBUG, "Called as %s with %d arguments.", opts->program, argc);
 }
 
 /**
@@ -43,7 +43,7 @@ void SignalHandler(int sig)
 {
        // At the moment just always exit.
        // Call `exit` so that Cleanup will be called to... clean up.
-       Log(LOGWARN, "SignalHandler", "Got signal %d (%s). Exiting.", sig, strsignal(sig));
+       Log(LOGWARN, "Got signal %d (%s). Exiting.", sig, strsignal(sig));
        exit(sig);
 }
 
@@ -53,8 +53,8 @@ void SignalHandler(int sig)
  */
 void Cleanup()
 {
-       Log(LOGDEBUG, "Cleanup", "Begin cleanup.");
-       Log(LOGDEBUG, "Cleanup", "Finish cleanup.");
+       Log(LOGDEBUG, "Begin cleanup.");
+       Log(LOGDEBUG, "Finish cleanup.");
 
 }
 
@@ -67,6 +67,7 @@ void Cleanup()
  */
 int main(int argc, char ** argv)
 {
+       ParseArguments(argc, argv, &g_options);
        return 0;
 }
 

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