-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.h
76 lines (57 loc) · 2.91 KB
/
logging.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
@file logging.h
Yet another set of logging macros.
Everyone has their own, it seems :)
Copyright 2011, Paul Chambers. All Rights Reserved.
*/
#ifndef __MY_LOGGING__
#define __MY_LOGGING__
#include <syslog.h>
#ifdef UNUSED
#elif defined(__GNUC__)
# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
#elif defined(__LCLINT__)
# define UNUSED(x) /*@unused@*/ x
#else
# define UNUSED(x) x
#endif
/* private - don't set this directly - use setLogThreshold() */
/* higher the number, more gets logged. logFatal and logError always log. */
extern int gLogThreshold;
/* also private - set using logTo() */
extern FILE *gLogFile;
void initLogging(int logLevel, FILE *logFile);
void setLogThreshold(int logLevel);
void logTo(FILE *logFile);
/* loggging macros */
#define logFatal(...) \
{ _logHelper(LOG_CRIT, __FILE__, __func__, __LINE__, 0, __VA_ARGS__); }
#define logFatalErrno(...) \
{ _logHelper(LOG_CRIT, __FILE__, __func__, __LINE__, errno, __VA_ARGS__); }
#define logError(...) \
{ _logHelper(LOG_ERR, __FILE__, __func__, __LINE__, 0, __VA_ARGS__); }
#define logErrorErrno(...) \
{ _logHelper(LOG_ERR, __FILE__, __func__, __LINE__, errno, __VA_ARGS__); }
#define logWarning(...) \
{ if (LOG_WARNING <= gLogThreshold) _logHelper(LOG_WARNING, __FILE__, __func__, __LINE__, 0, __VA_ARGS__); }
#define logWarningErrno(...) \
{ if (LOG_WARNING <= gLogThreshold) _logHelper(LOG_WARNING, __FILE__, __func__, __LINE__, errno, __VA_ARGS__); }
#define logInfo(...) \
{ if (LOG_INFO <= gLogThreshold) _logHelper(LOG_INFO, __FILE__, __func__, __LINE__, 0, __VA_ARGS__); }
#define logInfoErrno(...) \
{ if (LOG_INFO <= gLogThreshold) _logHelper(LOG_INFO, __FILE__, __func__, __LINE__, errno, __VA_ARGS__); }
#define logDebug(level, ...) \
{ if ((LOG_DEBUG + level) <= gLogThreshold) _logHelper((LOG_DEBUG + level), __FILE__, __func__, __LINE__, 0, __VA_ARGS__); }
#define logDebugErrno(level, ...) \
{ if ((LOG_DEBUG + level) <= gLogThreshold) _logHelper((LOG_DEBUG + level), __FILE__, __func__, __LINE__, errno, __VA_ARGS__); }
void _logHelper( int level, const char *file, const char *function, const int line, const int error, const char *format, ...)
__attribute__ ((format (printf, 6, 7)));
#define DEBUG_LINE_PREFIX "dbg: "
#define logDebugEnabled(level) ((LOG_DEBUG + level) <= gLogThreshold)
#define logprintf(...) { fprintf(gLogFile, __VA_ARGS__); }
/* fatal error macros */
#define fatalExit(exitcode, ...) _fatalExit(exitcode, __FILE__, __func__, __LINE__, 0, __VA_ARGS__)
#define fatalExitErrno(exitcode, ...) _fatalExit(exitcode, __FILE__, __func__, __LINE__, errno, __VA_ARGS__)
void _fatalExit( int exitcode, const char *file, const char *function, const int line, const int error, const char *format, ...)
__attribute__ ((format (printf, 6, 7),noreturn));
#endif