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-2013, International Business Machines
7*   Corporation and others.  All Rights Reserved.
8*
9*******************************************************************************
10*   file name:  utrace.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*   Definitions for ICU tracing/logging.
19*
20*/
21
22#ifndef __UTRACE_H__
23#define __UTRACE_H__
24
25#include <stdarg.h>
26#include "unicode/utypes.h"
27
28/**
29 * \file
30 * \brief C API:  Definitions for ICU tracing/logging.
31 *
32 * This provides API for debugging the internals of ICU without the use of
33 * a traditional debugger.
34 *
35 * By default, tracing is disabled in ICU. If you need to debug ICU with
36 * tracing, please compile ICU with the --enable-tracing configure option.
37 */
38
39U_CDECL_BEGIN
40
41/**
42 * Trace severity levels.  Higher levels increase the verbosity of the trace output.
43 * @see utrace_setLevel
44 * @stable ICU 2.8
45 */
46typedef enum UTraceLevel {
47    /** Disable all tracing  @stable ICU 2.8*/
48    UTRACE_OFF=-1,
49    /** Trace error conditions only  @stable ICU 2.8*/
50    UTRACE_ERROR=0,
51    /** Trace errors and warnings  @stable ICU 2.8*/
52    UTRACE_WARNING=3,
53    /** Trace opens and closes of ICU services  @stable ICU 2.8*/
54    UTRACE_OPEN_CLOSE=5,
55    /** Trace an intermediate number of ICU operations  @stable ICU 2.8*/
56    UTRACE_INFO=7,
57    /** Trace the maximum number of ICU operations  @stable ICU 2.8*/
58    UTRACE_VERBOSE=9
59} UTraceLevel;
60
61/**
62 *  These are the ICU functions that will be traced when tracing is enabled.
63 *  @stable ICU 2.8
64 */
65typedef enum UTraceFunctionNumber {
66    UTRACE_FUNCTION_START=0,
67    UTRACE_U_INIT=UTRACE_FUNCTION_START,
68    UTRACE_U_CLEANUP,
69#ifndef U_HIDE_DEPRECATED_API
70    /**
71     * One more than the highest normal collation trace location.
72     * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
73     */
74    UTRACE_FUNCTION_LIMIT,
75#endif  // U_HIDE_DEPRECATED_API
76
77    UTRACE_CONVERSION_START=0x1000,
78    UTRACE_UCNV_OPEN=UTRACE_CONVERSION_START,
79    UTRACE_UCNV_OPEN_PACKAGE,
80    UTRACE_UCNV_OPEN_ALGORITHMIC,
81    UTRACE_UCNV_CLONE,
82    UTRACE_UCNV_CLOSE,
83    UTRACE_UCNV_FLUSH_CACHE,
84    UTRACE_UCNV_LOAD,
85    UTRACE_UCNV_UNLOAD,
86#ifndef U_HIDE_DEPRECATED_API
87    /**
88     * One more than the highest normal collation trace location.
89     * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
90     */
91    UTRACE_CONVERSION_LIMIT,
92#endif  // U_HIDE_DEPRECATED_API
93
94    UTRACE_COLLATION_START=0x2000,
95    UTRACE_UCOL_OPEN=UTRACE_COLLATION_START,
96    UTRACE_UCOL_CLOSE,
97    UTRACE_UCOL_STRCOLL,
98    UTRACE_UCOL_GET_SORTKEY,
99    UTRACE_UCOL_GETLOCALE,
100    UTRACE_UCOL_NEXTSORTKEYPART,
101    UTRACE_UCOL_STRCOLLITER,
102    UTRACE_UCOL_OPEN_FROM_SHORT_STRING,
103    UTRACE_UCOL_STRCOLLUTF8, /**< @stable ICU 50 */
104#ifndef U_HIDE_DEPRECATED_API
105    /**
106     * One more than the highest normal collation trace location.
107     * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
108     */
109    UTRACE_COLLATION_LIMIT
110#endif  // U_HIDE_DEPRECATED_API
111} UTraceFunctionNumber;
112
113/**
114 * Setter for the trace level.
115 * @param traceLevel A UTraceLevel value.
116 * @stable ICU 2.8
117 */
118U_STABLE void U_EXPORT2
119utrace_setLevel(int32_t traceLevel);
120
121/**
122 * Getter for the trace level.
123 * @return The UTraceLevel value being used by ICU.
124 * @stable ICU 2.8
125 */
126U_STABLE int32_t U_EXPORT2
127utrace_getLevel(void);
128
129/* Trace function pointers types  ----------------------------- */
130
131/**
132  *  Type signature for the trace function to be called when entering a function.
133  *  @param context value supplied at the time the trace functions are set.
134  *  @param fnNumber Enum value indicating the ICU function being entered.
135  *  @stable ICU 2.8
136  */
137typedef void U_CALLCONV
138UTraceEntry(const void *context, int32_t fnNumber);
139
140/**
141  *  Type signature for the trace function to be called when exiting from a function.
142  *  @param context value supplied at the time the trace functions are set.
143  *  @param fnNumber Enum value indicating the ICU function being exited.
144  *  @param fmt     A formatting string that describes the number and types
145  *                 of arguments included with the variable args.  The fmt
146  *                 string has the same form as the utrace_vformat format
147  *                 string.
148  *  @param args    A variable arguments list.  Contents are described by
149  *                 the fmt parameter.
150  *  @see   utrace_vformat
151  *  @stable ICU 2.8
152  */
153typedef void U_CALLCONV
154UTraceExit(const void *context, int32_t fnNumber,
155           const char *fmt, va_list args);
156
157/**
158  *  Type signature for the trace function to be called from within an ICU function
159  *  to display data or messages.
160  *  @param context  value supplied at the time the trace functions are set.
161  *  @param fnNumber Enum value indicating the ICU function being exited.
162  *  @param level    The current tracing level
163  *  @param fmt      A format string describing the tracing data that is supplied
164  *                  as variable args
165  *  @param args     The data being traced, passed as variable args.
166  *  @stable ICU 2.8
167  */
168typedef void U_CALLCONV
169UTraceData(const void *context, int32_t fnNumber, int32_t level,
170           const char *fmt, va_list args);
171
172/**
173  *  Set ICU Tracing functions.  Installs application-provided tracing
174  *  functions into ICU.  After doing this, subsequent ICU operations
175  *  will call back to the installed functions, providing a trace
176  *  of the use of ICU.  Passing a NULL pointer for a tracing function
177  *  is allowed, and inhibits tracing action at points where that function
178  *  would be called.
179  *  <p>
180  *  Tracing and Threads:  Tracing functions are global to a process, and
181  *  will be called in response to ICU operations performed by any
182  *  thread.  If tracing of an individual thread is desired, the
183  *  tracing functions must themselves filter by checking that the
184  *  current thread is the desired thread.
185  *
186  *  @param context an uninterpretted pointer.  Whatever is passed in
187  *                 here will in turn be passed to each of the tracing
188  *                 functions UTraceEntry, UTraceExit and UTraceData.
189  *                 ICU does not use or alter this pointer.
190  *  @param e       Callback function to be called on entry to a
191  *                 a traced ICU function.
192  *  @param x       Callback function to be called on exit from a
193  *                 traced ICU function.
194  *  @param d       Callback function to be called from within a
195  *                 traced ICU function, for the purpose of providing
196  *                 data to the trace.
197  *
198  *  @stable ICU 2.8
199  */
200U_STABLE void U_EXPORT2
201utrace_setFunctions(const void *context,
202                    UTraceEntry *e, UTraceExit *x, UTraceData *d);
203
204/**
205  * Get the currently installed ICU tracing functions.   Note that a null function
206  *   pointer will be returned if no trace function has been set.
207  *
208  * @param context  The currently installed tracing context.
209  * @param e        The currently installed UTraceEntry function.
210  * @param x        The currently installed UTraceExit function.
211  * @param d        The currently installed UTraceData function.
212  * @stable ICU 2.8
213  */
214U_STABLE void U_EXPORT2
215utrace_getFunctions(const void **context,
216                    UTraceEntry **e, UTraceExit **x, UTraceData **d);
217
218
219
220/*
221 *
222 * ICU trace format string syntax
223 *
224 * Format Strings are passed to UTraceData functions, and define the
225 * number and types of the trace data being passed on each call.
226 *
227 * The UTraceData function, which is supplied by the application,
228 * not by ICU, can either forward the trace data (passed via
229 * varargs) and the format string back to ICU for formatting into
230 * a displayable string, or it can interpret the format itself,
231 * and do as it wishes with the trace data.
232 *
233 *
234 * Goals for the format string
235 * - basic data output
236 * - easy to use for trace programmer
237 * - sufficient provision for data types for trace output readability
238 * - well-defined types and binary portable APIs
239 *
240 * Non-goals
241 * - printf compatibility
242 * - fancy formatting
243 * - argument reordering and other internationalization features
244 *
245 * ICU trace format strings contain plain text with argument inserts,
246 * much like standard printf format strings.
247 * Each insert begins with a '%', then optionally contains a 'v',
248 * then exactly one type character.
249 * Two '%' in a row represent a '%' instead of an insert.
250 * The trace format strings need not have \n at the end.
251 *
252 *
253 * Types
254 * -----
255 *
256 * Type characters:
257 * - c A char character in the default codepage.
258 * - s A NUL-terminated char * string in the default codepage.
259 * - S A UChar * string.  Requires two params, (ptr, length).  Length=-1 for nul term.
260 * - b A byte (8-bit integer).
261 * - h A 16-bit integer.  Also a 16 bit Unicode code unit.
262 * - d A 32-bit integer.  Also a 20 bit Unicode code point value.
263 * - l A 64-bit integer.
264 * - p A data pointer.
265 *
266 * Vectors
267 * -------
268 *
269 * If the 'v' is not specified, then one item of the specified type
270 * is passed in.
271 * If the 'v' (for "vector") is specified, then a vector of items of the
272 * specified type is passed in, via a pointer to the first item
273 * and an int32_t value for the length of the vector.
274 * Length==-1 means zero or NUL termination.  Works for vectors of all types.
275 *
276 * Note:  %vS is a vector of (UChar *) strings.  The strings must
277 *        be nul terminated as there is no way to provide a
278 *        separate length parameter for each string.  The length
279 *        parameter (required for all vectors) is the number of
280 *        strings, not the length of the strings.
281 *
282 * Examples
283 * --------
284 *
285 * These examples show the parameters that will be passed to an application's
286 *   UTraceData() function for various formats.
287 *
288 * - the precise formatting is up to the application!
289 * - the examples use type casts for arguments only to _show_ the types of
290 *   arguments without needing variable declarations in the examples;
291 *   the type casts will not be necessary in actual code
292 *
293 * UTraceDataFunc(context, fnNumber, level,
294 *              "There is a character %c in the string %s.",   // Format String
295 *              (char)c, (const char *)s);                     // varargs parameters
296 * ->   There is a character 0x42 'B' in the string "Bravo".
297 *
298 * UTraceDataFunc(context, fnNumber, level,
299 *              "Vector of bytes %vb vector of chars %vc",
300 *              (const uint8_t *)bytes, (int32_t)bytesLength,
301 *              (const char *)chars, (int32_t)charsLength);
302 * ->  Vector of bytes
303 *      42 63 64 3f [4]
304 *     vector of chars
305 *      "Bcd?"[4]
306 *
307 * UTraceDataFunc(context, fnNumber, level,
308 *              "An int32_t %d and a whole bunch of them %vd",
309 *              (int32_t)-5, (const int32_t *)ints, (int32_t)intsLength);
310 * ->   An int32_t 0xfffffffb and a whole bunch of them
311 *      fffffffb 00000005 0000010a [3]
312 *
313 */
314
315
316
317/**
318  *  Trace output Formatter.  An application's UTraceData tracing functions may call
319  *                 back to this function to format the trace output in a
320  *                 human readable form.  Note that a UTraceData function may choose
321  *                 to not format the data;  it could, for example, save it in
322  *                 in the raw form it was received (more compact), leaving
323  *                 formatting for a later trace analyis tool.
324  *  @param outBuf  pointer to a buffer to receive the formatted output.  Output
325  *                 will be nul terminated if there is space in the buffer -
326  *                 if the length of the requested output < the output buffer size.
327  *  @param capacity  Length of the output buffer.
328  *  @param indent  Number of spaces to indent the output.  Intended to allow
329  *                 data displayed from nested functions to be indented for readability.
330  *  @param fmt     Format specification for the data to output
331  *  @param args    Data to be formatted.
332  *  @return        Length of formatted output, including the terminating NUL.
333  *                 If buffer capacity is insufficient, the required capacity is returned.
334  *  @stable ICU 2.8
335  */
336U_STABLE int32_t U_EXPORT2
337utrace_vformat(char *outBuf, int32_t capacity,
338              int32_t indent, const char *fmt,  va_list args);
339
340/**
341  *  Trace output Formatter.  An application's UTraceData tracing functions may call
342  *                 this function to format any additional trace data, beyond that
343  *                 provided by default, in human readable form with the same
344  *                 formatting conventions used by utrace_vformat().
345  *  @param outBuf  pointer to a buffer to receive the formatted output.  Output
346  *                 will be nul terminated if there is space in the buffer -
347  *                 if the length of the requested output < the output buffer size.
348  *  @param capacity  Length of the output buffer.
349  *  @param indent  Number of spaces to indent the output.  Intended to allow
350  *                 data displayed from nested functions to be indented for readability.
351  *  @param fmt     Format specification for the data to output
352  *  @param ...     Data to be formatted.
353  *  @return        Length of formatted output, including the terminating NUL.
354  *                 If buffer capacity is insufficient, the required capacity is returned.
355  *  @stable ICU 2.8
356  */
357U_STABLE int32_t U_EXPORT2
358utrace_format(char *outBuf, int32_t capacity,
359              int32_t indent, const char *fmt,  ...);
360
361
362
363/* Trace function numbers --------------------------------------------------- */
364
365/**
366 * Get the name of a function from its trace function number.
367 *
368 * @param fnNumber The trace number for an ICU function.
369 * @return The name string for the function.
370 *
371 * @see UTraceFunctionNumber
372 * @stable ICU 2.8
373 */
374U_STABLE const char * U_EXPORT2
375utrace_functionName(int32_t fnNumber);
376
377U_CDECL_END
378
379#endif
380