| 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 | */ |
| 19 | |
| 20 | #ifndef _THRIFT_TLOGGING_H_ |
| 21 | #define _THRIFT_TLOGGING_H_ 1 |
| 22 | |
| 23 | #include <thrift/thrift-config.h> |
| 24 | |
| 25 | /** |
| 26 | * Contains utility macros for debugging and logging. |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | #include <time.h> |
| 31 | |
| 32 | #ifdef HAVE_STDINT_H |
| 33 | #include <stdint.h> |
| 34 | #endif |
| 35 | |
| 36 | /** |
| 37 | * T_GLOBAL_DEBUGGING_LEVEL = 0: all debugging turned off, debug macros undefined |
| 38 | * T_GLOBAL_DEBUGGING_LEVEL = 1: all debugging turned on |
| 39 | */ |
| 40 | #define T_GLOBAL_DEBUGGING_LEVEL 0 |
| 41 | |
| 42 | /** |
| 43 | * T_GLOBAL_LOGGING_LEVEL = 0: all logging turned off, logging macros undefined |
| 44 | * T_GLOBAL_LOGGING_LEVEL = 1: all logging turned on |
| 45 | */ |
| 46 | #define T_GLOBAL_LOGGING_LEVEL 0 |
| 47 | |
| 48 | /** |
| 49 | * Standard wrapper around fprintf what will prefix the file name and line |
| 50 | * number to the line. Uses T_GLOBAL_DEBUGGING_LEVEL to control whether it is |
| 51 | * turned on or off. |
| 52 | * |
| 53 | * @param format_string |
| 54 | */ |
| 55 | #if T_GLOBAL_DEBUGGING_LEVEL > 0 |
| 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__); \ |
| 59 | } |
| 60 | #else |
| 61 | #define T_DEBUG(format_string, ...) |
| 62 | #endif |
| 63 | |
| 64 | /** |
| 65 | * analogous to T_DEBUG but also prints the time |
| 66 | * |
| 67 | * @param string format_string input: printf style format string |
| 68 | */ |
| 69 | #if T_GLOBAL_DEBUGGING_LEVEL > 0 |
| 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 | } |
| 86 | #else |
| 87 | #define T_DEBUG_T(format_string, ...) |
| 88 | #endif |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | /** |
| 95 | * Log input message |
| 96 | * |
| 97 | * @param string format_string input: printf style format string |
| 98 | */ |
| 99 | #if T_GLOBAL_LOGGING_LEVEL > 0 |
| 100 | #define T_LOG_OPER(format_string, ...) \ |
| 101 | { \ |
| 102 | if (T_GLOBAL_LOGGING_LEVEL > 0) { \ |
| 103 | time_t now; \ |
| 104 | char dbgtime[26]; \ |
| 105 | time(&now); \ |
| 106 | THRIFT_CTIME_R(&now, dbgtime); \ |
| 107 | dbgtime[24] = '\0'; \ |
| 108 | fprintf(stderr, "[%s] " format_string " \n", dbgtime, ##__VA_ARGS__); \ |
| 109 | } \ |
| 110 | } |
| 111 | #else |
| 112 | #define T_LOG_OPER(format_string, ...) |
| 113 | #endif |
| 114 | |
| 115 | /** |
| 116 | * T_GLOBAL_DEBUG_VIRTUAL = 0 or unset: normal operation, |
| 117 | * virtual call debug messages disabled |
| 118 | * T_GLOBAL_DEBUG_VIRTUAL = 1: log a debug messages whenever an |
| 119 | * avoidable virtual call is made |
| 120 | * T_GLOBAL_DEBUG_VIRTUAL = 2: record detailed info that can be |
| 121 | * printed by calling |
| 122 | * apache::thrift::profile_print_info() |
| 123 | */ |
| 124 | #if T_GLOBAL_DEBUG_VIRTUAL > 1 |
| 125 | #define T_VIRTUAL_CALL() ::apache::thrift::profile_virtual_call(typeid(*this)) |
| 126 | #define T_GENERIC_PROTOCOL(template_class, generic_prot, specific_prot) \ |
| 127 | do { \ |
| 128 | if (!(specific_prot)) { \ |
| 129 | ::apache::thrift::profile_generic_protocol(typeid(*template_class), typeid(*generic_prot)); \ |
| 130 | } \ |
| 131 | } while (0) |
| 132 | #elif T_GLOBAL_DEBUG_VIRTUAL == 1 |
| 133 | #define T_VIRTUAL_CALL() fprintf(stderr, "[%s,%d] virtual call\n", __FILE__, __LINE__) |
| 134 | #define T_GENERIC_PROTOCOL(template_class, generic_prot, specific_prot) \ |
| 135 | do { \ |
| 136 | if (!(specific_prot)) { \ |
| 137 | fprintf(stderr, "[%s,%d] failed to cast to specific protocol type\n", __FILE__, __LINE__); \ |
| 138 | } \ |
| 139 | } while (0) |
| 140 | #else |
| 141 | #define T_VIRTUAL_CALL() |
| 142 | #define T_GENERIC_PROTOCOL(template_class, generic_prot, specific_prot) |
| 143 | #endif |
| 144 | |
| 145 | #endif // #ifndef _THRIFT_TLOGGING_H_ |
| 146 | |