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