restrace.cpp 3.7 KB

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