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