Merge branch 'master' of git.ucc.asn.au:/ipdf/code
[ipdf/code.git] / src / log.h
1 /**
2  * @file log.h
3  * @brief Declaration of functions for printing log messages and/or terminating program after a fatal error
4  */
5
6 #ifndef _LOG_H
7 #define _LOG_H
8
9 #include <cstdio>
10 #include <cstdlib>
11 #include <string>
12 #include <cstring> // for strerror etc
13
14 inline std::string methodName(const std::string& prettyFunction)
15 {
16     size_t colons = prettyFunction.find("::");
17     size_t begin = prettyFunction.substr(0,colons).rfind(" ") + 1;
18     size_t end = prettyFunction.rfind("(") - begin;
19
20     return prettyFunction.substr(begin,end) + "()";
21 }
22
23 #define __METHOD_NAME__ methodName(__PRETTY_FUNCTION__).c_str()
24
25 //#define LOG_SYSLOG
26
27 #ifdef LOG_SYSLOG
28         #include <syslog.h>
29 #else
30         enum {LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG};
31 #endif //LOG_SYSLOG
32
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__)
36
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);
41
42
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).
45
46 #endif //_LOG_H
47
48 //EOF

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