blob: 6d82caf1e89d272d367d6a2e3ff6738068274e5a [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
Mark Slee63608e82006-10-26 05:06:26 +000010#include <time.h>
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000011#include <stdint.h>
12
13/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000014 * T_GLOBAL_DEBUGGING_LEVEL = 0: all debugging turned off, debug macros undefined
15 * T_GLOBAL_DEBUGGING_LEVEL = 1: all debugging turned on
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000016 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000017#define T_GLOBAL_DEBUGGING_LEVEL 0
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000018
19
20/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000021 * T_GLOBAL_LOGGING_LEVEL = 0: all logging turned off, logging macros undefined
22 * T_GLOBAL_LOGGING_LEVEL = 1: all logging turned on
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000023 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000024#define T_GLOBAL_LOGGING_LEVEL 1
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000025
26
27/**
28 * Standard wrapper around fprintf what will prefix the file name and line
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000029 * number to the line. Uses T_GLOBAL_DEBUGGING_LEVEL to control whether it is
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000030 * turned on or off.
31 *
32 * @param format_string
33 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000034#if T_GLOBAL_DEBUGGING_LEVEL > 0
35 #define T_DEBUG(format_string,...) \
36 if (T_GLOBAL_DEBUGGING_LEVEL > 0) { \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000037 fprintf(stderr,"[%s,%d] " #format_string " \n", __FILE__, __LINE__,##__VA_ARGS__); \
38 }
39#else
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000040 #define T_DEBUG(format_string,...)
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000041#endif
42
43
44/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000045 * analagous to T_DEBUG but also prints the time
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000046 *
47 * @param string format_string input: printf style format string
48 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000049#if T_GLOBAL_DEBUGGING_LEVEL > 0
50 #define T_DEBUG_T(format_string,...) \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000051 { \
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000052 if (T_GLOBAL_DEBUGGING_LEVEL > 0) { \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000053 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
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000062 #define T_DEBUG_T(format_string,...)
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000063#endif
64
65
66/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000067 * analagous to T_DEBUG but uses input level to determine whether or not the string
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000068 * should be logged.
69 *
70 * @param int level: specified debug level
71 * @param string format_string input: format string
72 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000073#define T_DEBUG_L(level, format_string,...) \
74 if ((level) > 0) { \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000075 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 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000084#define T_ERROR(format_string,...) \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000085 { \
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/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000096 * Analagous to T_ERROR, additionally aborting the process.
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000097 * WARNING: macro calls abort(), ending program execution
98 *
99 * @param string format_string input: printf style format string
100 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000101#define T_ERROR_ABORT(format_string,...) \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000102 { \
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 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000118#if T_GLOBAL_LOGGING_LEVEL > 0
119 #define T_LOG_OPER(format_string,...) \
120 { \
121 if (T_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 } \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000129 }
130#else
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000131 #define T_LOG_OPER(format_string,...)
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000132#endif
133
134#endif // _THRIFT_LOGGING_H