TLogging.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #ifndef _THRIFT_TLOGGING_H_
  20. #define _THRIFT_TLOGGING_H_ 1
  21. #include <thrift/thrift-config.h>
  22. /**
  23. * Contains utility macros for debugging and logging.
  24. *
  25. */
  26. #include <time.h>
  27. #ifdef HAVE_STDINT_H
  28. #include <stdint.h>
  29. #endif
  30. /**
  31. * T_GLOBAL_DEBUGGING_LEVEL = 0: all debugging turned off, debug macros undefined
  32. * T_GLOBAL_DEBUGGING_LEVEL = 1: all debugging turned on
  33. */
  34. #define T_GLOBAL_DEBUGGING_LEVEL 0
  35. /**
  36. * T_GLOBAL_LOGGING_LEVEL = 0: all logging turned off, logging macros undefined
  37. * T_GLOBAL_LOGGING_LEVEL = 1: all logging turned on
  38. */
  39. #define T_GLOBAL_LOGGING_LEVEL 1
  40. /**
  41. * Standard wrapper around fprintf what will prefix the file name and line
  42. * number to the line. Uses T_GLOBAL_DEBUGGING_LEVEL to control whether it is
  43. * turned on or off.
  44. *
  45. * @param format_string
  46. */
  47. #if T_GLOBAL_DEBUGGING_LEVEL > 0
  48. #define T_DEBUG(format_string, ...) \
  49. if (T_GLOBAL_DEBUGGING_LEVEL > 0) { \
  50. fprintf(stderr, "[%s,%d] " format_string " \n", __FILE__, __LINE__, ##__VA_ARGS__); \
  51. }
  52. #else
  53. #define T_DEBUG(format_string, ...)
  54. #endif
  55. /**
  56. * analogous to T_DEBUG but also prints the time
  57. *
  58. * @param string format_string input: printf style format string
  59. */
  60. #if T_GLOBAL_DEBUGGING_LEVEL > 0
  61. #define T_DEBUG_T(format_string, ...) \
  62. { \
  63. if (T_GLOBAL_DEBUGGING_LEVEL > 0) { \
  64. time_t now; \
  65. char dbgtime[26]; \
  66. time(&now); \
  67. THRIFT_CTIME_R(&now, dbgtime); \
  68. dbgtime[24] = '\0'; \
  69. fprintf(stderr, \
  70. "[%s,%d] [%s] " format_string " \n", \
  71. __FILE__, \
  72. __LINE__, \
  73. dbgtime, \
  74. ##__VA_ARGS__); \
  75. } \
  76. }
  77. #else
  78. #define T_DEBUG_T(format_string, ...)
  79. #endif
  80. /**
  81. * analogous to T_DEBUG but uses input level to determine whether or not the string
  82. * should be logged.
  83. *
  84. * @param int level: specified debug level
  85. * @param string format_string input: format string
  86. */
  87. #define T_DEBUG_L(level, format_string, ...) \
  88. if ((level) > 0) { \
  89. fprintf(stderr, "[%s,%d] " format_string " \n", __FILE__, __LINE__, ##__VA_ARGS__); \
  90. }
  91. /**
  92. * Explicit error logging. Prints time, file name and line number
  93. *
  94. * @param string format_string input: printf style format string
  95. */
  96. #define T_ERROR(format_string, ...) \
  97. { \
  98. time_t now; \
  99. char dbgtime[26]; \
  100. time(&now); \
  101. THRIFT_CTIME_R(&now, dbgtime); \
  102. dbgtime[24] = '\0'; \
  103. fprintf(stderr, \
  104. "[%s,%d] [%s] ERROR: " format_string " \n", \
  105. __FILE__, \
  106. __LINE__, \
  107. dbgtime, \
  108. ##__VA_ARGS__); \
  109. }
  110. /**
  111. * Analogous to T_ERROR, additionally aborting the process.
  112. * WARNING: macro calls abort(), ending program execution
  113. *
  114. * @param string format_string input: printf style format string
  115. */
  116. #define T_ERROR_ABORT(format_string, ...) \
  117. { \
  118. time_t now; \
  119. char dbgtime[26]; \
  120. time(&now); \
  121. THRIFT_CTIME_R(&now, dbgtime); \
  122. dbgtime[24] = '\0'; \
  123. fprintf(stderr, \
  124. "[%s,%d] [%s] ERROR: Going to abort " format_string " \n", \
  125. __FILE__, \
  126. __LINE__, \
  127. dbgtime, \
  128. ##__VA_ARGS__); \
  129. exit(1); \
  130. }
  131. /**
  132. * Log input message
  133. *
  134. * @param string format_string input: printf style format string
  135. */
  136. #if T_GLOBAL_LOGGING_LEVEL > 0
  137. #define T_LOG_OPER(format_string, ...) \
  138. { \
  139. if (T_GLOBAL_LOGGING_LEVEL > 0) { \
  140. time_t now; \
  141. char dbgtime[26]; \
  142. time(&now); \
  143. THRIFT_CTIME_R(&now, dbgtime); \
  144. dbgtime[24] = '\0'; \
  145. fprintf(stderr, "[%s] " format_string " \n", dbgtime, ##__VA_ARGS__); \
  146. } \
  147. }
  148. #else
  149. #define T_LOG_OPER(format_string, ...)
  150. #endif
  151. /**
  152. * T_GLOBAL_DEBUG_VIRTUAL = 0 or unset: normal operation,
  153. * virtual call debug messages disabled
  154. * T_GLOBAL_DEBUG_VIRTUAL = 1: log a debug messages whenever an
  155. * avoidable virtual call is made
  156. * T_GLOBAL_DEBUG_VIRTUAL = 2: record detailed info that can be
  157. * printed by calling
  158. * apache::thrift::profile_print_info()
  159. */
  160. #if T_GLOBAL_DEBUG_VIRTUAL > 1
  161. #define T_VIRTUAL_CALL() ::apache::thrift::profile_virtual_call(typeid(*this))
  162. #define T_GENERIC_PROTOCOL(template_class, generic_prot, specific_prot) \
  163. do { \
  164. if (!(specific_prot)) { \
  165. ::apache::thrift::profile_generic_protocol(typeid(*template_class), typeid(*generic_prot)); \
  166. } \
  167. } while (0)
  168. #elif T_GLOBAL_DEBUG_VIRTUAL == 1
  169. #define T_VIRTUAL_CALL() fprintf(stderr, "[%s,%d] virtual call\n", __FILE__, __LINE__)
  170. #define T_GENERIC_PROTOCOL(template_class, generic_prot, specific_prot) \
  171. do { \
  172. if (!(specific_prot)) { \
  173. fprintf(stderr, "[%s,%d] failed to cast to specific protocol type\n", __FILE__, __LINE__); \
  174. } \
  175. } while (0)
  176. #else
  177. #define T_VIRTUAL_CALL()
  178. #define T_GENERIC_PROTOCOL(template_class, generic_prot, specific_prot)
  179. #endif
  180. #endif // #ifndef _THRIFT_TLOGGING_H_