| 1 | // © 2019 and later: Unicode, Inc. and others. |
| 2 | // License & terms of use: http://www.unicode.org/copyright.html |
| 3 | |
| 4 | #include "unicode/utypes.h" |
| 5 | |
| 6 | #if U_ENABLE_TRACING |
| 7 | |
| 8 | #include "restrace.h" |
| 9 | #include "charstr.h" |
| 10 | #include "cstring.h" |
| 11 | #include "utracimp.h" |
| 12 | #include "uresimp.h" |
| 13 | #include "uassert.h" |
| 14 | #include "util.h" |
| 15 | |
| 16 | U_NAMESPACE_BEGIN |
| 17 | |
| 18 | ResourceTracer::~ResourceTracer() = default; |
| 19 | |
| 20 | void ResourceTracer::trace(const char* resType) const { |
| 21 | #if U_ENABLE_RESOURCE_TRACING |
| 22 | U_ASSERT(fResB || fParent); |
| 23 | UTRACE_ENTRY(UTRACE_UDATA_RESOURCE); |
| 24 | UErrorCode status = U_ZERO_ERROR; |
| 25 | |
| 26 | CharString filePath; |
| 27 | getFilePath(filePath, status); |
| 28 | |
| 29 | CharString resPath; |
| 30 | getResPath(resPath, status); |
| 31 | |
| 32 | // The longest type ("intvector") is 9 chars |
| 33 | const char kSpaces[] = " " ; |
| 34 | CharString format; |
| 35 | format.append(kSpaces, sizeof(kSpaces) - 1 - uprv_strlen(resType), status); |
| 36 | format.append("(%s) %s @ %s" , status); |
| 37 | |
| 38 | UTRACE_DATA3(UTRACE_VERBOSE, |
| 39 | format.data(), |
| 40 | resType, |
| 41 | filePath.data(), |
| 42 | resPath.data()); |
| 43 | UTRACE_EXIT_STATUS(status); |
| 44 | #endif // U_ENABLE_RESOURCE_TRACING |
| 45 | } |
| 46 | |
| 47 | void ResourceTracer::traceOpen() const { |
| 48 | #if U_ENABLE_RESOURCE_TRACING |
| 49 | U_ASSERT(fResB); |
| 50 | UTRACE_ENTRY(UTRACE_UDATA_BUNDLE); |
| 51 | UErrorCode status = U_ZERO_ERROR; |
| 52 | |
| 53 | CharString filePath; |
| 54 | UTRACE_DATA1(UTRACE_VERBOSE, "%s" , getFilePath(filePath, status).data()); |
| 55 | UTRACE_EXIT_STATUS(status); |
| 56 | #endif // U_ENABLE_RESOURCE_TRACING |
| 57 | } |
| 58 | |
| 59 | CharString& ResourceTracer::getFilePath(CharString& output, UErrorCode& status) const { |
| 60 | if (fResB) { |
| 61 | output.append(fResB->fData->fPath, status); |
| 62 | output.append('/', status); |
| 63 | output.append(fResB->fData->fName, status); |
| 64 | output.append(".res" , status); |
| 65 | } else { |
| 66 | fParent->getFilePath(output, status); |
| 67 | } |
| 68 | return output; |
| 69 | } |
| 70 | |
| 71 | CharString& ResourceTracer::getResPath(CharString& output, UErrorCode& status) const { |
| 72 | if (fResB) { |
| 73 | output.append('/', status); |
| 74 | output.append(fResB->fResPath, status); |
| 75 | // removing the trailing / |
| 76 | U_ASSERT(output[output.length()-1] == '/'); |
| 77 | output.truncate(output.length()-1); |
| 78 | } else { |
| 79 | fParent->getResPath(output, status); |
| 80 | } |
| 81 | if (fKey) { |
| 82 | output.append('/', status); |
| 83 | output.append(fKey, status); |
| 84 | } |
| 85 | if (fIndex != -1) { |
| 86 | output.append('[', status); |
| 87 | UnicodeString indexString; |
| 88 | ICU_Utility::appendNumber(indexString, fIndex); |
| 89 | output.appendInvariantChars(indexString, status); |
| 90 | output.append(']', status); |
| 91 | } |
| 92 | return output; |
| 93 | } |
| 94 | |
| 95 | void FileTracer::traceOpen(const char* path, const char* type, const char* name) { |
| 96 | if (uprv_strcmp(type, "res" ) == 0) { |
| 97 | traceOpenResFile(path, name); |
| 98 | } else { |
| 99 | traceOpenDataFile(path, type, name); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void FileTracer::traceOpenDataFile(const char* path, const char* type, const char* name) { |
| 104 | UTRACE_ENTRY(UTRACE_UDATA_DATA_FILE); |
| 105 | UErrorCode status = U_ZERO_ERROR; |
| 106 | |
| 107 | CharString filePath; |
| 108 | filePath.append(path, status); |
| 109 | filePath.append('/', status); |
| 110 | filePath.append(name, status); |
| 111 | filePath.append('.', status); |
| 112 | filePath.append(type, status); |
| 113 | |
| 114 | UTRACE_DATA1(UTRACE_VERBOSE, "%s" , filePath.data()); |
| 115 | UTRACE_EXIT_STATUS(status); |
| 116 | } |
| 117 | |
| 118 | void FileTracer::traceOpenResFile(const char* path, const char* name) { |
| 119 | #if U_ENABLE_RESOURCE_TRACING |
| 120 | UTRACE_ENTRY(UTRACE_UDATA_RES_FILE); |
| 121 | UErrorCode status = U_ZERO_ERROR; |
| 122 | |
| 123 | CharString filePath; |
| 124 | filePath.append(path, status); |
| 125 | filePath.append('/', status); |
| 126 | filePath.append(name, status); |
| 127 | filePath.append(".res" , status); |
| 128 | |
| 129 | UTRACE_DATA1(UTRACE_VERBOSE, "%s" , filePath.data()); |
| 130 | UTRACE_EXIT_STATUS(status); |
| 131 | #endif // U_ENABLE_RESOURCE_TRACING |
| 132 | } |
| 133 | |
| 134 | U_NAMESPACE_END |
| 135 | |
| 136 | #endif // U_ENABLE_TRACING |
| 137 | |