Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame^] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 7 | #ifndef _THRIFT_LOGGING_H |
| 8 | #define _THRIFT_LOGGING_H 1 |
| 9 | |
| 10 | /** |
| 11 | * Contains utility macros for debugging and logging. |
| 12 | * |
| 13 | * @author Aditya Agarwal |
| 14 | */ |
| 15 | |
Mark Slee | 63608e8 | 2006-10-26 05:06:26 +0000 | [diff] [blame] | 16 | #include <time.h> |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 17 | #include <stdint.h> |
| 18 | |
| 19 | /** |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 20 | * T_GLOBAL_DEBUGGING_LEVEL = 0: all debugging turned off, debug macros undefined |
| 21 | * T_GLOBAL_DEBUGGING_LEVEL = 1: all debugging turned on |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 22 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 23 | #define T_GLOBAL_DEBUGGING_LEVEL 0 |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 24 | |
| 25 | |
| 26 | /** |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 27 | * T_GLOBAL_LOGGING_LEVEL = 0: all logging turned off, logging macros undefined |
| 28 | * T_GLOBAL_LOGGING_LEVEL = 1: all logging turned on |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 29 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 30 | #define T_GLOBAL_LOGGING_LEVEL 1 |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 31 | |
| 32 | |
| 33 | /** |
| 34 | * Standard wrapper around fprintf what will prefix the file name and line |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 35 | * 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] | 36 | * turned on or off. |
| 37 | * |
| 38 | * @param format_string |
| 39 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 40 | #if T_GLOBAL_DEBUGGING_LEVEL > 0 |
| 41 | #define T_DEBUG(format_string,...) \ |
| 42 | if (T_GLOBAL_DEBUGGING_LEVEL > 0) { \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 43 | fprintf(stderr,"[%s,%d] " #format_string " \n", __FILE__, __LINE__,##__VA_ARGS__); \ |
| 44 | } |
| 45 | #else |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 46 | #define T_DEBUG(format_string,...) |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 47 | #endif |
| 48 | |
| 49 | |
| 50 | /** |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 51 | * analagous to T_DEBUG but also prints the time |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 52 | * |
| 53 | * @param string format_string input: printf style format string |
| 54 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 55 | #if T_GLOBAL_DEBUGGING_LEVEL > 0 |
| 56 | #define T_DEBUG_T(format_string,...) \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 57 | { \ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 58 | if (T_GLOBAL_DEBUGGING_LEVEL > 0) { \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 59 | time_t now; \ |
| 60 | char dbgtime[26] ; \ |
| 61 | time(&now); \ |
| 62 | ctime_r(&now, dbgtime); \ |
| 63 | dbgtime[24] = '\0'; \ |
| 64 | fprintf(stderr,"[%s,%d] [%s] " #format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \ |
| 65 | } \ |
| 66 | } |
| 67 | #else |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 68 | #define T_DEBUG_T(format_string,...) |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 69 | #endif |
| 70 | |
| 71 | |
| 72 | /** |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 73 | * 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] | 74 | * should be logged. |
| 75 | * |
| 76 | * @param int level: specified debug level |
| 77 | * @param string format_string input: format string |
| 78 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 79 | #define T_DEBUG_L(level, format_string,...) \ |
| 80 | if ((level) > 0) { \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 81 | fprintf(stderr,"[%s,%d] " #format_string " \n", __FILE__, __LINE__,##__VA_ARGS__); \ |
| 82 | } |
| 83 | |
| 84 | |
| 85 | /** |
| 86 | * Explicit error logging. Prints time, file name and line number |
| 87 | * |
| 88 | * @param string format_string input: printf style format string |
| 89 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 90 | #define T_ERROR(format_string,...) \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 91 | { \ |
| 92 | time_t now; \ |
| 93 | char dbgtime[26] ; \ |
| 94 | time(&now); \ |
| 95 | ctime_r(&now, dbgtime); \ |
| 96 | dbgtime[24] = '\0'; \ |
| 97 | fprintf(stderr,"[%s,%d] [%s] ERROR: " #format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \ |
| 98 | } |
| 99 | |
| 100 | |
| 101 | /** |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 102 | * Analagous to T_ERROR, additionally aborting the process. |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 103 | * WARNING: macro calls abort(), ending program execution |
| 104 | * |
| 105 | * @param string format_string input: printf style format string |
| 106 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 107 | #define T_ERROR_ABORT(format_string,...) \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 108 | { \ |
| 109 | time_t now; \ |
| 110 | char dbgtime[26] ; \ |
| 111 | time(&now); \ |
| 112 | ctime_r(&now, dbgtime); \ |
| 113 | dbgtime[24] = '\0'; \ |
| 114 | fprintf(stderr,"[%s,%d] [%s] ERROR: Going to abort " #format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \ |
| 115 | exit(1); \ |
| 116 | } |
| 117 | |
| 118 | |
| 119 | /** |
| 120 | * Log input message |
| 121 | * |
| 122 | * @param string format_string input: printf style format string |
| 123 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 124 | #if T_GLOBAL_LOGGING_LEVEL > 0 |
| 125 | #define T_LOG_OPER(format_string,...) \ |
| 126 | { \ |
| 127 | if (T_GLOBAL_LOGGING_LEVEL > 0) { \ |
| 128 | time_t now; \ |
| 129 | char dbgtime[26] ; \ |
| 130 | time(&now); \ |
| 131 | ctime_r(&now, dbgtime); \ |
| 132 | dbgtime[24] = '\0'; \ |
| 133 | fprintf(stderr,"[%s] " #format_string " \n", dbgtime,##__VA_ARGS__); \ |
| 134 | } \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 135 | } |
| 136 | #else |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 137 | #define T_LOG_OPER(format_string,...) |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 138 | #endif |
| 139 | |
| 140 | #endif // _THRIFT_LOGGING_H |