blob: 788174e583467c7c9be368a28bf1b958b93d41d6 [file] [log] [blame]
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +00001#ifndef _THRIFT_LOGGING_H
2#define _THRIFT_LOGGING_H 1
3
4/**
5 * Contains utility macros for debugging and logging.
6 *
7 * @author Aditya Agarwal
8 */
9
10#include "time.h"
11#include <stdint.h>
12
13/**
14 * GLOBAL_DEBUGGING_LEVEL = 0: all debugging turned off, debug macros undefined
15 * GLOBAL_DEBUGGING_LEVEL = 1: all debugging turned on
16 */
17#define GLOBAL_DEBUGGING_LEVEL 0
18
19
20/**
21 * GLOBAL_LOGGING_LEVEL = 0: all logging turned off, logging macros undefined
22 * GLOBAL_LOGGING_LEVEL = 1: all logging turned on
23 */
24#define GLOBAL_LOGGING_LEVEL 1
25
26
27/**
28 * Standard wrapper around fprintf what will prefix the file name and line
29 * number to the line. Uses GLOBAL_DEBUGGING_LEVEL to control whether it is
30 * turned on or off.
31 *
32 * @param format_string
33 */
34#if GLOBAL_DEBUGGING_LEVEL > 0
35 #define DEBUG(format_string,...) \
36 if (GLOBAL_DEBUGGING_LEVEL > 0) { \
37 fprintf(stderr,"[%s,%d] " #format_string " \n", __FILE__, __LINE__,##__VA_ARGS__); \
38 }
39#else
40 #define DEBUG(format_string,...)
41#endif
42
43
44/**
45 * analagous to DEBUG but also prints the time
46 *
47 * @param string format_string input: printf style format string
48 */
49#if GLOBAL_DEBUGGING_LEVEL > 0
50 #define DEBUG_T(format_string,...) \
51 { \
52 if (GLOBAL_DEBUGGING_LEVEL > 0) { \
53 time_t now; \
54 char dbgtime[26] ; \
55 time(&now); \
56 ctime_r(&now, dbgtime); \
57 dbgtime[24] = '\0'; \
58 fprintf(stderr,"[%s,%d] [%s] " #format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \
59 } \
60 }
61#else
62 #define DEBUG_T(format_string,...)
63#endif
64
65
66/**
67 * analagous to DEBUG but uses input level to determine whether or not the string
68 * should be logged.
69 *
70 * @param int level: specified debug level
71 * @param string format_string input: format string
72 */
73#define DEBUG_L(level, format_string,...) \
74 if ((level) > 0) { \
75 fprintf(stderr,"[%s,%d] " #format_string " \n", __FILE__, __LINE__,##__VA_ARGS__); \
76 }
77
78
79/**
80 * Explicit error logging. Prints time, file name and line number
81 *
82 * @param string format_string input: printf style format string
83 */
84#define ERROR(format_string,...) \
85 { \
86 time_t now; \
87 char dbgtime[26] ; \
88 time(&now); \
89 ctime_r(&now, dbgtime); \
90 dbgtime[24] = '\0'; \
91 fprintf(stderr,"[%s,%d] [%s] ERROR: " #format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \
92 }
93
94
95/**
96 * Analagous to ERROR, additionally aborting the process.
97 * WARNING: macro calls abort(), ending program execution
98 *
99 * @param string format_string input: printf style format string
100 */
101#define ERROR_ABORT(format_string,...) \
102 { \
103 time_t now; \
104 char dbgtime[26] ; \
105 time(&now); \
106 ctime_r(&now, dbgtime); \
107 dbgtime[24] = '\0'; \
108 fprintf(stderr,"[%s,%d] [%s] ERROR: Going to abort " #format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \
109 exit(1); \
110 }
111
112
113/**
114 * Log input message
115 *
116 * @param string format_string input: printf style format string
117 */
118#if GLOBAL_LOGGING_LEVEL > 0
119 #define LOG_OPER(format_string,...) \
120 { \
121 if (GLOBAL_LOGGING_LEVEL > 0) { \
122 time_t now; \
123 char dbgtime[26] ; \
124 time(&now); \
125 ctime_r(&now, dbgtime); \
126 dbgtime[24] = '\0'; \
127 fprintf(stderr,"[%s] " #format_string " \n", dbgtime,##__VA_ARGS__); \
128 } \
129 }
130#else
131 #define LOG_OPER(format_string,...)
132#endif
133
134#endif // _THRIFT_LOGGING_H