Add basic 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
13 inline std::string methodName(const std::string& prettyFunction)
14 {
15     size_t colons = prettyFunction.find("::");
16     size_t begin = prettyFunction.substr(0,colons).rfind(" ") + 1;
17     size_t end = prettyFunction.rfind("(") - begin;
18
19     return prettyFunction.substr(begin,end) + "()";
20 }
21
22 #define __METHOD_NAME__ methodName(__PRETTY_FUNCTION__).c_str()
23
24 //#define LOG_SYSLOG
25
26 #ifdef LOG_SYSLOG
27         #include <syslog.h>
28 #else
29         enum {LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG};
30 #endif //LOG_SYSLOG
31
32 //To get around a 'pedantic' C99 rule that you must have at least 1 variadic arg, combine fmt into that.
33 #define Log(level, ...) LogEx(level, __METHOD_NAME__, __FILE__, __LINE__, __VA_ARGS__)
34 #define Fatal(...) FatalEx(__METHOD_NAME__, __FILE__, __LINE__, __VA_ARGS__)
35
36 #define Debug(...) LogEx(LOG_DEBUG, __func__, __FILE__, __LINE__, __VA_ARGS__)
37 #define Error(...) LogEx(LOG_ERR, __func__, __FILE__, __LINE__, __VA_ARGS__)
38 #define Warn(...) LogEx(LOG_WARN, __func__, __FILE__, __LINE__, __VA_ARGS__)
39
40
41 extern void LogEx(int level, const char * funct, const char * file, int line,  ...); // General function for printing log messages to stderr
42 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).
43
44 #endif //_LOG_H
45
46 //EOF

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