ICU-2403 Misc tracing tweaks. Add cleanup func.

X-SVN-Rev: 13207
This commit is contained in:
Andy Heninger 2003-09-25 17:58:08 +00:00
parent 7b7c351c69
commit e873651a72
6 changed files with 45 additions and 19 deletions

View file

@ -16,12 +16,14 @@
#include "unicode/utypes.h"
#include "unicode/uclean.h"
#include "unicode/utrace.h"
#include "ustr_imp.h"
#include "unormimp.h"
#include "ucln_cmn.h"
#include "umutex.h"
#include "ucln.h"
#include "cmemory.h"
#include "uassert.h"
static UBool gICUInitialized = FALSE;
static UMTX gICUInitMutex;
@ -39,6 +41,7 @@ U_CAPI void U_EXPORT2
ucln_registerCleanup(ECleanupLibraryType type,
cleanupFunc *func)
{
U_ASSERT(UCLN_START < type && type < UCLN_COMMON);
if (UCLN_START < type && type < UCLN_COMMON)
{
gCleanupFunctions[type] = func;
@ -52,17 +55,17 @@ ucln_registerCleanup(ECleanupLibraryType type,
U_CAPI void U_EXPORT2
u_cleanup(void)
{
ECleanupLibraryType libType;
ECleanupLibraryType libType = UCLN_START;
while (++libType < UCLN_COMMON)
{
UTRACE_ENTRY(UTRACE_U_CLEANUP);
for (libType = UCLN_START+1; libType<UCLN_COMMON; libType++) {
if (gCleanupFunctions[libType])
{
gCleanupFunctions[libType]();
gCleanupFunctions[libType] = NULL;
}
}
#if !UCONFIG_NO_IDNA
usprep_cleanup();
#endif
@ -91,6 +94,8 @@ u_cleanup(void)
umtx_cleanup();
cmemory_cleanup(); /* undo any heap functions set by u_setMemoryFunctions(). */
gICUInitialized = FALSE;
UTRACE_EXIT(); /* Must be before utrace_cleanup(), which turns off tracing. */
utrace_cleanup();
}
@ -104,17 +109,16 @@ u_cleanup(void)
U_CAPI void U_EXPORT2
u_init(UErrorCode *status) {
UTRACE_ENTRY(UTRACE_U_INIT);
/* Make sure the global mutexes are initialized. */
umtx_init(NULL);
umtx_lock(&gICUInitMutex);
if (gICUInitialized || U_FAILURE(*status)) {
umtx_unlock(&gICUInitMutex);
UTRACE_EXIT_S(*status);
return;
}
ucnv_init(status);
ures_init(status);
/* Do any required init for services that don't have open operations
* and use "only" the double-check initialization method for performance
* reasons (avoiding a mutex lock even for _checking_ whether the
@ -130,6 +134,7 @@ u_init(UErrorCode *status) {
#endif
gICUInitialized = TRUE; /* TODO: don't set if U_FAILURE? */
umtx_unlock(&gICUInitMutex);
UTRACE_EXIT_S(*status);
}

View file

@ -55,6 +55,8 @@ U_CFUNC UBool cmemory_cleanup(void);
U_CFUNC UBool umtx_cleanup(void);
U_CFUNC UBool utrace_cleanup(void);
/* Only mutexes should be initialized in these functions. */
U_CFUNC void ucnv_init(UErrorCode *status);

View file

@ -70,6 +70,8 @@ typedef enum UTraceLevel UTraceLevel;
*/
#ifdef UTRACE_IMPL
U_EXPORT int32_t
#elif U_COMMON_IMPLEMENTATION
U_CFUNC int32_t
#else
U_CFUNC U_IMPORT int32_t
#endif
@ -432,7 +434,8 @@ utrace_setFunctions(const void *context,
* data displayed from nested functions to be indented for readability.
* @param fmt Format specification for the data to output
* @param args Data to be formatted.
* @return Length of formatted output, including the terminating NULL if present.
* @return Length of formatted output, including the terminating NULL.
* If buffer capacity is insufficient, the required capacity is returned.
*/
U_CAPI int32_t U_EXPORT2
utrace_format(char *outBuf, int32_t capacity,

View file

@ -18,7 +18,7 @@
static UTraceEntry *pTraceEntryFunc = NULL;
static UTraceExit *pTraceExitFunc = NULL;
static UTraceData *pTraceDataFunc = NULL;
static void *gTraceContext = NULL;
static const void *gTraceContext = NULL;
U_EXPORT int32_t
utrace_level;
@ -59,19 +59,21 @@ static void outputChar(char c, char *outBuf, int32_t *outIx, int32_t capacity, i
int32_t i;
if (*outIx < capacity) {
outBuf[*outIx] = c;
if (c != 0) {
/* Nulls only appear as end-of-string terminators. Move them to the output
* buffer, but do not update the length of the buffer, so that any
* following output will overwrite the null. */
(*outIx)++;
};
}
if (c != 0) {
/* Nulls only appear as end-of-string terminators. Move them to the output
* buffer, but do not update the length of the buffer, so that any
* following output will overwrite the null. */
(*outIx)++;
}
/* Handle indenting at the start of lines */
if (c == '\n') {
for(i=0; i<indent; i++) {
if (*outIx < capacity) {
outBuf[*outIx] = ' ';
(*outIx)++;
}
(*outIx)++;
}
}
}
@ -288,8 +290,8 @@ utrace_format(char *outBuf, int32_t capacity, int32_t indent, const char *fmt, v
outputChar(fmtC, outBuf, &outIx, capacity, indent);
}
}
outputChar(0, outBuf, &outIx, capacity, indent);
return outIx;
outputChar(0, outBuf, &outIx, capacity, indent); /* Make sure that output is null terminated */
return outIx + 1; /* outIx + 1 because outIx does not increment when outputing final null. */
}
@ -349,8 +351,21 @@ utrace_setFunctions(const void *context,
pTraceExitFunc = x;
pTraceDataFunc = d;
utrace_level = traceLevel;
gTraceContext = context;
}
U_CFUNC UBool
utrace_cleanup() {
pTraceEntryFunc = NULL;
pTraceExitFunc = NULL;
pTraceDataFunc = NULL;
utrace_level = UTRACE_OFF;
gTraceContext = NULL;
return TRUE;
}
static const char * const
trFnName[] = {"u_init",
"u_cleanup",

View file

@ -474,7 +474,7 @@ ucol_setReqValidLocales(UCollator *coll, char *requestedLocaleToAdopt, char *val
U_CAPI void U_EXPORT2
ucol_close(UCollator *coll)
{
UTRACE_ENTRY(UTRACE_UCNV_CLOSE);
UTRACE_ENTRY(UTRACE_UCOL_CLOSE);
UTRACE_DATA1(UTRACE_INFO, "coll = %p", coll);
if(coll != NULL) {
// these are always owned by each UCollator struct,

View file

@ -192,6 +192,7 @@ int main(int argc, const char* const argv[])
}
u_cleanup();
errorCode = U_ZERO_ERROR;
utrace_setFunctions(NULL, TraceEntry, TraceExit, TraceData, ICU_TRACE, &errorCode);
/* Initialize ICU */
ctest_setICU_DATA(); /* u_setDataDirectory() must happen Before u_init() */