Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 1 | #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 | /** |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 14 | * T_GLOBAL_DEBUGGING_LEVEL = 0: all debugging turned off, debug macros undefined |
| 15 | * T_GLOBAL_DEBUGGING_LEVEL = 1: all debugging turned on |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 16 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 17 | #define T_GLOBAL_DEBUGGING_LEVEL 0 |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 18 | |
| 19 | |
| 20 | /** |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 21 | * T_GLOBAL_LOGGING_LEVEL = 0: all logging turned off, logging macros undefined |
| 22 | * T_GLOBAL_LOGGING_LEVEL = 1: all logging turned on |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 23 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 24 | #define T_GLOBAL_LOGGING_LEVEL 1 |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 25 | |
| 26 | |
| 27 | /** |
| 28 | * Standard wrapper around fprintf what will prefix the file name and line |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 29 | * number to the line. Uses T_GLOBAL_DEBUGGING_LEVEL to control whether it is |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 30 | * turned on or off. |
| 31 | * |
| 32 | * @param format_string |
| 33 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 34 | #if T_GLOBAL_DEBUGGING_LEVEL > 0 |
| 35 | #define T_DEBUG(format_string,...) \ |
| 36 | if (T_GLOBAL_DEBUGGING_LEVEL > 0) { \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 37 | fprintf(stderr,"[%s,%d] " #format_string " \n", __FILE__, __LINE__,##__VA_ARGS__); \ |
| 38 | } |
| 39 | #else |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 40 | #define T_DEBUG(format_string,...) |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 41 | #endif |
| 42 | |
| 43 | |
| 44 | /** |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 45 | * analagous to T_DEBUG but also prints the time |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 46 | * |
| 47 | * @param string format_string input: printf style format string |
| 48 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 49 | #if T_GLOBAL_DEBUGGING_LEVEL > 0 |
| 50 | #define T_DEBUG_T(format_string,...) \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 51 | { \ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 52 | if (T_GLOBAL_DEBUGGING_LEVEL > 0) { \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 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 |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 62 | #define T_DEBUG_T(format_string,...) |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 63 | #endif |
| 64 | |
| 65 | |
| 66 | /** |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 67 | * analagous to T_DEBUG but uses input level to determine whether or not the string |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 68 | * should be logged. |
| 69 | * |
| 70 | * @param int level: specified debug level |
| 71 | * @param string format_string input: format string |
| 72 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 73 | #define T_DEBUG_L(level, format_string,...) \ |
| 74 | if ((level) > 0) { \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 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 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 84 | #define T_ERROR(format_string,...) \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 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 | /** |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 96 | * Analagous to T_ERROR, additionally aborting the process. |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 97 | * WARNING: macro calls abort(), ending program execution |
| 98 | * |
| 99 | * @param string format_string input: printf style format string |
| 100 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 101 | #define T_ERROR_ABORT(format_string,...) \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 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 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 118 | #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 Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 129 | } |
| 130 | #else |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 131 | #define T_LOG_OPER(format_string,...) |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 132 | #endif |
| 133 | |
| 134 | #endif // _THRIFT_LOGGING_H |