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     U_ASSERT(fResB || fParent);
22     UTRACE_ENTRY(UTRACE_UDATA_RESOURCE);
23     UErrorCode status = U_ZERO_ERROR;
24 
25     CharString filePath;
26     getFilePath(filePath, status);
27 
28     CharString resPath;
29     getResPath(resPath, status);
30 
31     // The longest type ("intvector") is 9 chars
32     const char kSpaces[] = "         ";
33     CharString format;
34     format.append(kSpaces, sizeof(kSpaces) - 1 - uprv_strlen(resType), status);
35     format.append("(%s) %s @ %s", status);
36 
37     UTRACE_DATA3(UTRACE_VERBOSE,
38         format.data(),
39         resType,
40         filePath.data(),
41         resPath.data());
42     UTRACE_EXIT_STATUS(status);
43 }
44 
traceOpen() const45 void ResourceTracer::traceOpen() const {
46     U_ASSERT(fResB);
47     UTRACE_ENTRY(UTRACE_UDATA_BUNDLE);
48     UErrorCode status = U_ZERO_ERROR;
49 
50     CharString filePath;
51     UTRACE_DATA1(UTRACE_VERBOSE, "%s", getFilePath(filePath, status).data());
52     UTRACE_EXIT_STATUS(status);
53 }
54 
getFilePath(CharString & output,UErrorCode & status) const55 CharString& ResourceTracer::getFilePath(CharString& output, UErrorCode& status) const {
56     if (fResB) {
57         // Note: if you get a segfault around here, check that ResourceTable and
58         // ResourceArray instances outlive ResourceValue instances referring to
59         // their contents:
60         output.append(fResB->fData->fPath, status);
61         output.append('/', status);
62         output.append(fResB->fData->fName, status);
63         output.append(".res", status);
64     } else {
65         fParent->getFilePath(output, status);
66     }
67     return output;
68 }
69 
getResPath(CharString & output,UErrorCode & status) const70 CharString& ResourceTracer::getResPath(CharString& output, UErrorCode& status) const {
71     if (fResB) {
72         output.append('/', status);
73         output.append(fResB->fResPath, status);
74         // removing the trailing /
75         U_ASSERT(output[output.length()-1] == '/');
76         output.truncate(output.length()-1);
77     } else {
78         fParent->getResPath(output, status);
79     }
80     if (fKey) {
81         output.append('/', status);
82         output.append(fKey, status);
83     }
84     if (fIndex != -1) {
85         output.append('[', status);
86         UnicodeString indexString;
87         ICU_Utility::appendNumber(indexString, fIndex);
88         output.appendInvariantChars(indexString, status);
89         output.append(']', status);
90     }
91     return output;
92 }
93 
traceOpen(const char * path,const char * type,const char * name)94 void FileTracer::traceOpen(const char* path, const char* type, const char* name) {
95     if (uprv_strcmp(type, "res") == 0) {
96         traceOpenResFile(path, name);
97     } else {
98         traceOpenDataFile(path, type, name);
99     }
100 }
101 
traceOpenDataFile(const char * path,const char * type,const char * name)102 void FileTracer::traceOpenDataFile(const char* path, const char* type, const char* name) {
103     UTRACE_ENTRY(UTRACE_UDATA_DATA_FILE);
104     UErrorCode status = U_ZERO_ERROR;
105 
106     CharString filePath;
107     filePath.append(path, status);
108     filePath.append('/', status);
109     filePath.append(name, status);
110     filePath.append('.', status);
111     filePath.append(type, status);
112 
113     UTRACE_DATA1(UTRACE_VERBOSE, "%s", filePath.data());
114     UTRACE_EXIT_STATUS(status);
115 }
116 
traceOpenResFile(const char * path,const char * name)117 void FileTracer::traceOpenResFile(const char* path, const char* name) {
118     UTRACE_ENTRY(UTRACE_UDATA_RES_FILE);
119     UErrorCode status = U_ZERO_ERROR;
120 
121     CharString filePath;
122     filePath.append(path, status);
123     filePath.append('/', status);
124     filePath.append(name, status);
125     filePath.append(".res", status);
126 
127     UTRACE_DATA1(UTRACE_VERBOSE, "%s", filePath.data());
128     UTRACE_EXIT_STATUS(status);
129 }
130 
131 U_NAMESPACE_END
132 
133 #endif // U_ENABLE_TRACING
134