Skip to content

Commit

Permalink
Merge pull request #1481 from cfnptr/master
Browse files Browse the repository at this point in the history
Clean up MVKLogging
  • Loading branch information
billhollings authored Nov 26, 2021
2 parents 8025677 + 0da221f commit c376273
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 85 deletions.
2 changes: 1 addition & 1 deletion MoltenVK/MoltenVK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@
A98149401FB6A3F7005F00B4 /* Utility */ = {
isa = PBXGroup;
children = (
A9F0429E1FB4CF82009FCCB8 /* MVKLogging.h */,
A98149421FB6A3F7005F00B4 /* MVKBaseObject.h */,
A98149411FB6A3F7005F00B4 /* MVKBaseObject.mm */,
A9D7104E25CDE05E00E38106 /* MVKBitArray.h */,
Expand Down Expand Up @@ -751,7 +752,6 @@
isa = PBXGroup;
children = (
A9F0429D1FB4CF82009FCCB8 /* MVKCommonEnvironment.h */,
A9F0429E1FB4CF82009FCCB8 /* MVKLogging.h */,
A9B51BD6225E986A00AC74D2 /* MVKOSExtensions.h */,
A9B51BD2225E986A00AC74D2 /* MVKOSExtensions.mm */,
A981496A1FB6A998005F00B4 /* MVKStrings.h */,
Expand Down
4 changes: 3 additions & 1 deletion MoltenVK/MoltenVK/API/vk_mvk_moltenvk.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ typedef unsigned long MTLLanguageVersion;
typedef enum MVKConfigLogLevel {
MVK_CONFIG_LOG_LEVEL_NONE = 0, /**< No logging. */
MVK_CONFIG_LOG_LEVEL_ERROR = 1, /**< Log errors only. */
MVK_CONFIG_LOG_LEVEL_INFO = 2, /**< Log errors and informational messages. */
MVK_CONFIG_LOG_LEVEL_WARNING = 2, /**< Log errors and warning messages. */
MVK_CONFIG_LOG_LEVEL_INFO = 3, /**< Log errors, warnings and informational messages. */
MVK_CONFIG_LOG_LEVEL_DEBUG = 4, /**< Log errors, warnings, infos and debug messages. */
MVK_CONFIG_LOG_LEVEL_MAX_ENUM = 0x7FFFFFFF
} MVKConfigLogLevel;

Expand Down
6 changes: 3 additions & 3 deletions MoltenVK/MoltenVK/GPUObjects/MVKInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class MVKInstance : public MVKDispatchableVulkanAPIObject {
VkDebugUtilsMessageTypeFlagsEXT messageTypes,
const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData);

void debugReportMessage(MVKVulkanAPIObject* mvkAPIObj, int aslLvl, const char* pMessage);
void debugReportMessage(MVKVulkanAPIObject* mvkAPIObj, MVKConfigLogLevel logLevel, const char* pMessage);

/** Returns whether debug callbacks are being used. */
bool hasDebugCallbacks() { return _hasDebugReportCallbacks || _hasDebugUtilsMessengers; }
Expand Down Expand Up @@ -181,8 +181,8 @@ class MVKInstance : public MVKDispatchableVulkanAPIObject {
void initProcAddrs();
void initDebugCallbacks(const VkInstanceCreateInfo* pCreateInfo);
NSArray<id<MTLDevice>>* getAvailableMTLDevicesArray();
VkDebugReportFlagsEXT getVkDebugReportFlagsFromASLLevel(int aslLvl);
VkDebugUtilsMessageSeverityFlagBitsEXT getVkDebugUtilsMessageSeverityFlagBitsFromASLLevel(int aslLvl);
VkDebugReportFlagsEXT getVkDebugReportFlagsFromLogLevel(MVKConfigLogLevel logLevel);
VkDebugUtilsMessageSeverityFlagBitsEXT getVkDebugUtilsMessageSeverityFlagBitsFromLogLevel(MVKConfigLogLevel logLevel);
MVKEntryPoint* getEntryPoint(const char* pName);
void logVersions();
VkResult verifyLayers(uint32_t count, const char* const* names);
Expand Down
44 changes: 15 additions & 29 deletions MoltenVK/MoltenVK/GPUObjects/MVKInstance.mm
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,17 @@
}
}

void MVKInstance::debugReportMessage(MVKVulkanAPIObject* mvkAPIObj, int aslLvl, const char* pMessage) {
void MVKInstance::debugReportMessage(MVKVulkanAPIObject* mvkAPIObj, MVKConfigLogLevel logLevel, const char* pMessage) {

if (_hasDebugReportCallbacks) {
VkDebugReportFlagsEXT flags = getVkDebugReportFlagsFromASLLevel(aslLvl);
VkDebugReportFlagsEXT flags = getVkDebugReportFlagsFromLogLevel(logLevel);
uint64_t object = (uint64_t)(mvkAPIObj ? mvkAPIObj->getVkHandle() : nullptr);
VkDebugReportObjectTypeEXT objectType = mvkAPIObj ? mvkAPIObj->getVkDebugReportObjectType() : VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT;
debugReportMessage(flags, objectType, object, 0, 0, _debugReportCallbackLayerPrefix, pMessage);
}

if (_hasDebugUtilsMessengers) {
VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity = getVkDebugUtilsMessageSeverityFlagBitsFromASLLevel(aslLvl);
VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity = getVkDebugUtilsMessageSeverityFlagBitsFromLogLevel(logLevel);
uint64_t objectHandle = (uint64_t)(mvkAPIObj ? mvkAPIObj->getVkHandle() : nullptr);
VkObjectType objectType = mvkAPIObj ? mvkAPIObj->getVkObjectType() : VK_OBJECT_TYPE_UNKNOWN;

Expand Down Expand Up @@ -237,43 +237,29 @@
}
}

VkDebugReportFlagsEXT MVKInstance::getVkDebugReportFlagsFromASLLevel(int aslLvl) {
switch (aslLvl) {
case ASL_LEVEL_DEBUG:
VkDebugReportFlagsEXT MVKInstance::getVkDebugReportFlagsFromLogLevel(MVKConfigLogLevel logLevel) {
switch (logLevel) {
case MVK_CONFIG_LOG_LEVEL_DEBUG:
return VK_DEBUG_REPORT_DEBUG_BIT_EXT;

case ASL_LEVEL_INFO:
case ASL_LEVEL_NOTICE:
case MVK_CONFIG_LOG_LEVEL_INFO:
return VK_DEBUG_REPORT_INFORMATION_BIT_EXT;

case ASL_LEVEL_WARNING:
case MVK_CONFIG_LOG_LEVEL_WARNING:
return VK_DEBUG_REPORT_WARNING_BIT_EXT;

case ASL_LEVEL_ERR:
case ASL_LEVEL_CRIT:
case ASL_LEVEL_ALERT:
case ASL_LEVEL_EMERG:
case MVK_CONFIG_LOG_LEVEL_ERROR:
default:
return VK_DEBUG_REPORT_ERROR_BIT_EXT;
}
}

VkDebugUtilsMessageSeverityFlagBitsEXT MVKInstance::getVkDebugUtilsMessageSeverityFlagBitsFromASLLevel(int aslLvl) {
switch (aslLvl) {
case ASL_LEVEL_DEBUG:
VkDebugUtilsMessageSeverityFlagBitsEXT MVKInstance::getVkDebugUtilsMessageSeverityFlagBitsFromLogLevel(MVKConfigLogLevel logLevel) {
switch (logLevel) {
case MVK_CONFIG_LOG_LEVEL_DEBUG:
return VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT;

case ASL_LEVEL_INFO:
case ASL_LEVEL_NOTICE:
case MVK_CONFIG_LOG_LEVEL_INFO:
return VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT;

case ASL_LEVEL_WARNING:
case MVK_CONFIG_LOG_LEVEL_WARNING:
return VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;

case ASL_LEVEL_ERR:
case ASL_LEVEL_CRIT:
case ASL_LEVEL_ALERT:
case ASL_LEVEL_EMERG:
case MVK_CONFIG_LOG_LEVEL_ERROR:
default:
return VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
}
Expand Down
8 changes: 4 additions & 4 deletions MoltenVK/MoltenVK/Utility/MVKBaseObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#pragma once

#include "mvk_vulkan.h"
#include "vk_mvk_moltenvk.h"
#include <string>
#include <atomic>

Expand Down Expand Up @@ -47,15 +47,15 @@ class MVKBaseObject {
* and some subclasses will also forward the message to their VkInstance for
* output to the Vulkan debug report messaging API.
*/
void reportMessage(int aslLvl, const char* format, ...) __printflike(3, 4);
void reportMessage(MVKConfigLogLevel logLevel, const char* format, ...) __printflike(3, 4);

/**
* Report a Vulkan error message, on behalf of the object, which may be nil.
* Reporting includes logging to a standard system logging stream, and if the object
* is not nil and has access to the VkInstance, the message will also be forwarded
* to the VkInstance for output to the Vulkan debug report messaging API.
*/
static void reportMessage(MVKBaseObject* mvkObj, int aslLvl, const char* format, ...) __printflike(3, 4);
static void reportMessage(MVKBaseObject* mvkObj, MVKConfigLogLevel logLevel, const char* format, ...) __printflike(3, 4);

/**
* Report a Vulkan error message, on behalf of the object, which may be nil.
Expand All @@ -65,7 +65,7 @@ class MVKBaseObject {
*
* This is the core reporting implementation. Other similar functions delegate here.
*/
static void reportMessage(MVKBaseObject* mvkObj, int aslLvl, const char* format, va_list args) __printflike(3, 0);
static void reportMessage(MVKBaseObject* mvkObj, MVKConfigLogLevel logLevel, const char* format, va_list args) __printflike(3, 0);

/**
* Report a Vulkan error message. This includes logging to a standard system logging stream,
Expand Down
39 changes: 16 additions & 23 deletions MoltenVK/MoltenVK/Utility/MVKBaseObject.mm
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,15 @@
using namespace std;


static const char* getReportingLevelString(int aslLvl) {
switch (aslLvl) {
case ASL_LEVEL_DEBUG:
static const char* getReportingLevelString(MVKConfigLogLevel logLevel) {
switch (logLevel) {
case MVK_CONFIG_LOG_LEVEL_DEBUG:
return "mvk-debug";

case ASL_LEVEL_INFO:
case ASL_LEVEL_NOTICE:
case MVK_CONFIG_LOG_LEVEL_INFO:
return "mvk-info";

case ASL_LEVEL_WARNING:
case MVK_CONFIG_LOG_LEVEL_WARNING:
return "mvk-warn";

case ASL_LEVEL_ERR:
case ASL_LEVEL_CRIT:
case ASL_LEVEL_ALERT:
case ASL_LEVEL_EMERG:
case MVK_CONFIG_LOG_LEVEL_ERROR:
default:
return "mvk-error";
}
Expand All @@ -59,27 +52,27 @@
return clzName;
}

void MVKBaseObject::reportMessage(int aslLvl, const char* format, ...) {
void MVKBaseObject::reportMessage(MVKConfigLogLevel logLevel, const char* format, ...) {
va_list args;
va_start(args, format);
reportMessage(this, aslLvl, format, args);
reportMessage(this, logLevel, format, args);
va_end(args);
}

void MVKBaseObject::reportMessage(MVKBaseObject* mvkObj, int aslLvl, const char* format, ...) {
void MVKBaseObject::reportMessage(MVKBaseObject* mvkObj, MVKConfigLogLevel logLevel, const char* format, ...) {
va_list args;
va_start(args, format);
reportMessage(mvkObj, aslLvl, format, args);
reportMessage(mvkObj, logLevel, format, args);
va_end(args);
}

// This is the core reporting implementation. Other similar functions delegate here.
void MVKBaseObject::reportMessage(MVKBaseObject* mvkObj, int aslLvl, const char* format, va_list args) {
void MVKBaseObject::reportMessage(MVKBaseObject* mvkObj, MVKConfigLogLevel logLevel, const char* format, va_list args) {

MVKVulkanAPIObject* mvkAPIObj = mvkObj ? mvkObj->getVulkanAPIObject() : nullptr;
MVKInstance* mvkInst = mvkAPIObj ? mvkAPIObj->getInstance() : nullptr;
bool hasDebugCallbacks = mvkInst && mvkInst->hasDebugCallbacks();
bool shouldLog = (aslLvl < (mvkConfig().logLevel << 2));
bool shouldLog = logLevel <= mvkConfig().logLevel;

// Fail fast to avoid further unnecessary processing.
if ( !(shouldLog || hasDebugCallbacks) ) { return; }
Expand Down Expand Up @@ -107,10 +100,10 @@
va_end(origArgs);

// Log the message to the standard error stream
if (shouldLog) { fprintf(stderr, "[%s] %s\n", getReportingLevelString(aslLvl), pMessage); }
if (shouldLog) { fprintf(stderr, "[%s] %s\n", getReportingLevelString(logLevel), pMessage); }

// Broadcast the message to any Vulkan debug report callbacks
if (hasDebugCallbacks) { mvkInst->debugReportMessage(mvkAPIObj, aslLvl, pMessage); }
if (hasDebugCallbacks) { mvkInst->debugReportMessage(mvkAPIObj, logLevel, pMessage); }

free(redoBuff);
}
Expand Down Expand Up @@ -138,11 +131,11 @@
const char* vkRsltName = mvkVkResultName(vkErr);
char fmtStr[strlen(vkRsltName) + strlen(format) + 4];
sprintf(fmtStr, "%s: %s", vkRsltName, format);

// Report the error
va_list lclArgs;
va_copy(lclArgs, args);
reportMessage(mvkObj, ASL_LEVEL_ERR, fmtStr, lclArgs);
reportMessage(mvkObj, MVK_CONFIG_LOG_LEVEL_ERROR, fmtStr, lclArgs);
va_end(lclArgs);

return vkErr;
Expand Down
46 changes: 22 additions & 24 deletions Common/MVKLogging.h → MoltenVK/MoltenVK/Utility/MVKLogging.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@ extern "C" {
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <asl.h>
#include <stdbool.h>

/**
* This library adds flexible, non-intrusive logging and assertion capabilities
* that can be efficiently enabled or disabled via compiler switches.
*
* There are five levels of logging: Trace, Info, Warning, Error and Debug, and each can be enabled
* independently via the MVK_LOG_LEVEL_TRACE, MVK_LOG_LEVEL_INFO, MVK_LOG_LEVEL_WARNING,
* MVK_LOG_LEVEL_ERROR and MVK_LOG_LEVEL_DEBUG switches, respectively.
* There are five levels of logging, controlled by the MVK_LOG_LEVEL_TRACE, MVK_LOG_LEVEL_DEBUG,
* MVK_LOG_LEVEL_INFO, MVK_LOG_LEVEL_WARNING аnd MVK_LOG_LEVEL_ERROR switches.
*
* ALL logging can be enabled or disabled via the MVK_LOGGING_ENABLED switch.
*
Expand All @@ -59,10 +57,10 @@ extern "C" {
* MVKLogErrorIf(cond, fmt, ...) - same as MVKLogError if boolean "cond" condition expression evaluates to YES,
* otherwise logs nothing.
*
* MVKLogWarning(fmt, ...) - recommended for not immediately harmful errors
* - will print if MVK_LOG_LEVEL_WARNING is set on.
* MVKLogWarningIf(cond, fmt, ...) - same as MVKLogWarning if boolean "cond" condition expression evaluates to YES,
* otherwise logs nothing.
* MVKLogWarning(fmt, ...) - recommended for not immediately harmful errors
* - will print if MVK_LOG_LEVEL_WARNING is set on.
* MVKLogWarningIf(cond, fmt, ...) - same as MVKLogWarning if boolean "cond" condition expression evaluates to YES,
* otherwise logs nothing.
*
* MVKLogInfo(fmt, ...) - recommended for general, infrequent, information messages
* - will print if MVK_LOG_LEVEL_INFO is set on.
Expand Down Expand Up @@ -150,8 +148,8 @@ extern "C" {

// Warning logging - for not immediately harmful errors
#if MVK_LOG_LEVEL_WARNING
# define MVKLogWarning(fmt, ...) MVKLogWarningImpl(fmt, ##__VA_ARGS__)
# define MVKLogWarningIf(cond, fmt, ...) if(cond) { MVKLogWarningImpl(fmt, ##__VA_ARGS__); }
# define MVKLogWarning(fmt, ...) MVKLogWarningImpl(fmt, ##__VA_ARGS__)
# define MVKLogWarningIf(cond, fmt, ...) if(cond) { MVKLogWarningImpl(fmt, ##__VA_ARGS__); }
#else
# define MVKLogWarning(...)
# define MVKLogWarningIf(cond, fmt, ...)
Expand All @@ -166,15 +164,6 @@ extern "C" {
# define MVKLogInfoIf(cond, fmt, ...)
#endif

// Trace logging - for detailed tracing
#if MVK_LOG_LEVEL_TRACE
# define MVKLogTrace(fmt, ...) MVKLogTraceImpl(fmt, ##__VA_ARGS__)
# define MVKLogTraceIf(cond, fmt, ...) if(cond) { MVKLogTraceImpl(fmt, ##__VA_ARGS__); }
#else
# define MVKLogTrace(...)
# define MVKLogTraceIf(cond, fmt, ...)
#endif

// Debug logging - use only temporarily for highlighting and tracking down problems
#if MVK_LOG_LEVEL_DEBUG
# define MVKLogDebug(fmt, ...) MVKLogDebugImpl(fmt, ##__VA_ARGS__)
Expand All @@ -184,11 +173,20 @@ extern "C" {
# define MVKLogDebugIf(cond, fmt, ...)
#endif

#define MVKLogErrorImpl(fmt, ...) reportMessage(ASL_LEVEL_ERR, fmt, ##__VA_ARGS__)
#define MVKLogWarningImpl(fmt, ...) reportMessage(ASL_LEVEL_WARNING, fmt, ##__VA_ARGS__)
#define MVKLogInfoImpl(fmt, ...) reportMessage(ASL_LEVEL_NOTICE, fmt, ##__VA_ARGS__)
#define MVKLogTraceImpl(fmt, ...) reportMessage(ASL_LEVEL_DEBUG, fmt, ##__VA_ARGS__)
#define MVKLogDebugImpl(fmt, ...) reportMessage(ASL_LEVEL_DEBUG, fmt, ##__VA_ARGS__)
// Trace logging - for detailed tracing
#if MVK_LOG_LEVEL_TRACE
# define MVKLogTrace(fmt, ...) MVKLogTraceImpl(fmt, ##__VA_ARGS__)
# define MVKLogTraceIf(cond, fmt, ...) if(cond) { MVKLogTraceImpl(fmt, ##__VA_ARGS__); }
#else
# define MVKLogTrace(...)
# define MVKLogTraceIf(cond, fmt, ...)
#endif

#define MVKLogErrorImpl(fmt, ...) reportMessage(MVK_CONFIG_LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__)
#define MVKLogWarningImpl(fmt, ...) reportMessage(MVK_CONFIG_LOG_LEVEL_WARNING, fmt, ##__VA_ARGS__)
#define MVKLogInfoImpl(fmt, ...) reportMessage(MVK_CONFIG_LOG_LEVEL_INFO, fmt, ##__VA_ARGS__)
#define MVKLogDebugImpl(fmt, ...) reportMessage(MVK_CONFIG_LOG_LEVEL_DEBUG, fmt, ##__VA_ARGS__)
#define MVKLogTraceImpl(fmt, ...) reportMessage(MVK_CONFIG_LOG_LEVEL_DEBUG, fmt, ##__VA_ARGS__)

// Assertions
#ifdef NS_BLOCK_ASSERTIONS
Expand Down

0 comments on commit c376273

Please sign in to comment.