restrace.h 3.4 KB

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