utracimp.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. *
  6. * Copyright (C) 2003-2009, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. *******************************************************************************
  10. * file name: utracimp.h
  11. * encoding: UTF-8
  12. * tab size: 8 (not used)
  13. * indentation:4
  14. *
  15. * created on: 2003aug06
  16. * created by: Markus W. Scherer
  17. *
  18. * Internal header for ICU tracing/logging.
  19. *
  20. *
  21. * Various notes:
  22. * - using a trace level variable to only call trace functions
  23. * when the level is sufficient
  24. * - using the same variable for tracing on/off to never make a function
  25. * call when off
  26. * - the function number is put into a local variable by the entry macro
  27. * and used implicitly to avoid copy&paste/typing mistakes by the developer
  28. * - the application must call utrace_setFunctions() and pass in
  29. * implementations for the trace functions
  30. * - ICU trace macros call ICU functions that route through the function
  31. * pointers if they have been set;
  32. * this avoids an indirection at the call site
  33. * (which would cost more code for another check and for the indirection)
  34. *
  35. * ### TODO Issues:
  36. * - Verify that va_list is portable among compilers for the same platform.
  37. * va_list should be portable because printf() would fail otherwise!
  38. * - Should enum values like UTraceLevel be passed into int32_t-type arguments,
  39. * or should enum types be used?
  40. */
  41. #ifndef __UTRACIMP_H__
  42. #define __UTRACIMP_H__
  43. #include "unicode/utrace.h"
  44. #include <stdarg.h>
  45. U_CDECL_BEGIN
  46. /**
  47. * Traced Function Exit return types.
  48. * Flags indicating the number and types of varargs included in a call
  49. * to a UTraceExit function.
  50. * Bits 0-3: The function return type. First variable param.
  51. * Bit 4: Flag for presence of U_ErrorCode status param.
  52. * @internal
  53. */
  54. typedef enum UTraceExitVal {
  55. /** The traced function returns no value @internal */
  56. UTRACE_EXITV_NONE = 0,
  57. /** The traced function returns an int32_t, or compatible, type. @internal */
  58. UTRACE_EXITV_I32 = 1,
  59. /** The traced function returns a pointer @internal */
  60. UTRACE_EXITV_PTR = 2,
  61. /** The traced function returns a UBool @internal */
  62. UTRACE_EXITV_BOOL = 3,
  63. /** Mask to extract the return type values from a UTraceExitVal @internal */
  64. UTRACE_EXITV_MASK = 0xf,
  65. /** Bit indicating that the traced function includes a UErrorCode parameter @internal */
  66. UTRACE_EXITV_STATUS = 0x10
  67. } UTraceExitVal;
  68. /**
  69. * Trace function for the entry point of a function.
  70. * Do not use directly, use UTRACE_ENTRY instead.
  71. * @param fnNumber The UTraceFunctionNumber for the current function.
  72. * @internal
  73. */
  74. U_CAPI void U_EXPORT2
  75. utrace_entry(int32_t fnNumber);
  76. /**
  77. * Trace function for each exit point of a function.
  78. * Do not use directly, use UTRACE_EXIT* instead.
  79. * @param fnNumber The UTraceFunctionNumber for the current function.
  80. * @param returnType The type of the value returned by the function.
  81. * @param errorCode The UErrorCode value at function exit. See UTRACE_EXIT.
  82. * @internal
  83. */
  84. U_CAPI void U_EXPORT2
  85. utrace_exit(int32_t fnNumber, int32_t returnType, ...);
  86. /**
  87. * Trace function used inside functions that have a UTRACE_ENTRY() statement.
  88. * Do not use directly, use UTRACE_DATAX() macros instead.
  89. *
  90. * @param utraceFnNumber The number of the current function, from the local
  91. * variable of the same name.
  92. * @param level The trace level for this message.
  93. * @param fmt The trace format string.
  94. *
  95. * @internal
  96. */
  97. U_CAPI void U_EXPORT2
  98. utrace_data(int32_t utraceFnNumber, int32_t level, const char *fmt, ...);
  99. U_CDECL_END
  100. #if U_ENABLE_TRACING
  101. /**
  102. * Boolean expression to see if ICU tracing is turned on
  103. * to at least the specified level.
  104. * @internal
  105. */
  106. #define UTRACE_LEVEL(level) (utrace_getLevel()>=(level))
  107. /**
  108. * Flag bit in utraceFnNumber, the local variable added to each function
  109. * with tracing code to contains the function number.
  110. *
  111. * Set the flag if the function's entry is traced, which will cause the
  112. * function's exit to also be traced. utraceFnNumber is uncoditionally
  113. * set at entry, whether or not the entry is traced, so that it will
  114. * always be available for error trace output.
  115. * @internal
  116. */
  117. #define UTRACE_TRACED_ENTRY 0x80000000
  118. /**
  119. * Trace statement for the entry point of a function.
  120. * Stores the function number in a local variable.
  121. * In C code, must be placed immediately after the last variable declaration.
  122. * Must be matched with UTRACE_EXIT() at all function exit points.
  123. *
  124. * Tracing should start with UTRACE_ENTRY after checking for
  125. * U_FAILURE at function entry, so that if a function returns immediately
  126. * because of a pre-existing error condition, it does not show up in the trace,
  127. * consistent with ICU's error handling model.
  128. *
  129. * @param fnNumber The UTraceFunctionNumber for the current function.
  130. * @internal
  131. */
  132. #define UTRACE_ENTRY(fnNumber) \
  133. int32_t utraceFnNumber=(fnNumber); \
  134. UPRV_BLOCK_MACRO_BEGIN { \
  135. if(utrace_getLevel()>=UTRACE_INFO) { \
  136. utrace_entry(fnNumber); \
  137. utraceFnNumber |= UTRACE_TRACED_ENTRY; \
  138. } \
  139. } UPRV_BLOCK_MACRO_END
  140. /**
  141. * Trace statement for the entry point of open and close functions.
  142. * Produces trace output at a less verbose setting than plain UTRACE_ENTRY
  143. * Stores the function number in a local variable.
  144. * In C code, must be placed immediately after the last variable declaration.
  145. * Must be matched with UTRACE_EXIT() at all function exit points.
  146. *
  147. * @param fnNumber The UTraceFunctionNumber for the current function.
  148. * @internal
  149. */
  150. #define UTRACE_ENTRY_OC(fnNumber) \
  151. int32_t utraceFnNumber=(fnNumber); \
  152. UPRV_BLOCK_MACRO_BEGIN { \
  153. if(utrace_getLevel()>=UTRACE_OPEN_CLOSE) { \
  154. utrace_entry(fnNumber); \
  155. utraceFnNumber |= UTRACE_TRACED_ENTRY; \
  156. } \
  157. } UPRV_BLOCK_MACRO_END
  158. /**
  159. * Trace statement for each exit point of a function that has a UTRACE_ENTRY()
  160. * statement.
  161. *
  162. * @param errorCode The function's ICU UErrorCode value at function exit,
  163. * or U_ZERO_ERROR if the function does not use a UErrorCode.
  164. * 0==U_ZERO_ERROR indicates success,
  165. * positive values an error (see u_errorName()),
  166. * negative values an informational status.
  167. *
  168. * @internal
  169. */
  170. #define UTRACE_EXIT() UPRV_BLOCK_MACRO_BEGIN { \
  171. if(utraceFnNumber & UTRACE_TRACED_ENTRY) { \
  172. utrace_exit(utraceFnNumber & ~UTRACE_TRACED_ENTRY, UTRACE_EXITV_NONE); \
  173. } \
  174. } UPRV_BLOCK_MACRO_END
  175. /**
  176. * Trace statement for each exit point of a function that has a UTRACE_ENTRY()
  177. * statement, and that returns a value.
  178. *
  179. * @param val The function's return value, int32_t or compatible type.
  180. *
  181. * @internal
  182. */
  183. #define UTRACE_EXIT_VALUE(val) UPRV_BLOCK_MACRO_BEGIN { \
  184. if(utraceFnNumber & UTRACE_TRACED_ENTRY) { \
  185. utrace_exit(utraceFnNumber & ~UTRACE_TRACED_ENTRY, UTRACE_EXITV_I32, val); \
  186. } \
  187. } UPRV_BLOCK_MACRO_END
  188. #define UTRACE_EXIT_STATUS(status) UPRV_BLOCK_MACRO_BEGIN { \
  189. if(utraceFnNumber & UTRACE_TRACED_ENTRY) { \
  190. utrace_exit(utraceFnNumber & ~UTRACE_TRACED_ENTRY, UTRACE_EXITV_STATUS, status); \
  191. } \
  192. } UPRV_BLOCK_MACRO_END
  193. #define UTRACE_EXIT_VALUE_STATUS(val, status) UPRV_BLOCK_MACRO_BEGIN { \
  194. if(utraceFnNumber & UTRACE_TRACED_ENTRY) { \
  195. utrace_exit(utraceFnNumber & ~UTRACE_TRACED_ENTRY, (UTRACE_EXITV_I32 | UTRACE_EXITV_STATUS), val, status); \
  196. } \
  197. } UPRV_BLOCK_MACRO_END
  198. #define UTRACE_EXIT_PTR_STATUS(ptr, status) UPRV_BLOCK_MACRO_BEGIN { \
  199. if(utraceFnNumber & UTRACE_TRACED_ENTRY) { \
  200. utrace_exit(utraceFnNumber & ~UTRACE_TRACED_ENTRY, (UTRACE_EXITV_PTR | UTRACE_EXITV_STATUS), ptr, status); \
  201. } \
  202. } UPRV_BLOCK_MACRO_END
  203. /**
  204. * Trace statement used inside functions that have a UTRACE_ENTRY() statement.
  205. * Takes no data arguments.
  206. * The number of arguments for this macro must match the number of inserts
  207. * in the format string. Vector inserts count as two arguments.
  208. * Calls utrace_data() if the level is high enough.
  209. * @internal
  210. */
  211. #define UTRACE_DATA0(level, fmt) UPRV_BLOCK_MACRO_BEGIN { \
  212. if(UTRACE_LEVEL(level)) { \
  213. utrace_data(utraceFnNumber & ~UTRACE_TRACED_ENTRY, (level), (fmt)); \
  214. } \
  215. } UPRV_BLOCK_MACRO_END
  216. /**
  217. * Trace statement used inside functions that have a UTRACE_ENTRY() statement.
  218. * Takes one data argument.
  219. * The number of arguments for this macro must match the number of inserts
  220. * in the format string. Vector inserts count as two arguments.
  221. * Calls utrace_data() if the level is high enough.
  222. * @internal
  223. */
  224. #define UTRACE_DATA1(level, fmt, a) UPRV_BLOCK_MACRO_BEGIN { \
  225. if(UTRACE_LEVEL(level)) { \
  226. utrace_data(utraceFnNumber & ~UTRACE_TRACED_ENTRY , (level), (fmt), (a)); \
  227. } \
  228. } UPRV_BLOCK_MACRO_END
  229. /**
  230. * Trace statement used inside functions that have a UTRACE_ENTRY() statement.
  231. * Takes two data arguments.
  232. * The number of arguments for this macro must match the number of inserts
  233. * in the format string. Vector inserts count as two arguments.
  234. * Calls utrace_data() if the level is high enough.
  235. * @internal
  236. */
  237. #define UTRACE_DATA2(level, fmt, a, b) UPRV_BLOCK_MACRO_BEGIN { \
  238. if(UTRACE_LEVEL(level)) { \
  239. utrace_data(utraceFnNumber & ~UTRACE_TRACED_ENTRY , (level), (fmt), (a), (b)); \
  240. } \
  241. } UPRV_BLOCK_MACRO_END
  242. /**
  243. * Trace statement used inside functions that have a UTRACE_ENTRY() statement.
  244. * Takes three data arguments.
  245. * The number of arguments for this macro must match the number of inserts
  246. * in the format string. Vector inserts count as two arguments.
  247. * Calls utrace_data() if the level is high enough.
  248. * @internal
  249. */
  250. #define UTRACE_DATA3(level, fmt, a, b, c) UPRV_BLOCK_MACRO_BEGIN { \
  251. if(UTRACE_LEVEL(level)) { \
  252. utrace_data(utraceFnNumber & ~UTRACE_TRACED_ENTRY, (level), (fmt), (a), (b), (c)); \
  253. } \
  254. } UPRV_BLOCK_MACRO_END
  255. /**
  256. * Trace statement used inside functions that have a UTRACE_ENTRY() statement.
  257. * Takes four data arguments.
  258. * The number of arguments for this macro must match the number of inserts
  259. * in the format string. Vector inserts count as two arguments.
  260. * Calls utrace_data() if the level is high enough.
  261. * @internal
  262. */
  263. #define UTRACE_DATA4(level, fmt, a, b, c, d) UPRV_BLOCK_MACRO_BEGIN { \
  264. if(UTRACE_LEVEL(level)) { \
  265. utrace_data(utraceFnNumber & ~UTRACE_TRACED_ENTRY, (level), (fmt), (a), (b), (c), (d)); \
  266. } \
  267. } UPRV_BLOCK_MACRO_END
  268. /**
  269. * Trace statement used inside functions that have a UTRACE_ENTRY() statement.
  270. * Takes five data arguments.
  271. * The number of arguments for this macro must match the number of inserts
  272. * in the format string. Vector inserts count as two arguments.
  273. * Calls utrace_data() if the level is high enough.
  274. * @internal
  275. */
  276. #define UTRACE_DATA5(level, fmt, a, b, c, d, e) UPRV_BLOCK_MACRO_BEGIN { \
  277. if(UTRACE_LEVEL(level)) { \
  278. utrace_data(utraceFnNumber & ~UTRACE_TRACED_ENTRY, (level), (fmt), (a), (b), (c), (d), (e)); \
  279. } \
  280. } UPRV_BLOCK_MACRO_END
  281. /**
  282. * Trace statement used inside functions that have a UTRACE_ENTRY() statement.
  283. * Takes six data arguments.
  284. * The number of arguments for this macro must match the number of inserts
  285. * in the format string. Vector inserts count as two arguments.
  286. * Calls utrace_data() if the level is high enough.
  287. * @internal
  288. */
  289. #define UTRACE_DATA6(level, fmt, a, b, c, d, e, f) UPRV_BLOCK_MACRO_BEGIN { \
  290. if(UTRACE_LEVEL(level)) { \
  291. utrace_data(utraceFnNumber & ~UTRACE_TRACED_ENTRY, (level), (fmt), (a), (b), (c), (d), (e), (f)); \
  292. } \
  293. } UPRV_BLOCK_MACRO_END
  294. /**
  295. * Trace statement used inside functions that have a UTRACE_ENTRY() statement.
  296. * Takes seven data arguments.
  297. * The number of arguments for this macro must match the number of inserts
  298. * in the format string. Vector inserts count as two arguments.
  299. * Calls utrace_data() if the level is high enough.
  300. * @internal
  301. */
  302. #define UTRACE_DATA7(level, fmt, a, b, c, d, e, f, g) UPRV_BLOCK_MACRO_BEGIN { \
  303. if(UTRACE_LEVEL(level)) { \
  304. utrace_data(utraceFnNumber & ~UTRACE_TRACED_ENTRY, (level), (fmt), (a), (b), (c), (d), (e), (f), (g)); \
  305. } \
  306. } UPRV_BLOCK_MACRO_END
  307. /**
  308. * Trace statement used inside functions that have a UTRACE_ENTRY() statement.
  309. * Takes eight data arguments.
  310. * The number of arguments for this macro must match the number of inserts
  311. * in the format string. Vector inserts count as two arguments.
  312. * Calls utrace_data() if the level is high enough.
  313. * @internal
  314. */
  315. #define UTRACE_DATA8(level, fmt, a, b, c, d, e, f, g, h) UPRV_BLOCK_MACRO_BEGIN { \
  316. if(UTRACE_LEVEL(level)) { \
  317. utrace_data(utraceFnNumber & ~UTRACE_TRACED_ENTRY, (level), (fmt), (a), (b), (c), (d), (e), (f), (g), (h)); \
  318. } \
  319. } UPRV_BLOCK_MACRO_END
  320. /**
  321. * Trace statement used inside functions that have a UTRACE_ENTRY() statement.
  322. * Takes nine data arguments.
  323. * The number of arguments for this macro must match the number of inserts
  324. * in the format string. Vector inserts count as two arguments.
  325. * Calls utrace_data() if the level is high enough.
  326. * @internal
  327. */
  328. #define UTRACE_DATA9(level, fmt, a, b, c, d, e, f, g, h, i) UPRV_BLOCK_MACRO_BEGIN { \
  329. if(UTRACE_LEVEL(level)) { \
  330. utrace_data(utraceFnNumber & ~UTRACE_TRACED_ENTRY, (level), (fmt), (a), (b), (c), (d), (e), (f), (g), (h), (i)); \
  331. } \
  332. } UPRV_BLOCK_MACRO_END
  333. #else
  334. /*
  335. * When tracing is disabled, the following macros become empty
  336. */
  337. #define UTRACE_LEVEL(level) 0
  338. #define UTRACE_ENTRY(fnNumber)
  339. #define UTRACE_ENTRY_OC(fnNumber)
  340. #define UTRACE_EXIT()
  341. #define UTRACE_EXIT_VALUE(val)
  342. #define UTRACE_EXIT_STATUS(status)
  343. #define UTRACE_EXIT_VALUE_STATUS(val, status)
  344. #define UTRACE_EXIT_PTR_STATUS(ptr, status)
  345. #define UTRACE_DATA0(level, fmt)
  346. #define UTRACE_DATA1(level, fmt, a)
  347. #define UTRACE_DATA2(level, fmt, a, b)
  348. #define UTRACE_DATA3(level, fmt, a, b, c)
  349. #define UTRACE_DATA4(level, fmt, a, b, c, d)
  350. #define UTRACE_DATA5(level, fmt, a, b, c, d, e)
  351. #define UTRACE_DATA6(level, fmt, a, b, c, d, e, f)
  352. #define UTRACE_DATA7(level, fmt, a, b, c, d, e, f, g)
  353. #define UTRACE_DATA8(level, fmt, a, b, c, d, e, f, g, h)
  354. #define UTRACE_DATA9(level, fmt, a, b, c, d, e, f, g, h, i)
  355. #endif
  356. #endif