loader_logger.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright (c) 2017-2022, The Khronos Group Inc.
  2. // Copyright (c) 2017-2019 Valve Corporation
  3. // Copyright (c) 2017-2019 LunarG, Inc.
  4. //
  5. // SPDX-License-Identifier: Apache-2.0 OR MIT
  6. //
  7. // Initial Author: Mark Young <[email protected]>
  8. //
  9. #pragma once
  10. #include <memory>
  11. #include <mutex>
  12. #include <string>
  13. #include <unordered_map>
  14. #include <unordered_set>
  15. #include <vector>
  16. #include <set>
  17. #include <map>
  18. #include <shared_mutex>
  19. #include <openxr/openxr.h>
  20. #include "hex_and_handles.h"
  21. #include "object_info.h"
  22. // Use internal versions of flags similar to XR_EXT_debug_utils so that
  23. // we're not tightly coupled to that extension. This way, if the extension
  24. // changes or gets replaced, we can be flexible in the loader.
  25. #define XR_LOADER_LOG_MESSAGE_SEVERITY_VERBOSE_BIT 0x00000001
  26. #define XR_LOADER_LOG_MESSAGE_SEVERITY_INFO_BIT 0x00000010
  27. #define XR_LOADER_LOG_MESSAGE_SEVERITY_WARNING_BIT 0x00000100
  28. #define XR_LOADER_LOG_MESSAGE_SEVERITY_ERROR_BIT 0x00001000
  29. #define XR_LOADER_LOG_MESSAGE_SEVERITY_DEFAULT_BITS 0x00000000
  30. typedef XrFlags64 XrLoaderLogMessageSeverityFlagBits;
  31. typedef XrFlags64 XrLoaderLogMessageSeverityFlags;
  32. #define XR_LOADER_LOG_MESSAGE_TYPE_GENERAL_BIT 0x00000001
  33. #define XR_LOADER_LOG_MESSAGE_TYPE_SPECIFICATION_BIT 0x00000002
  34. #define XR_LOADER_LOG_MESSAGE_TYPE_PERFORMANCE_BIT 0x00000004
  35. #define XR_LOADER_LOG_MESSAGE_TYPE_DEFAULT_BITS 0xffffffff
  36. typedef XrFlags64 XrLoaderLogMessageTypeFlagBits;
  37. typedef XrFlags64 XrLoaderLogMessageTypeFlags;
  38. struct XrLoaderLogMessengerCallbackData {
  39. const char* message_id;
  40. const char* command_name;
  41. const char* message;
  42. uint8_t object_count;
  43. XrSdkLogObjectInfo* objects;
  44. uint8_t session_labels_count;
  45. XrDebugUtilsLabelEXT* session_labels;
  46. };
  47. enum XrLoaderLogType {
  48. XR_LOADER_LOG_UNKNOWN = 0,
  49. XR_LOADER_LOG_STDERR,
  50. XR_LOADER_LOG_STDOUT,
  51. XR_LOADER_LOG_DEBUG_UTILS,
  52. XR_LOADER_LOG_DEBUGGER,
  53. XR_LOADER_LOG_LOGCAT,
  54. };
  55. class LoaderLogRecorder {
  56. public:
  57. LoaderLogRecorder(XrLoaderLogType type, void* user_data, XrLoaderLogMessageSeverityFlags message_severities,
  58. XrLoaderLogMessageTypeFlags message_types) {
  59. _active = false;
  60. _user_data = user_data;
  61. _type = type;
  62. _unique_id = 0;
  63. _message_severities = message_severities;
  64. _message_types = message_types;
  65. }
  66. virtual ~LoaderLogRecorder() = default;
  67. XrLoaderLogType Type() { return _type; }
  68. uint64_t UniqueId() { return _unique_id; }
  69. XrLoaderLogMessageSeverityFlags MessageSeverities() { return _message_severities; }
  70. XrLoaderLogMessageTypeFlags MessageTypes() { return _message_types; }
  71. virtual void Start() { _active = true; }
  72. bool IsPaused() { return _active; }
  73. virtual void Pause() { _active = false; }
  74. virtual void Resume() { _active = true; }
  75. virtual void Stop() { _active = false; }
  76. virtual bool LogMessage(XrLoaderLogMessageSeverityFlagBits message_severity, XrLoaderLogMessageTypeFlags message_type,
  77. const XrLoaderLogMessengerCallbackData* callback_data) = 0;
  78. // Extension-specific logging functions - defaults to do nothing.
  79. virtual bool LogDebugUtilsMessage(XrDebugUtilsMessageSeverityFlagsEXT message_severity,
  80. XrDebugUtilsMessageTypeFlagsEXT message_type,
  81. const XrDebugUtilsMessengerCallbackDataEXT* callback_data);
  82. protected:
  83. bool _active;
  84. XrLoaderLogType _type;
  85. uint64_t _unique_id;
  86. void* _user_data;
  87. XrLoaderLogMessageSeverityFlags _message_severities;
  88. XrLoaderLogMessageTypeFlags _message_types;
  89. };
  90. class LoaderLogger {
  91. public:
  92. static LoaderLogger& GetInstance() {
  93. static LoaderLogger instance;
  94. return instance;
  95. }
  96. void AddLogRecorder(std::unique_ptr<LoaderLogRecorder>&& recorder);
  97. void RemoveLogRecorder(uint64_t unique_id);
  98. void AddLogRecorderForXrInstance(XrInstance instance, std::unique_ptr<LoaderLogRecorder>&& recorder);
  99. void RemoveLogRecordersForXrInstance(XrInstance instance);
  100. //! Called from LoaderXrTermSetDebugUtilsObjectNameEXT - an empty name means remove
  101. void AddObjectName(uint64_t object_handle, XrObjectType object_type, const std::string& object_name);
  102. void BeginLabelRegion(XrSession session, const XrDebugUtilsLabelEXT* label_info);
  103. void EndLabelRegion(XrSession session);
  104. void InsertLabel(XrSession session, const XrDebugUtilsLabelEXT* label_info);
  105. void DeleteSessionLabels(XrSession session);
  106. bool LogMessage(XrLoaderLogMessageSeverityFlagBits message_severity, XrLoaderLogMessageTypeFlags message_type,
  107. const std::string& message_id, const std::string& command_name, const std::string& message,
  108. const std::vector<XrSdkLogObjectInfo>& objects = {});
  109. static bool LogErrorMessage(const std::string& command_name, const std::string& message,
  110. const std::vector<XrSdkLogObjectInfo>& objects = {}) {
  111. return GetInstance().LogMessage(XR_LOADER_LOG_MESSAGE_SEVERITY_ERROR_BIT, XR_LOADER_LOG_MESSAGE_TYPE_GENERAL_BIT,
  112. "OpenXR-Loader", command_name, message, objects);
  113. }
  114. static bool LogWarningMessage(const std::string& command_name, const std::string& message,
  115. const std::vector<XrSdkLogObjectInfo>& objects = {}) {
  116. return GetInstance().LogMessage(XR_LOADER_LOG_MESSAGE_SEVERITY_WARNING_BIT, XR_LOADER_LOG_MESSAGE_TYPE_GENERAL_BIT,
  117. "OpenXR-Loader", command_name, message, objects);
  118. }
  119. static bool LogInfoMessage(const std::string& command_name, const std::string& message,
  120. const std::vector<XrSdkLogObjectInfo>& objects = {}) {
  121. return GetInstance().LogMessage(XR_LOADER_LOG_MESSAGE_SEVERITY_INFO_BIT, XR_LOADER_LOG_MESSAGE_TYPE_GENERAL_BIT,
  122. "OpenXR-Loader", command_name, message, objects);
  123. }
  124. static bool LogVerboseMessage(const std::string& command_name, const std::string& message,
  125. const std::vector<XrSdkLogObjectInfo>& objects = {}) {
  126. return GetInstance().LogMessage(XR_LOADER_LOG_MESSAGE_SEVERITY_VERBOSE_BIT, XR_LOADER_LOG_MESSAGE_TYPE_GENERAL_BIT,
  127. "OpenXR-Loader", command_name, message, objects);
  128. }
  129. static bool LogValidationErrorMessage(const std::string& vuid, const std::string& command_name, const std::string& message,
  130. const std::vector<XrSdkLogObjectInfo>& objects = {}) {
  131. return GetInstance().LogMessage(XR_LOADER_LOG_MESSAGE_SEVERITY_ERROR_BIT, XR_LOADER_LOG_MESSAGE_TYPE_SPECIFICATION_BIT,
  132. vuid, command_name, message, objects);
  133. }
  134. static bool LogValidationWarningMessage(const std::string& vuid, const std::string& command_name, const std::string& message,
  135. const std::vector<XrSdkLogObjectInfo>& objects = {}) {
  136. return GetInstance().LogMessage(XR_LOADER_LOG_MESSAGE_SEVERITY_WARNING_BIT, XR_LOADER_LOG_MESSAGE_TYPE_SPECIFICATION_BIT,
  137. vuid, command_name, message, objects);
  138. }
  139. // Extension-specific logging functions
  140. bool LogDebugUtilsMessage(XrDebugUtilsMessageSeverityFlagsEXT message_severity, XrDebugUtilsMessageTypeFlagsEXT message_type,
  141. const XrDebugUtilsMessengerCallbackDataEXT* callback_data);
  142. // Non-copyable
  143. LoaderLogger(const LoaderLogger&) = delete;
  144. LoaderLogger& operator=(const LoaderLogger&) = delete;
  145. private:
  146. LoaderLogger();
  147. std::shared_timed_mutex _mutex;
  148. // List of *all* available recorder objects (including created specifically for an Instance)
  149. std::vector<std::unique_ptr<LoaderLogRecorder>> _recorders;
  150. // List of recorder objects only created specifically for an XrInstance
  151. std::unordered_map<XrInstance, std::unordered_set<uint64_t>> _recordersByInstance;
  152. DebugUtilsData data_;
  153. };
  154. // Utility functions for converting to/from XR_EXT_debug_utils values
  155. XrLoaderLogMessageSeverityFlags DebugUtilsSeveritiesToLoaderLogMessageSeverities(
  156. XrDebugUtilsMessageSeverityFlagsEXT utils_severities);
  157. XrDebugUtilsMessageSeverityFlagsEXT LoaderLogMessageSeveritiesToDebugUtilsMessageSeverities(
  158. XrLoaderLogMessageSeverityFlags log_severities);
  159. XrLoaderLogMessageTypeFlagBits DebugUtilsMessageTypesToLoaderLogMessageTypes(XrDebugUtilsMessageTypeFlagsEXT utils_types);
  160. XrDebugUtilsMessageTypeFlagsEXT LoaderLogMessageTypesToDebugUtilsMessageTypes(XrLoaderLogMessageTypeFlagBits log_types);