3 * @brief Declaration of functions for printing log messages and/or terminating program after a fatal error
12 #include <cstring> // for strerror etc
14 inline std::string methodName(const std::string& prettyFunction)
16 size_t colons = prettyFunction.find("::");
17 size_t begin = prettyFunction.substr(0,colons).rfind(" ") + 1;
18 size_t end = prettyFunction.rfind("(") - begin;
20 return prettyFunction.substr(begin,end) + "()";
23 #define __METHOD_NAME__ methodName(__PRETTY_FUNCTION__).c_str()
30 enum {LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG};
33 //To get around a 'pedantic' C99 rule that you must have at least 1 variadic arg, combine fmt into that.
34 #define Log(level, ...) LogEx(level, __METHOD_NAME__, __FILE__, __LINE__, __VA_ARGS__)
35 #define Fatal(...) FatalEx(__METHOD_NAME__, __FILE__, __LINE__, __VA_ARGS__)
37 #define Debug(...) LogEx(LOG_DEBUG, __func__, __FILE__, __LINE__, __VA_ARGS__)
38 #define Error(...) LogEx(LOG_ERR, __func__, __FILE__, __LINE__, __VA_ARGS__)
39 #define Warn(...) LogEx(LOG_WARNING, __func__, __FILE__, __LINE__, __VA_ARGS__)
40 extern void Backtrace(int size=10);
43 extern void LogEx(int level, const char * funct, const char * file, int line, ...); // General function for printing log messages to stderr
44 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).