| 1 | // Copyright (c) 2017-2023, 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 <marky@lunarg.com> |
| 8 | // |
| 9 | |
| 10 | #include "loader_logger.hpp" |
| 11 | |
| 12 | #include "extra_algorithms.h" |
| 13 | #include "hex_and_handles.h" |
| 14 | #include "loader_logger_recorders.hpp" |
| 15 | #include "platform_utils.hpp" |
| 16 | |
| 17 | #include <openxr/openxr.h> |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <iterator> |
| 21 | #include <memory> |
| 22 | #include <mutex> |
| 23 | #include <sstream> |
| 24 | #include <string> |
| 25 | #include <unordered_map> |
| 26 | #include <utility> |
| 27 | #include <vector> |
| 28 | |
| 29 | // For routing platform_utils.hpp messages into the LoaderLogger. |
| 30 | void LogPlatformUtilsError(const std::string& message) { LoaderLogger::LogErrorMessage("platform_utils" , message); } |
| 31 | |
| 32 | bool LoaderLogRecorder::LogDebugUtilsMessage(XrDebugUtilsMessageSeverityFlagsEXT /*message_severity*/, |
| 33 | XrDebugUtilsMessageTypeFlagsEXT /*message_type*/, |
| 34 | const XrDebugUtilsMessengerCallbackDataEXT* /*callback_data*/) { |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | // Utility functions for converting to/from XR_EXT_debug_utils values |
| 39 | |
| 40 | XrLoaderLogMessageSeverityFlags DebugUtilsSeveritiesToLoaderLogMessageSeverities( |
| 41 | XrDebugUtilsMessageSeverityFlagsEXT utils_severities) { |
| 42 | XrLoaderLogMessageSeverityFlags log_severities = 0UL; |
| 43 | if ((utils_severities & XR_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) != 0u) { |
| 44 | log_severities |= XR_LOADER_LOG_MESSAGE_SEVERITY_VERBOSE_BIT; |
| 45 | } |
| 46 | if ((utils_severities & XR_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) != 0u) { |
| 47 | log_severities |= XR_LOADER_LOG_MESSAGE_SEVERITY_INFO_BIT; |
| 48 | } |
| 49 | if ((utils_severities & XR_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) != 0u) { |
| 50 | log_severities |= XR_LOADER_LOG_MESSAGE_SEVERITY_WARNING_BIT; |
| 51 | } |
| 52 | if ((utils_severities & XR_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) != 0u) { |
| 53 | log_severities |= XR_LOADER_LOG_MESSAGE_SEVERITY_ERROR_BIT; |
| 54 | } |
| 55 | return log_severities; |
| 56 | } |
| 57 | |
| 58 | XrDebugUtilsMessageSeverityFlagsEXT LoaderLogMessageSeveritiesToDebugUtilsMessageSeverities( |
| 59 | XrLoaderLogMessageSeverityFlags log_severities) { |
| 60 | XrDebugUtilsMessageSeverityFlagsEXT utils_severities = 0UL; |
| 61 | if ((log_severities & XR_LOADER_LOG_MESSAGE_SEVERITY_VERBOSE_BIT) != 0u) { |
| 62 | utils_severities |= XR_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT; |
| 63 | } |
| 64 | if ((log_severities & XR_LOADER_LOG_MESSAGE_SEVERITY_INFO_BIT) != 0u) { |
| 65 | utils_severities |= XR_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT; |
| 66 | } |
| 67 | if ((log_severities & XR_LOADER_LOG_MESSAGE_SEVERITY_WARNING_BIT) != 0u) { |
| 68 | utils_severities |= XR_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT; |
| 69 | } |
| 70 | if ((log_severities & XR_LOADER_LOG_MESSAGE_SEVERITY_ERROR_BIT) != 0u) { |
| 71 | utils_severities |= XR_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT; |
| 72 | } |
| 73 | return utils_severities; |
| 74 | } |
| 75 | |
| 76 | XrLoaderLogMessageTypeFlagBits DebugUtilsMessageTypesToLoaderLogMessageTypes(XrDebugUtilsMessageTypeFlagsEXT utils_types) { |
| 77 | XrLoaderLogMessageTypeFlagBits log_types = 0UL; |
| 78 | if ((utils_types & XR_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT) != 0u) { |
| 79 | log_types |= XR_LOADER_LOG_MESSAGE_TYPE_GENERAL_BIT; |
| 80 | } |
| 81 | if ((utils_types & XR_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) != 0u) { |
| 82 | log_types |= XR_LOADER_LOG_MESSAGE_TYPE_SPECIFICATION_BIT; |
| 83 | } |
| 84 | if ((utils_types & XR_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) != 0u) { |
| 85 | log_types |= XR_LOADER_LOG_MESSAGE_TYPE_PERFORMANCE_BIT; |
| 86 | } |
| 87 | return log_types; |
| 88 | } |
| 89 | |
| 90 | XrDebugUtilsMessageTypeFlagsEXT LoaderLogMessageTypesToDebugUtilsMessageTypes(XrLoaderLogMessageTypeFlagBits log_types) { |
| 91 | XrDebugUtilsMessageTypeFlagsEXT utils_types = 0UL; |
| 92 | if ((log_types & XR_LOADER_LOG_MESSAGE_TYPE_GENERAL_BIT) != 0u) { |
| 93 | utils_types |= XR_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT; |
| 94 | } |
| 95 | if ((log_types & XR_LOADER_LOG_MESSAGE_TYPE_SPECIFICATION_BIT) != 0u) { |
| 96 | utils_types |= XR_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT; |
| 97 | } |
| 98 | if ((log_types & XR_LOADER_LOG_MESSAGE_TYPE_PERFORMANCE_BIT) != 0u) { |
| 99 | utils_types |= XR_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT; |
| 100 | } |
| 101 | return utils_types; |
| 102 | } |
| 103 | |
| 104 | LoaderLogger::LoaderLogger() { |
| 105 | std::string debug_string = PlatformUtilsGetEnv("XR_LOADER_DEBUG" ); |
| 106 | |
| 107 | // Add an error logger by default so that we at least get errors out to std::cerr. |
| 108 | // Normally we enable stderr output. But if the XR_LOADER_DEBUG environment variable is |
| 109 | // present as "none" then we don't. |
| 110 | if (debug_string != "none" ) { |
| 111 | AddLogRecorder(MakeStdErrLoaderLogRecorder(nullptr)); |
| 112 | #ifdef __ANDROID__ |
| 113 | // Add a logcat logger by default. |
| 114 | AddLogRecorder(MakeLogcatLoaderLogRecorder()); |
| 115 | #endif // __ANDROID__ |
| 116 | } |
| 117 | |
| 118 | #ifdef _WIN32 |
| 119 | // Add an debugger logger by default so that we at least get errors out to the debugger. |
| 120 | AddLogRecorder(MakeDebuggerLoaderLogRecorder(nullptr)); |
| 121 | #endif |
| 122 | |
| 123 | // If the environment variable to enable loader debugging is set, then enable the |
| 124 | // appropriate logging out to std::cout. |
| 125 | if (!debug_string.empty()) { |
| 126 | XrLoaderLogMessageSeverityFlags debug_flags = {}; |
| 127 | if (debug_string == "error" ) { |
| 128 | debug_flags = XR_LOADER_LOG_MESSAGE_SEVERITY_ERROR_BIT; |
| 129 | } else if (debug_string == "warn" ) { |
| 130 | debug_flags = XR_LOADER_LOG_MESSAGE_SEVERITY_ERROR_BIT | XR_LOADER_LOG_MESSAGE_SEVERITY_WARNING_BIT; |
| 131 | } else if (debug_string == "info" ) { |
| 132 | debug_flags = XR_LOADER_LOG_MESSAGE_SEVERITY_ERROR_BIT | XR_LOADER_LOG_MESSAGE_SEVERITY_WARNING_BIT | |
| 133 | XR_LOADER_LOG_MESSAGE_SEVERITY_INFO_BIT; |
| 134 | } else if (debug_string == "all" || debug_string == "verbose" ) { |
| 135 | debug_flags = XR_LOADER_LOG_MESSAGE_SEVERITY_ERROR_BIT | XR_LOADER_LOG_MESSAGE_SEVERITY_WARNING_BIT | |
| 136 | XR_LOADER_LOG_MESSAGE_SEVERITY_INFO_BIT | XR_LOADER_LOG_MESSAGE_SEVERITY_VERBOSE_BIT; |
| 137 | } |
| 138 | AddLogRecorder(MakeStdOutLoaderLogRecorder(nullptr, debug_flags)); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void LoaderLogger::AddLogRecorder(std::unique_ptr<LoaderLogRecorder>&& recorder) { |
| 143 | std::unique_lock<std::shared_timed_mutex> lock(_mutex); |
| 144 | _recorders.push_back(std::move(recorder)); |
| 145 | } |
| 146 | |
| 147 | void LoaderLogger::AddLogRecorderForXrInstance(XrInstance instance, std::unique_ptr<LoaderLogRecorder>&& recorder) { |
| 148 | std::unique_lock<std::shared_timed_mutex> lock(_mutex); |
| 149 | _recordersByInstance[instance].insert(recorder->UniqueId()); |
| 150 | _recorders.emplace_back(std::move(recorder)); |
| 151 | } |
| 152 | |
| 153 | void LoaderLogger::RemoveLogRecorder(uint64_t unique_id) { |
| 154 | std::unique_lock<std::shared_timed_mutex> lock(_mutex); |
| 155 | vector_remove_if_and_erase( |
| 156 | _recorders, [=](std::unique_ptr<LoaderLogRecorder> const& recorder) { return recorder->UniqueId() == unique_id; }); |
| 157 | for (auto& recorders : _recordersByInstance) { |
| 158 | auto& messengersForInstance = recorders.second; |
| 159 | if (messengersForInstance.count(unique_id) > 0) { |
| 160 | messengersForInstance.erase(unique_id); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void LoaderLogger::RemoveLogRecordersForXrInstance(XrInstance instance) { |
| 166 | std::unique_lock<std::shared_timed_mutex> lock(_mutex); |
| 167 | if (_recordersByInstance.find(instance) != _recordersByInstance.end()) { |
| 168 | auto recorders = _recordersByInstance[instance]; |
| 169 | vector_remove_if_and_erase(_recorders, [=](std::unique_ptr<LoaderLogRecorder> const& recorder) { |
| 170 | return recorders.find(recorder->UniqueId()) != recorders.end(); |
| 171 | }); |
| 172 | _recordersByInstance.erase(instance); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | bool LoaderLogger::LogMessage(XrLoaderLogMessageSeverityFlagBits message_severity, XrLoaderLogMessageTypeFlags message_type, |
| 177 | const std::string& message_id, const std::string& command_name, const std::string& message, |
| 178 | const std::vector<XrSdkLogObjectInfo>& objects) { |
| 179 | XrLoaderLogMessengerCallbackData callback_data = {}; |
| 180 | callback_data.message_id = message_id.c_str(); |
| 181 | callback_data.command_name = command_name.c_str(); |
| 182 | callback_data.message = message.c_str(); |
| 183 | |
| 184 | auto names_and_labels = data_.PopulateNamesAndLabels(objects); |
| 185 | callback_data.objects = names_and_labels.sdk_objects.empty() ? nullptr : names_and_labels.sdk_objects.data(); |
| 186 | callback_data.object_count = static_cast<uint8_t>(names_and_labels.objects.size()); |
| 187 | |
| 188 | callback_data.session_labels = names_and_labels.labels.empty() ? nullptr : names_and_labels.labels.data(); |
| 189 | callback_data.session_labels_count = static_cast<uint8_t>(names_and_labels.labels.size()); |
| 190 | |
| 191 | std::shared_lock<std::shared_timed_mutex> lock(_mutex); |
| 192 | bool exit_app = false; |
| 193 | for (std::unique_ptr<LoaderLogRecorder>& recorder : _recorders) { |
| 194 | if ((recorder->MessageSeverities() & message_severity) == message_severity && |
| 195 | (recorder->MessageTypes() & message_type) == message_type) { |
| 196 | exit_app |= recorder->LogMessage(message_severity, message_type, &callback_data); |
| 197 | } |
| 198 | } |
| 199 | return exit_app; |
| 200 | } |
| 201 | |
| 202 | // Extension-specific logging functions |
| 203 | bool LoaderLogger::LogDebugUtilsMessage(XrDebugUtilsMessageSeverityFlagsEXT message_severity, |
| 204 | XrDebugUtilsMessageTypeFlagsEXT message_type, |
| 205 | const XrDebugUtilsMessengerCallbackDataEXT* callback_data) { |
| 206 | bool exit_app = false; |
| 207 | XrLoaderLogMessageSeverityFlags log_message_severity = DebugUtilsSeveritiesToLoaderLogMessageSeverities(message_severity); |
| 208 | XrLoaderLogMessageTypeFlags log_message_type = DebugUtilsMessageTypesToLoaderLogMessageTypes(message_type); |
| 209 | |
| 210 | AugmentedCallbackData augmented_data; |
| 211 | data_.WrapCallbackData(&augmented_data, callback_data); |
| 212 | |
| 213 | // Loop through the recorders |
| 214 | std::shared_lock<std::shared_timed_mutex> lock(_mutex); |
| 215 | for (std::unique_ptr<LoaderLogRecorder>& recorder : _recorders) { |
| 216 | // Only send the message if it's a debug utils recorder and of the type the recorder cares about. |
| 217 | if (recorder->Type() != XR_LOADER_LOG_DEBUG_UTILS || |
| 218 | (recorder->MessageSeverities() & log_message_severity) != log_message_severity || |
| 219 | (recorder->MessageTypes() & log_message_type) != log_message_type) { |
| 220 | continue; |
| 221 | } |
| 222 | |
| 223 | exit_app |= recorder->LogDebugUtilsMessage(message_severity, message_type, augmented_data.exported_data); |
| 224 | } |
| 225 | return exit_app; |
| 226 | } |
| 227 | |
| 228 | void LoaderLogger::AddObjectName(uint64_t object_handle, XrObjectType object_type, const std::string& object_name) { |
| 229 | data_.AddObjectName(object_handle, object_type, object_name); |
| 230 | } |
| 231 | |
| 232 | void LoaderLogger::BeginLabelRegion(XrSession session, const XrDebugUtilsLabelEXT* label_info) { |
| 233 | data_.BeginLabelRegion(session, *label_info); |
| 234 | } |
| 235 | |
| 236 | void LoaderLogger::EndLabelRegion(XrSession session) { data_.EndLabelRegion(session); } |
| 237 | |
| 238 | void LoaderLogger::InsertLabel(XrSession session, const XrDebugUtilsLabelEXT* label_info) { |
| 239 | data_.InsertLabel(session, *label_info); |
| 240 | } |
| 241 | |
| 242 | void LoaderLogger::DeleteSessionLabels(XrSession session) { data_.DeleteSessionLabels(session); } |
| 243 | |