xref: /aosp_15_r20/external/cronet/third_party/icu/source/common/restrace.cpp (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 
trace(const char * resType) const20 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 
traceOpen() const47 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 
getFilePath(CharString & output,UErrorCode & status) const59 CharString& ResourceTracer::getFilePath(CharString& output, UErrorCode& status) const {
60     if (fResB) {
61         // Note: if you get a segfault around here, check that ResourceTable and
62         // ResourceArray instances outlive ResourceValue instances referring to
63         // their contents:
64         output.append(fResB->fData->fPath, status);
65         output.append('/', status);
66         output.append(fResB->fData->fName, status);
67         output.append(".res", status);
68     } else {
69         fParent->getFilePath(output, status);
70     }
71     return output;
72 }
73 
getResPath(CharString & output,UErrorCode & status) const74 CharString& ResourceTracer::getResPath(CharString& output, UErrorCode& status) const {
75     if (fResB) {
76         output.append('/', status);
77         output.append(fResB->fResPath, status);
78         // removing the trailing /
79         U_ASSERT(output[output.length()-1] == '/');
80         output.truncate(output.length()-1);
81     } else {
82         fParent->getResPath(output, status);
83     }
84     if (fKey) {
85         output.append('/', status);
86         output.append(fKey, status);
87     }
88     if (fIndex != -1) {
89         output.append('[', status);
90         UnicodeString indexString;
91         ICU_Utility::appendNumber(indexString, fIndex);
92         output.appendInvariantChars(indexString, status);
93         output.append(']', status);
94     }
95     return output;
96 }
97 
traceOpen(const char * path,const char * type,const char * name)98 void FileTracer::traceOpen(const char* path, const char* type, const char* name) {
99     if (uprv_strcmp(type, "res") == 0) {
100         traceOpenResFile(path, name);
101     } else {
102         traceOpenDataFile(path, type, name);
103     }
104 }
105 
traceOpenDataFile(const char * path,const char * type,const char * name)106 void FileTracer::traceOpenDataFile(const char* path, const char* type, const char* name) {
107     UTRACE_ENTRY(UTRACE_UDATA_DATA_FILE);
108     UErrorCode status = U_ZERO_ERROR;
109 
110     CharString filePath;
111     filePath.append(path, status);
112     filePath.append('/', status);
113     filePath.append(name, status);
114     filePath.append('.', status);
115     filePath.append(type, status);
116 
117     UTRACE_DATA1(UTRACE_VERBOSE, "%s", filePath.data());
118     UTRACE_EXIT_STATUS(status);
119 }
120 
traceOpenResFile(const char * path,const char * name)121 void FileTracer::traceOpenResFile(const char* path, const char* name) {
122 #if U_ENABLE_RESOURCE_TRACING
123     UTRACE_ENTRY(UTRACE_UDATA_RES_FILE);
124     UErrorCode status = U_ZERO_ERROR;
125 
126     CharString filePath;
127     filePath.append(path, status);
128     filePath.append('/', status);
129     filePath.append(name, status);
130     filePath.append(".res", status);
131 
132     UTRACE_DATA1(UTRACE_VERBOSE, "%s", filePath.data());
133     UTRACE_EXIT_STATUS(status);
134 #endif  // U_ENABLE_RESOURCE_TRACING
135 }
136 
137 U_NAMESPACE_END
138 
139 #endif // U_ENABLE_TRACING
140