#include "loader_logger_recorders.hpp"
#include "hex_and_handles.h"
#include "loader_logger.hpp"
#include <openxr/openxr.h>
#include <memory>
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#ifdef __ANDROID__
#include "android/log.h"
#endif
#ifdef _WIN32
#include <windows.h>
#endif
namespace {
void OutputMessageToStream(std::ostream& os, XrLoaderLogMessageSeverityFlagBits message_severity,
XrLoaderLogMessageTypeFlags message_type, const XrLoaderLogMessengerCallbackData* callback_data) { … }
class OstreamLoaderLogRecorder : public LoaderLogRecorder { … };
class DebugUtilsLogRecorder : public LoaderLogRecorder { … };
#ifdef __ANDROID__
class LogcatLoaderLogRecorder : public LoaderLogRecorder {
public:
LogcatLoaderLogRecorder();
bool LogMessage(XrLoaderLogMessageSeverityFlagBits message_severity, XrLoaderLogMessageTypeFlags message_type,
const XrLoaderLogMessengerCallbackData* callback_data) override;
};
#endif
#ifdef _WIN32
class DebuggerLoaderLogRecorder : public LoaderLogRecorder {
public:
DebuggerLoaderLogRecorder(void* user_data, XrLoaderLogMessageSeverityFlags flags);
bool LogMessage(XrLoaderLogMessageSeverityFlagBits message_severity, XrLoaderLogMessageTypeFlags message_type,
const XrLoaderLogMessengerCallbackData* callback_data) override;
};
#endif
OstreamLoaderLogRecorder::OstreamLoaderLogRecorder(std::ostream& os, void* user_data, XrLoaderLogMessageSeverityFlags flags)
: … { … }
bool OstreamLoaderLogRecorder::LogMessage(XrLoaderLogMessageSeverityFlagBits message_severity,
XrLoaderLogMessageTypeFlags message_type,
const XrLoaderLogMessengerCallbackData* callback_data) { … }
DebugUtilsLogRecorder::DebugUtilsLogRecorder(const XrDebugUtilsMessengerCreateInfoEXT* create_info,
XrDebugUtilsMessengerEXT debug_messenger)
: … { … }
bool DebugUtilsLogRecorder::LogMessage(XrLoaderLogMessageSeverityFlagBits message_severity,
XrLoaderLogMessageTypeFlags message_type,
const XrLoaderLogMessengerCallbackData* callback_data) { … }
bool DebugUtilsLogRecorder::LogDebugUtilsMessage(XrDebugUtilsMessageSeverityFlagsEXT message_severity,
XrDebugUtilsMessageTypeFlagsEXT message_type,
const XrDebugUtilsMessengerCallbackDataEXT* callback_data) { … }
#ifdef __ANDROID__
static inline android_LogPriority LoaderToAndroidLogPriority(XrLoaderLogMessageSeverityFlags message_severity) {
if (0 != (message_severity & XR_LOADER_LOG_MESSAGE_SEVERITY_ERROR_BIT)) {
return ANDROID_LOG_ERROR;
}
if (0 != (message_severity & XR_LOADER_LOG_MESSAGE_SEVERITY_WARNING_BIT)) {
return ANDROID_LOG_WARN;
}
if (0 != (message_severity & XR_LOADER_LOG_MESSAGE_SEVERITY_INFO_BIT)) {
return ANDROID_LOG_INFO;
}
return ANDROID_LOG_VERBOSE;
}
LogcatLoaderLogRecorder::LogcatLoaderLogRecorder()
: LoaderLogRecorder(XR_LOADER_LOG_LOGCAT, nullptr,
XR_LOADER_LOG_MESSAGE_SEVERITY_VERBOSE_BIT | XR_LOADER_LOG_MESSAGE_SEVERITY_INFO_BIT |
XR_LOADER_LOG_MESSAGE_SEVERITY_WARNING_BIT | XR_LOADER_LOG_MESSAGE_SEVERITY_ERROR_BIT,
0xFFFFFFFFUL) {
Start();
}
bool LogcatLoaderLogRecorder::LogMessage(XrLoaderLogMessageSeverityFlagBits message_severity,
XrLoaderLogMessageTypeFlags message_type,
const XrLoaderLogMessengerCallbackData* callback_data) {
if (_active && 0 != (_message_severities & message_severity) && 0 != (_message_types & message_type)) {
std::stringstream ss;
OutputMessageToStream(ss, message_severity, message_type, callback_data);
__android_log_write(LoaderToAndroidLogPriority(message_severity), "OpenXR-Loader", ss.str().c_str());
}
return false;
}
#endif
#ifdef _WIN32
DebuggerLoaderLogRecorder::DebuggerLoaderLogRecorder(void* user_data, XrLoaderLogMessageSeverityFlags flags)
: LoaderLogRecorder(XR_LOADER_LOG_DEBUGGER, user_data, flags, 0xFFFFFFFFUL) {
Start();
}
bool DebuggerLoaderLogRecorder::LogMessage(XrLoaderLogMessageSeverityFlagBits message_severity,
XrLoaderLogMessageTypeFlags message_type,
const XrLoaderLogMessengerCallbackData* callback_data) {
if (_active && 0 != (_message_severities & message_severity) && 0 != (_message_types & message_type)) {
std::stringstream ss;
OutputMessageToStream(ss, message_severity, message_type, callback_data);
OutputDebugStringA(ss.str().c_str());
}
return false;
}
#endif
}
std::unique_ptr<LoaderLogRecorder> MakeStdOutLoaderLogRecorder(void* user_data, XrLoaderLogMessageSeverityFlags flags) { … }
std::unique_ptr<LoaderLogRecorder> MakeStdErrLoaderLogRecorder(void* user_data) { … }
std::unique_ptr<LoaderLogRecorder> MakeDebugUtilsLoaderLogRecorder(const XrDebugUtilsMessengerCreateInfoEXT* create_info,
XrDebugUtilsMessengerEXT debug_messenger) { … }
#ifdef __ANDROID__
std::unique_ptr<LoaderLogRecorder> MakeLogcatLoaderLogRecorder() {
std::unique_ptr<LoaderLogRecorder> recorder(new LogcatLoaderLogRecorder());
return recorder;
}
#endif
#ifdef _WIN32
std::unique_ptr<LoaderLogRecorder> MakeDebuggerLoaderLogRecorder(void* user_data) {
std::unique_ptr<LoaderLogRecorder> recorder(new DebuggerLoaderLogRecorder(user_data, XR_LOADER_LOG_MESSAGE_SEVERITY_ERROR_BIT));
return recorder;
}
#endif