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 | |
Konrad Grochowski | 9be4e68 | 2013-06-22 22:03:31 +0200 | [diff] [blame] | 23 | #include <thrift/thrift-config.h> |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 24 | |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 25 | /** |
| 26 | * Contains utility macros for debugging and logging. |
| 27 | * |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 28 | */ |
| 29 | |
Mark Slee | 63608e8 | 2006-10-26 05:06:26 +0000 | [diff] [blame] | 30 | #include <time.h> |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 31 | |
| 32 | #ifdef HAVE_STDINT_H |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 33 | #include <stdint.h> |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 34 | #endif |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 35 | |
| 36 | /** |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 37 | * T_GLOBAL_DEBUGGING_LEVEL = 0: all debugging turned off, debug macros undefined |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 38 | * T_GLOBAL_DEBUGGING_LEVEL = 1: all debugging 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_DEBUGGING_LEVEL 0 |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 41 | |
| 42 | |
| 43 | /** |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 44 | * T_GLOBAL_LOGGING_LEVEL = 0: all logging turned off, logging macros undefined |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 45 | * T_GLOBAL_LOGGING_LEVEL = 1: all logging turned on |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 46 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 47 | #define T_GLOBAL_LOGGING_LEVEL 1 |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 48 | |
| 49 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 50 | /** |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 51 | * Standard wrapper around fprintf what will prefix the file name and line |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 52 | * 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] | 53 | * turned on or off. |
| 54 | * |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 55 | * @param format_string |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 56 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 57 | #if T_GLOBAL_DEBUGGING_LEVEL > 0 |
| 58 | #define T_DEBUG(format_string,...) \ |
| 59 | if (T_GLOBAL_DEBUGGING_LEVEL > 0) { \ |
David Reiss | 46e4f25 | 2010-10-06 17:10:53 +0000 | [diff] [blame] | 60 | fprintf(stderr,"[%s,%d] " format_string " \n", __FILE__, __LINE__,##__VA_ARGS__); \ |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 61 | } |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 62 | #else |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 63 | #define T_DEBUG(format_string,...) |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 64 | #endif |
| 65 | |
| 66 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 67 | /** |
| 68 | * analagous to T_DEBUG but also prints the time |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 69 | * |
| 70 | * @param string format_string input: printf style format string |
| 71 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 72 | #if T_GLOBAL_DEBUGGING_LEVEL > 0 |
| 73 | #define T_DEBUG_T(format_string,...) \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 74 | { \ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 75 | if (T_GLOBAL_DEBUGGING_LEVEL > 0) { \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 76 | time_t now; \ |
| 77 | char dbgtime[26] ; \ |
| 78 | time(&now); \ |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 79 | THRIFT_CTIME_R(&now, dbgtime); \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 80 | dbgtime[24] = '\0'; \ |
David Reiss | 46e4f25 | 2010-10-06 17:10:53 +0000 | [diff] [blame] | 81 | 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] | 82 | } \ |
| 83 | } |
| 84 | #else |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 85 | #define T_DEBUG_T(format_string,...) |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 86 | #endif |
| 87 | |
| 88 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 89 | /** |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 90 | * 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] | 91 | * should be logged. |
| 92 | * |
| 93 | * @param int level: specified debug level |
| 94 | * @param string format_string input: format string |
| 95 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 96 | #define T_DEBUG_L(level, format_string,...) \ |
| 97 | if ((level) > 0) { \ |
David Reiss | 46e4f25 | 2010-10-06 17:10:53 +0000 | [diff] [blame] | 98 | fprintf(stderr,"[%s,%d] " format_string " \n", __FILE__, __LINE__,##__VA_ARGS__); \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 102 | /** |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 103 | * Explicit error logging. Prints time, file name and line number |
| 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(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); \ |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 112 | THRIFT_CTIME_R(&now, dbgtime); \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 113 | dbgtime[24] = '\0'; \ |
David Reiss | 46e4f25 | 2010-10-06 17:10:53 +0000 | [diff] [blame] | 114 | 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] | 115 | } |
| 116 | |
| 117 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 118 | /** |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 119 | * Analagous to T_ERROR, additionally aborting the process. |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 120 | * WARNING: macro calls abort(), ending program execution |
| 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 | #define T_ERROR_ABORT(format_string,...) \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 125 | { \ |
| 126 | time_t now; \ |
| 127 | char dbgtime[26] ; \ |
| 128 | time(&now); \ |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 129 | THRIFT_CTIME_R(&now, dbgtime); \ |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 130 | dbgtime[24] = '\0'; \ |
David Reiss | 46e4f25 | 2010-10-06 17:10:53 +0000 | [diff] [blame] | 131 | 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] | 132 | exit(1); \ |
| 133 | } |
| 134 | |
| 135 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 136 | /** |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 137 | * Log input message |
| 138 | * |
| 139 | * @param string format_string input: printf style format string |
| 140 | */ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 141 | #if T_GLOBAL_LOGGING_LEVEL > 0 |
| 142 | #define T_LOG_OPER(format_string,...) \ |
| 143 | { \ |
| 144 | if (T_GLOBAL_LOGGING_LEVEL > 0) { \ |
| 145 | time_t now; \ |
| 146 | char dbgtime[26] ; \ |
| 147 | time(&now); \ |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 148 | THRIFT_CTIME_R(&now, dbgtime); \ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 149 | dbgtime[24] = '\0'; \ |
David Reiss | 46e4f25 | 2010-10-06 17:10:53 +0000 | [diff] [blame] | 150 | fprintf(stderr,"[%s] " format_string " \n", dbgtime,##__VA_ARGS__); \ |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 151 | } \ |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 152 | } |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 153 | #else |
Aditya Agarwal | 35ae1c7 | 2006-10-26 03:31:34 +0000 | [diff] [blame] | 154 | #define T_LOG_OPER(format_string,...) |
Aditya Agarwal | be3f8d8 | 2006-10-11 02:43:25 +0000 | [diff] [blame] | 155 | #endif |
| 156 | |
David Reiss | e879c2f | 2010-10-06 17:09:50 +0000 | [diff] [blame] | 157 | |
| 158 | /** |
David Reiss | c3b3622 | 2010-10-06 17:10:10 +0000 | [diff] [blame] | 159 | * T_GLOBAL_DEBUG_VIRTUAL = 0 or unset: normal operation, |
| 160 | * virtual call debug messages disabled |
| 161 | * T_GLOBAL_DEBUG_VIRTUAL = 1: log a debug messages whenever an |
| 162 | * avoidable virtual call is made |
| 163 | * T_GLOBAL_DEBUG_VIRTUAL = 2: record detailed info that can be |
| 164 | * printed by calling |
| 165 | * apache::thrift::profile_print_info() |
David Reiss | e879c2f | 2010-10-06 17:09:50 +0000 | [diff] [blame] | 166 | */ |
David Reiss | c3b3622 | 2010-10-06 17:10:10 +0000 | [diff] [blame] | 167 | #if T_GLOBAL_DEBUG_VIRTUAL > 1 |
| 168 | #define T_VIRTUAL_CALL() \ |
| 169 | ::apache::thrift::profile_virtual_call(typeid(*this)) |
| 170 | #define T_GENERIC_PROTOCOL(template_class, generic_prot, specific_prot) \ |
| 171 | do { \ |
| 172 | if (!(specific_prot)) { \ |
| 173 | ::apache::thrift::profile_generic_protocol( \ |
| 174 | typeid(*template_class), typeid(*generic_prot)); \ |
| 175 | } \ |
| 176 | } while (0) |
| 177 | #elif T_GLOBAL_DEBUG_VIRTUAL == 1 |
| 178 | #define T_VIRTUAL_CALL() \ |
David Reiss | e879c2f | 2010-10-06 17:09:50 +0000 | [diff] [blame] | 179 | fprintf(stderr,"[%s,%d] virtual call\n", __FILE__, __LINE__) |
David Reiss | c3b3622 | 2010-10-06 17:10:10 +0000 | [diff] [blame] | 180 | #define T_GENERIC_PROTOCOL(template_class, generic_prot, specific_prot) \ |
| 181 | do { \ |
| 182 | if (!(specific_prot)) { \ |
| 183 | fprintf(stderr, \ |
| 184 | "[%s,%d] failed to cast to specific protocol type\n", \ |
| 185 | __FILE__, __LINE__); \ |
| 186 | } \ |
| 187 | } while (0) |
David Reiss | e879c2f | 2010-10-06 17:09:50 +0000 | [diff] [blame] | 188 | #else |
| 189 | #define T_VIRTUAL_CALL() |
David Reiss | c3b3622 | 2010-10-06 17:10:10 +0000 | [diff] [blame] | 190 | #define T_GENERIC_PROTOCOL(template_class, generic_prot, specific_prot) |
David Reiss | e879c2f | 2010-10-06 17:09:50 +0000 | [diff] [blame] | 191 | #endif |
| 192 | |
David Reiss | 2375312 | 2007-08-27 19:57:34 +0000 | [diff] [blame] | 193 | #endif // #ifndef _THRIFT_TLOGGING_H_ |