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