David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
David Reiss | 2375312 | 2007-08-27 19:57:34 +0000 | [diff] [blame] | 20 | #ifndef _THRIFT_TLOGGING_H_ |
| 21 | #define _THRIFT_TLOGGING_H_ 1 |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 22 | |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include "config.h" |
| 25 | #endif |
| 26 | |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 27 | /** |
| 28 | * Contains utility macros for debugging and logging. |
| 29 | * |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 32 | #ifndef HAVE_CLOCK_GETTIME |
Mark Slee | 63608e8 | 2006-10-26 05:06:26 +0000 | [diff] [blame] | 33 | #include <time.h> |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 34 | #else |
| 35 | #include <sys/time.h> |
| 36 | #endif |
| 37 | |
| 38 | #ifdef HAVE_STDINT_H |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 39 | #include <stdint.h> |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 40 | #endif |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 41 | |
| 42 | /** |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 43 | * T_GLOBAL_DEBUGGING_LEVEL = 0: all debugging turned off, debug macros undefined |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 44 | * T_GLOBAL_DEBUGGING_LEVEL = 1: all debugging turned on |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 45 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 46 | #define T_GLOBAL_DEBUGGING_LEVEL 0 |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 47 | |
| 48 | |
| 49 | /** |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 50 | * T_GLOBAL_LOGGING_LEVEL = 0: all logging turned off, logging macros undefined |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 51 | * T_GLOBAL_LOGGING_LEVEL = 1: all logging turned on |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 52 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 53 | #define T_GLOBAL_LOGGING_LEVEL 1 |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 54 | |
| 55 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 56 | /** |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 57 | * Standard wrapper around fprintf what will prefix the file name and line |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 58 | * 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] | 59 | * turned on or off. |
| 60 | * |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 61 | * @param format_string |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 62 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 63 | #if T_GLOBAL_DEBUGGING_LEVEL > 0 |
| 64 | #define T_DEBUG(format_string,...) \ |
| 65 | if (T_GLOBAL_DEBUGGING_LEVEL > 0) { \ |
David Reiss | 46e4f25 | 2010-10-06 17:10:53 +0000 | [diff] [blame^] | 66 | fprintf(stderr,"[%s,%d] " format_string " \n", __FILE__, __LINE__,##__VA_ARGS__); \ |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 67 | } |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 68 | #else |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 69 | #define T_DEBUG(format_string,...) |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 70 | #endif |
| 71 | |
| 72 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 73 | /** |
| 74 | * analagous to T_DEBUG but also prints the time |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 75 | * |
| 76 | * @param string format_string input: printf style format string |
| 77 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 78 | #if T_GLOBAL_DEBUGGING_LEVEL > 0 |
| 79 | #define T_DEBUG_T(format_string,...) \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 80 | { \ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 81 | if (T_GLOBAL_DEBUGGING_LEVEL > 0) { \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 82 | time_t now; \ |
| 83 | char dbgtime[26] ; \ |
| 84 | time(&now); \ |
| 85 | ctime_r(&now, dbgtime); \ |
| 86 | dbgtime[24] = '\0'; \ |
David Reiss | 46e4f25 | 2010-10-06 17:10:53 +0000 | [diff] [blame^] | 87 | fprintf(stderr,"[%s,%d] [%s] " format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 88 | } \ |
| 89 | } |
| 90 | #else |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 91 | #define T_DEBUG_T(format_string,...) |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 92 | #endif |
| 93 | |
| 94 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 95 | /** |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 96 | * 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] | 97 | * should be logged. |
| 98 | * |
| 99 | * @param int level: specified debug level |
| 100 | * @param string format_string input: format string |
| 101 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 102 | #define T_DEBUG_L(level, format_string,...) \ |
| 103 | if ((level) > 0) { \ |
David Reiss | 46e4f25 | 2010-10-06 17:10:53 +0000 | [diff] [blame^] | 104 | fprintf(stderr,"[%s,%d] " format_string " \n", __FILE__, __LINE__,##__VA_ARGS__); \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 108 | /** |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 109 | * Explicit error logging. Prints time, file name and line number |
| 110 | * |
| 111 | * @param string format_string input: printf style format string |
| 112 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 113 | #define T_ERROR(format_string,...) \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 114 | { \ |
| 115 | time_t now; \ |
| 116 | char dbgtime[26] ; \ |
| 117 | time(&now); \ |
| 118 | ctime_r(&now, dbgtime); \ |
| 119 | dbgtime[24] = '\0'; \ |
David Reiss | 46e4f25 | 2010-10-06 17:10:53 +0000 | [diff] [blame^] | 120 | fprintf(stderr,"[%s,%d] [%s] ERROR: " format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 124 | /** |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 125 | * Analagous to T_ERROR, additionally aborting the process. |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 126 | * WARNING: macro calls abort(), ending program execution |
| 127 | * |
| 128 | * @param string format_string input: printf style format string |
| 129 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 130 | #define T_ERROR_ABORT(format_string,...) \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 131 | { \ |
| 132 | time_t now; \ |
| 133 | char dbgtime[26] ; \ |
| 134 | time(&now); \ |
| 135 | ctime_r(&now, dbgtime); \ |
| 136 | dbgtime[24] = '\0'; \ |
David Reiss | 46e4f25 | 2010-10-06 17:10:53 +0000 | [diff] [blame^] | 137 | fprintf(stderr,"[%s,%d] [%s] ERROR: Going to abort " format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 138 | exit(1); \ |
| 139 | } |
| 140 | |
| 141 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 142 | /** |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 143 | * Log input message |
| 144 | * |
| 145 | * @param string format_string input: printf style format string |
| 146 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 147 | #if T_GLOBAL_LOGGING_LEVEL > 0 |
| 148 | #define T_LOG_OPER(format_string,...) \ |
| 149 | { \ |
| 150 | if (T_GLOBAL_LOGGING_LEVEL > 0) { \ |
| 151 | time_t now; \ |
| 152 | char dbgtime[26] ; \ |
| 153 | time(&now); \ |
| 154 | ctime_r(&now, dbgtime); \ |
| 155 | dbgtime[24] = '\0'; \ |
David Reiss | 46e4f25 | 2010-10-06 17:10:53 +0000 | [diff] [blame^] | 156 | fprintf(stderr,"[%s] " format_string " \n", dbgtime,##__VA_ARGS__); \ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 157 | } \ |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 158 | } |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 159 | #else |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 160 | #define T_LOG_OPER(format_string,...) |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 161 | #endif |
| 162 | |
David Reiss | e879c2f | 2010-10-06 17:09:50 +0000 | [diff] [blame] | 163 | |
| 164 | /** |
David Reiss | c3b3622 | 2010-10-06 17:10:10 +0000 | [diff] [blame] | 165 | * T_GLOBAL_DEBUG_VIRTUAL = 0 or unset: normal operation, |
| 166 | * virtual call debug messages disabled |
| 167 | * T_GLOBAL_DEBUG_VIRTUAL = 1: log a debug messages whenever an |
| 168 | * avoidable virtual call is made |
| 169 | * T_GLOBAL_DEBUG_VIRTUAL = 2: record detailed info that can be |
| 170 | * printed by calling |
| 171 | * apache::thrift::profile_print_info() |
David Reiss | e879c2f | 2010-10-06 17:09:50 +0000 | [diff] [blame] | 172 | */ |
David Reiss | c3b3622 | 2010-10-06 17:10:10 +0000 | [diff] [blame] | 173 | #if T_GLOBAL_DEBUG_VIRTUAL > 1 |
| 174 | #define T_VIRTUAL_CALL() \ |
| 175 | ::apache::thrift::profile_virtual_call(typeid(*this)) |
| 176 | #define T_GENERIC_PROTOCOL(template_class, generic_prot, specific_prot) \ |
| 177 | do { \ |
| 178 | if (!(specific_prot)) { \ |
| 179 | ::apache::thrift::profile_generic_protocol( \ |
| 180 | typeid(*template_class), typeid(*generic_prot)); \ |
| 181 | } \ |
| 182 | } while (0) |
| 183 | #elif T_GLOBAL_DEBUG_VIRTUAL == 1 |
| 184 | #define T_VIRTUAL_CALL() \ |
David Reiss | e879c2f | 2010-10-06 17:09:50 +0000 | [diff] [blame] | 185 | fprintf(stderr,"[%s,%d] virtual call\n", __FILE__, __LINE__) |
David Reiss | c3b3622 | 2010-10-06 17:10:10 +0000 | [diff] [blame] | 186 | #define T_GENERIC_PROTOCOL(template_class, generic_prot, specific_prot) \ |
| 187 | do { \ |
| 188 | if (!(specific_prot)) { \ |
| 189 | fprintf(stderr, \ |
| 190 | "[%s,%d] failed to cast to specific protocol type\n", \ |
| 191 | __FILE__, __LINE__); \ |
| 192 | } \ |
| 193 | } while (0) |
David Reiss | e879c2f | 2010-10-06 17:09:50 +0000 | [diff] [blame] | 194 | #else |
| 195 | #define T_VIRTUAL_CALL() |
David Reiss | c3b3622 | 2010-10-06 17:10:10 +0000 | [diff] [blame] | 196 | #define T_GENERIC_PROTOCOL(template_class, generic_prot, specific_prot) |
David Reiss | e879c2f | 2010-10-06 17:09:50 +0000 | [diff] [blame] | 197 | #endif |
| 198 | |
David Reiss | 2375312 | 2007-08-27 19:57:34 +0000 | [diff] [blame] | 199 | #endif // #ifndef _THRIFT_TLOGGING_H_ |