Path: blob/master/thirdparty/openxr/src/loader/loader_logger.hpp
9917 views
// Copyright (c) 2017-2025 The Khronos Group Inc.1// Copyright (c) 2017-2019 Valve Corporation2// Copyright (c) 2017-2019 LunarG, Inc.3//4// SPDX-License-Identifier: Apache-2.0 OR MIT5//6// Initial Author: Mark Young <[email protected]>7//89#pragma once1011#include <memory>12#include <mutex>13#include <string>14#include <unordered_map>15#include <unordered_set>16#include <vector>17#include <set>18#include <map>19#include <shared_mutex>2021#include <openxr/openxr.h>2223#include "hex_and_handles.h"24#include "object_info.h"2526// Use internal versions of flags similar to XR_EXT_debug_utils so that27// we're not tightly coupled to that extension. This way, if the extension28// changes or gets replaced, we can be flexible in the loader.29#define XR_LOADER_LOG_MESSAGE_SEVERITY_VERBOSE_BIT 0x0000000130#define XR_LOADER_LOG_MESSAGE_SEVERITY_INFO_BIT 0x0000001031#define XR_LOADER_LOG_MESSAGE_SEVERITY_WARNING_BIT 0x0000010032#define XR_LOADER_LOG_MESSAGE_SEVERITY_ERROR_BIT 0x0000100033#define XR_LOADER_LOG_MESSAGE_SEVERITY_DEFAULT_BITS 0x0000000034typedef XrFlags64 XrLoaderLogMessageSeverityFlagBits;35typedef XrFlags64 XrLoaderLogMessageSeverityFlags;3637#define XR_LOADER_LOG_MESSAGE_TYPE_GENERAL_BIT 0x0000000138#define XR_LOADER_LOG_MESSAGE_TYPE_SPECIFICATION_BIT 0x0000000239#define XR_LOADER_LOG_MESSAGE_TYPE_PERFORMANCE_BIT 0x0000000440#define XR_LOADER_LOG_MESSAGE_TYPE_DEFAULT_BITS 0xffffffff41typedef XrFlags64 XrLoaderLogMessageTypeFlagBits;42typedef XrFlags64 XrLoaderLogMessageTypeFlags;4344struct XrLoaderLogMessengerCallbackData {45const char* message_id;46const char* command_name;47const char* message;48uint8_t object_count;49XrSdkLogObjectInfo* objects;50uint8_t session_labels_count;51XrDebugUtilsLabelEXT* session_labels;52};5354enum XrLoaderLogType {55XR_LOADER_LOG_UNKNOWN = 0,56XR_LOADER_LOG_STDERR,57XR_LOADER_LOG_STDOUT,58XR_LOADER_LOG_DEBUG_UTILS,59XR_LOADER_LOG_DEBUGGER,60XR_LOADER_LOG_LOGCAT,61};6263class LoaderLogRecorder {64public:65LoaderLogRecorder(XrLoaderLogType type, void* user_data, XrLoaderLogMessageSeverityFlags message_severities,66XrLoaderLogMessageTypeFlags message_types) {67_active = false;68_user_data = user_data;69_type = type;70_unique_id = 0;71_message_severities = message_severities;72_message_types = message_types;73}74virtual ~LoaderLogRecorder() = default;7576XrLoaderLogType Type() { return _type; }7778uint64_t UniqueId() { return _unique_id; }7980XrLoaderLogMessageSeverityFlags MessageSeverities() { return _message_severities; }8182XrLoaderLogMessageTypeFlags MessageTypes() { return _message_types; }8384virtual void Start() { _active = true; }8586bool IsPaused() { return _active; }8788virtual void Pause() { _active = false; }8990virtual void Resume() { _active = true; }9192virtual void Stop() { _active = false; }9394virtual bool LogMessage(XrLoaderLogMessageSeverityFlagBits message_severity, XrLoaderLogMessageTypeFlags message_type,95const XrLoaderLogMessengerCallbackData* callback_data) = 0;9697// Extension-specific logging functions - defaults to do nothing.98virtual bool LogDebugUtilsMessage(XrDebugUtilsMessageSeverityFlagsEXT message_severity,99XrDebugUtilsMessageTypeFlagsEXT message_type,100const XrDebugUtilsMessengerCallbackDataEXT* callback_data);101102protected:103bool _active;104XrLoaderLogType _type;105uint64_t _unique_id;106void* _user_data;107XrLoaderLogMessageSeverityFlags _message_severities;108XrLoaderLogMessageTypeFlags _message_types;109};110111class LoaderLogger {112public:113static LoaderLogger& GetInstance() {114static LoaderLogger instance;115return instance;116}117118void AddLogRecorder(std::unique_ptr<LoaderLogRecorder>&& recorder);119void RemoveLogRecorder(uint64_t unique_id);120121void AddLogRecorderForXrInstance(XrInstance instance, std::unique_ptr<LoaderLogRecorder>&& recorder);122void RemoveLogRecordersForXrInstance(XrInstance instance);123124//! Called from LoaderXrTermSetDebugUtilsObjectNameEXT - an empty name means remove125void AddObjectName(uint64_t object_handle, XrObjectType object_type, const std::string& object_name);126void BeginLabelRegion(XrSession session, const XrDebugUtilsLabelEXT* label_info);127void EndLabelRegion(XrSession session);128void InsertLabel(XrSession session, const XrDebugUtilsLabelEXT* label_info);129void DeleteSessionLabels(XrSession session);130131bool LogMessage(XrLoaderLogMessageSeverityFlagBits message_severity, XrLoaderLogMessageTypeFlags message_type,132const std::string& message_id, const std::string& command_name, const std::string& message,133const std::vector<XrSdkLogObjectInfo>& objects = {});134static bool LogErrorMessage(const std::string& command_name, const std::string& message,135const std::vector<XrSdkLogObjectInfo>& objects = {}) {136return GetInstance().LogMessage(XR_LOADER_LOG_MESSAGE_SEVERITY_ERROR_BIT, XR_LOADER_LOG_MESSAGE_TYPE_GENERAL_BIT,137"OpenXR-Loader", command_name, message, objects);138}139static bool LogWarningMessage(const std::string& command_name, const std::string& message,140const std::vector<XrSdkLogObjectInfo>& objects = {}) {141return GetInstance().LogMessage(XR_LOADER_LOG_MESSAGE_SEVERITY_WARNING_BIT, XR_LOADER_LOG_MESSAGE_TYPE_GENERAL_BIT,142"OpenXR-Loader", command_name, message, objects);143}144static bool LogInfoMessage(const std::string& command_name, const std::string& message,145const std::vector<XrSdkLogObjectInfo>& objects = {}) {146return GetInstance().LogMessage(XR_LOADER_LOG_MESSAGE_SEVERITY_INFO_BIT, XR_LOADER_LOG_MESSAGE_TYPE_GENERAL_BIT,147"OpenXR-Loader", command_name, message, objects);148}149static bool LogVerboseMessage(const std::string& command_name, const std::string& message,150const std::vector<XrSdkLogObjectInfo>& objects = {}) {151return GetInstance().LogMessage(XR_LOADER_LOG_MESSAGE_SEVERITY_VERBOSE_BIT, XR_LOADER_LOG_MESSAGE_TYPE_GENERAL_BIT,152"OpenXR-Loader", command_name, message, objects);153}154static bool LogValidationErrorMessage(const std::string& vuid, const std::string& command_name, const std::string& message,155const std::vector<XrSdkLogObjectInfo>& objects = {}) {156return GetInstance().LogMessage(XR_LOADER_LOG_MESSAGE_SEVERITY_ERROR_BIT, XR_LOADER_LOG_MESSAGE_TYPE_SPECIFICATION_BIT,157vuid, command_name, message, objects);158}159static bool LogValidationWarningMessage(const std::string& vuid, const std::string& command_name, const std::string& message,160const std::vector<XrSdkLogObjectInfo>& objects = {}) {161return GetInstance().LogMessage(XR_LOADER_LOG_MESSAGE_SEVERITY_WARNING_BIT, XR_LOADER_LOG_MESSAGE_TYPE_SPECIFICATION_BIT,162vuid, command_name, message, objects);163}164165// Extension-specific logging functions166bool LogDebugUtilsMessage(XrDebugUtilsMessageSeverityFlagsEXT message_severity, XrDebugUtilsMessageTypeFlagsEXT message_type,167const XrDebugUtilsMessengerCallbackDataEXT* callback_data);168169// Non-copyable170LoaderLogger(const LoaderLogger&) = delete;171LoaderLogger& operator=(const LoaderLogger&) = delete;172173private:174LoaderLogger();175176std::shared_timed_mutex _mutex;177178// List of *all* available recorder objects (including created specifically for an Instance)179std::vector<std::unique_ptr<LoaderLogRecorder>> _recorders;180181// List of recorder objects only created specifically for an XrInstance182std::unordered_map<XrInstance, std::unordered_set<uint64_t>> _recordersByInstance;183184DebugUtilsData data_;185};186187// Utility functions for converting to/from XR_EXT_debug_utils values188XrLoaderLogMessageSeverityFlags DebugUtilsSeveritiesToLoaderLogMessageSeverities(189XrDebugUtilsMessageSeverityFlagsEXT utils_severities);190XrDebugUtilsMessageSeverityFlagsEXT LoaderLogMessageSeveritiesToDebugUtilsMessageSeverities(191XrLoaderLogMessageSeverityFlags log_severities);192XrLoaderLogMessageTypeFlagBits DebugUtilsMessageTypesToLoaderLogMessageTypes(XrDebugUtilsMessageTypeFlagsEXT utils_types);193XrDebugUtilsMessageTypeFlagsEXT LoaderLogMessageTypesToDebugUtilsMessageTypes(XrLoaderLogMessageTypeFlagBits log_types);194195196