From: Sam Moore Date: Wed, 26 Mar 2014 08:20:30 +0000 (+0800) Subject: Add basic code X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=f8de7d373ff0a5d977adee1d1a464401ece60550;p=ipdf%2Fcode.git Add basic code Log functions & Makefil --- diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f533957 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.o +*.d +*~ diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..39f09ef --- /dev/null +++ b/src/Makefile @@ -0,0 +1,35 @@ +#Makefile +CXX = g++ -std=gnu++0x -Wall -Werror -Wshadow -pedantic -g +OBJ = log.o +LIB = #-lSDL2 +DEPS := $(OBJ:%.o=%.d) + +LINKOBJ = $(OBJ) + +RM = rm -f +BIN = ipdf + +all : $(BIN) + +$(BIN) : $(LINKOBJ) main.o + $(CXX) -o $(BIN) main.o $(LINKOBJ) $(LIB) + +%.o : %.cpp + $(CXX) -c -MMD -o $@ $< + +no_main : $(OBJ) + +main.o : main.cpp + $(CXX) -c main.cpp + +clean : + $(RM) $(BIN) $(OBJ) $(LINKOBJ) main.o + +clean_full: #cleans up all backup files + $(RM) $(BIN) $(OBJ) $(LINKOBJ) + $(RM) *.*~ + $(RM) *~ + $(RM) *.o + + + diff --git a/src/common.h b/src/common.h new file mode 100644 index 0000000..6af0792 --- /dev/null +++ b/src/common.h @@ -0,0 +1,4 @@ +#include +#include + +#include "log.h" diff --git a/src/ipdf b/src/ipdf new file mode 100755 index 0000000..e6be120 Binary files /dev/null and b/src/ipdf differ diff --git a/src/log.cpp b/src/log.cpp new file mode 100644 index 0000000..699036b --- /dev/null +++ b/src/log.cpp @@ -0,0 +1,141 @@ +/** + * @file log.cpp + * @brief Implement logging and error handling functions + */ + + +// --- Custom headers --- // +#include "common.h" + +#include +#include +#include + +#ifdef LOG_SYSLOG + +static void InitSyslog() +{ + static bool init = false; + if (!init) + { + openlog(Options::program_name, LOG_PERROR | LOG_PID, LOG_USER); + init = true; + } +} + +#endif //LOG_SYSLOG + +static const char * unspecified_funct = "???"; + + + +/** + * Print a message to stderr and log it via syslog. The message must be + * less than BUFSIZ characters long, or it will be truncated. + * @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 + * @param funct - String indicating the function name from which this function was called. + If this is NULL, Log will show the unspecified_funct string instead + * @param file - Source file containing the function + * @param line - Line in the source file at which Log is called + * @param fmt - A format string + * @param ... - Arguments to be printed according to the format string + */ +void LogEx(int level, const char * funct, const char * file, int line, ...) +{ + //Todo: consider setlogmask(3) to filter messages + const char *fmt; + char buffer[BUFSIZ]; + va_list va; + + // Don't print the message unless we need to + //if (level > Globals::Verbosity()) + // return; + + va_start(va, line); + fmt = va_arg(va, const char*); + + if (fmt == NULL) // sanity check + Fatal("Format string is NULL"); + + vsnprintf(buffer, BUFSIZ, fmt, va); + va_end(va); + + if (funct == NULL) + funct = unspecified_funct; + + // Make a human readable severity string + const char *severity; + switch (level) + { + case LOG_ERR: + severity = "ERROR"; + break; + case LOG_WARNING: + severity = "WARNING"; + break; + case LOG_NOTICE: + severity = "NOTICE"; + break; + case LOG_INFO: + severity = "INFO"; + break; + default: + severity = "DEBUG"; + break; + } + + #ifdef LOG_SYSLOG + InitSyslog(); + syslog(level, "%s: %s (%s:%d) - %s", severity, funct, file, line, buffer); + #else + //fprintf(stderr, "%s[%d]: %s: %s (%s:%d) - %s\n", Options::program_name, getpid(), severity, funct, file, line, buffer); + fprintf(stderr, "%s: %s (%s:%d) - %s\n", severity, funct, file, line, buffer); + #endif //LOG_SYSLOG + +} + +/** + * 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 file - Name of the source file containing the calling function + * @param line - Line in the source file at which Fatal is called + * @param fmt - A format string + * @param ... - Arguments to be printed according to the format string + */ +void FatalEx(const char * funct, const char * file, int line, ...) +{ + const char *fmt; + char buffer[BUFSIZ]; + va_list va; + va_start(va, line); + 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("Format string is NULL"); + return; // Should never get here + } + + vsnprintf(buffer, BUFSIZ, fmt,va); + va_end(va); + + if (funct == NULL) + funct = unspecified_funct; + + #ifdef LOG_SYSLOG + InitSyslog(); + syslog(LOG_CRIT, "FATAL: %s (%s:%d) - %s", funct, file, line, buffer); + #else + //fprintf(stderr, "%s[%d]: FATAL: %s (%s:%d) - %s\n", Options::program_name, getpid(), funct, file, line, buffer); + fprintf(stderr, "FATAL: %s (%s:%d) - %s\n", funct, file, line, buffer); + + #endif //LOG_SYSLOG + + exit(EXIT_FAILURE); +} + + diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..0dfedfd --- /dev/null +++ b/src/log.h @@ -0,0 +1,46 @@ +/** + * @file log.h + * @brief Declaration of functions for printing log messages and/or terminating program after a fatal error + */ + +#ifndef _LOG_H +#define _LOG_H + +#include +#include +#include + +inline std::string methodName(const std::string& prettyFunction) +{ + size_t colons = prettyFunction.find("::"); + size_t begin = prettyFunction.substr(0,colons).rfind(" ") + 1; + size_t end = prettyFunction.rfind("(") - begin; + + return prettyFunction.substr(begin,end) + "()"; +} + +#define __METHOD_NAME__ methodName(__PRETTY_FUNCTION__).c_str() + +//#define LOG_SYSLOG + +#ifdef LOG_SYSLOG + #include +#else + enum {LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG}; +#endif //LOG_SYSLOG + +//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, __METHOD_NAME__, __FILE__, __LINE__, __VA_ARGS__) +#define Fatal(...) FatalEx(__METHOD_NAME__, __FILE__, __LINE__, __VA_ARGS__) + +#define Debug(...) LogEx(LOG_DEBUG, __func__, __FILE__, __LINE__, __VA_ARGS__) +#define Error(...) LogEx(LOG_ERR, __func__, __FILE__, __LINE__, __VA_ARGS__) +#define Warn(...) LogEx(LOG_WARN, __func__, __FILE__, __LINE__, __VA_ARGS__) + + +extern void LogEx(int level, const char * funct, const char * file, int line, ...); // General function for printing log messages to stderr +extern void FatalEx(const char * funct, const char * file, int line, ...); // Function that deals with a fatal error (prints a message, then exits the program). + +#endif //_LOG_H + +//EOF diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..e683564 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,7 @@ +#include "common.h" + +int main(int argc, char ** argv) +{ + Debug("It's alive!"); + return 0; +}