1 // © 2019 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 
4 #ifndef __RESTRACE_H__
5 #define __RESTRACE_H__
6 
7 #include "unicode/utypes.h"
8 
9 #if U_ENABLE_TRACING
10 
11 struct UResourceBundle;
12 
13 U_NAMESPACE_BEGIN
14 
15 class CharString;
16 
17 /**
18  * Instances of this class store information used to trace reads from resource
19  * bundles when ICU is built with --enable-tracing.
20  *
21  * All arguments of type const UResourceBundle*, const char*, and
22  * const ResourceTracer& are stored as pointers. The caller must retain
23  * ownership for the lifetime of this ResourceTracer.
24  *
25  * Exported as U_COMMON_API for Windows because it is a value field
26  * in other exported types.
27  */
28 class U_COMMON_API ResourceTracer {
29 public:
ResourceTracer()30     ResourceTracer() :
31         fResB(nullptr),
32         fParent(nullptr),
33         fKey(nullptr),
34         fIndex(-1) {}
35 
ResourceTracer(const UResourceBundle * resB)36     ResourceTracer(const UResourceBundle* resB) :
37         fResB(resB),
38         fParent(nullptr),
39         fKey(nullptr),
40         fIndex(-1) {}
41 
ResourceTracer(const UResourceBundle * resB,const char * key)42     ResourceTracer(const UResourceBundle* resB, const char* key) :
43         fResB(resB),
44         fParent(nullptr),
45         fKey(key),
46         fIndex(-1) {}
47 
ResourceTracer(const UResourceBundle * resB,int32_t index)48     ResourceTracer(const UResourceBundle* resB, int32_t index) :
49         fResB(resB),
50         fParent(nullptr),
51         fKey(nullptr),
52         fIndex(index) {}
53 
ResourceTracer(const ResourceTracer & parent,const char * key)54     ResourceTracer(const ResourceTracer& parent, const char* key) :
55         fResB(nullptr),
56         fParent(&parent),
57         fKey(key),
58         fIndex(-1) {}
59 
ResourceTracer(const ResourceTracer & parent,int32_t index)60     ResourceTracer(const ResourceTracer& parent, int32_t index) :
61         fResB(nullptr),
62         fParent(&parent),
63         fKey(nullptr),
64         fIndex(index) {}
65 
66     ~ResourceTracer();
67 
68     void trace(const char* type) const;
69     void traceOpen() const;
70 
71     /**
72      * Calls trace() if the resB or parent provided to the constructor was
73      * non-null; otherwise, does nothing.
74      */
maybeTrace(const char * type)75     void maybeTrace(const char* type) const {
76         if (fResB || fParent) {
77             trace(type);
78         }
79     }
80 
81 private:
82     const UResourceBundle* fResB;
83     const ResourceTracer* fParent;
84     const char* fKey;
85     int32_t fIndex;
86 
87     CharString& getFilePath(CharString& output, UErrorCode& status) const;
88 
89     CharString& getResPath(CharString& output, UErrorCode& status) const;
90 };
91 
92 /**
93  * This class provides methods to trace data file reads when ICU is built
94  * with --enable-tracing.
95  */
96 class FileTracer {
97 public:
98     static void traceOpen(const char* path, const char* type, const char* name);
99 
100 private:
101     static void traceOpenDataFile(const char* path, const char* type, const char* name);
102     static void traceOpenResFile(const char* path, const char* name);
103 };
104 
105 U_NAMESPACE_END
106 
107 #else // U_ENABLE_TRACING
108 
109 U_NAMESPACE_BEGIN
110 
111 /**
112  * Default trivial implementation when --enable-tracing is not used.
113  */
114 class U_COMMON_API ResourceTracer {
115 public:
116     ResourceTracer() {}
117 
118     ResourceTracer(const void*) {}
119 
120     ResourceTracer(const void*, const char*) {}
121 
122     ResourceTracer(const void*, int32_t) {}
123 
124     ResourceTracer(const ResourceTracer&, const char*) {}
125 
126     ResourceTracer(const ResourceTracer&, int32_t) {}
127 
128     void trace(const char*) const {}
129 
130     void traceOpen() const {}
131 
132     void maybeTrace(const char*) const {}
133 };
134 
135 /**
136  * Default trivial implementation when --enable-tracing is not used.
137  */
138 class FileTracer {
139 public:
140     static void traceOpen(const char*, const char*, const char*) {}
141 };
142 
143 U_NAMESPACE_END
144 
145 #endif // U_ENABLE_TRACING
146 
147 #endif //__RESTRACE_H__
148