Skip to content

Commit

Permalink
Prepend [Vulkan Loader] to stderr log output
Browse files Browse the repository at this point in the history
This makes it clear *who* is creating log messages, which wasn't as clear
before.

This commit refactors the logging code to be easier to understand.

Example output:

```
[Vulkan Loader] INFO:           Vulkan Loader Version 1.4.307
[Vulkan Loader] INFO:           [Vulkan Loader Git - Tag: improve_debug_log_messages, Branch/Commit: v1.4.307-6-gd9c615ac8]
[Vulkan Loader] INFO:           No valid vk_loader_settings.json file found, no loader settings will be active
[Vulkan Loader] INFO:           Found manifest file /etc/vulkan/implicit_layer.d/renderdoc_capture.json (file version 1.1.2)
```
  • Loading branch information
charles-lunarg committed Feb 4, 2025
1 parent 30aa753 commit 9d96399
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 96 deletions.
101 changes: 61 additions & 40 deletions loader/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,55 +88,47 @@ void loader_init_global_debug_level(void) {

void loader_set_global_debug_level(uint32_t new_loader_debug) { g_loader_debug = new_loader_debug; }

void generate_debug_flag_str(VkFlags msg_type, size_t cmd_line_size, char *cmd_line_msg, size_t *num_used) {
void generate_debug_flag_str(VkFlags msg_type, size_t cmd_line_size, char *cmd_line_msg) {
cmd_line_msg[0] = '\0';

// Helper macro which strncat's the given string literal, then updates num_used & cmd_line_end
// Assumes that we haven't used the entire buffer - must manually check this when adding new filter types
// We concat at the end of cmd_line_msg, so that strncat isn't a victim of Schlemiel the Painter
// We write to the end - 1 of cmd_line_msg, as the end is actually a null terminator
#define STRNCAT_TO_BUFFER(string_literal_to_cat) \
loader_strncat(cmd_line_msg + *num_used, cmd_line_size - *num_used, string_literal_to_cat, sizeof(string_literal_to_cat)); \
*num_used += sizeof(string_literal_to_cat) - 1; // subtract one to remove the null terminator in the string literal

if ((msg_type & VULKAN_LOADER_ERROR_BIT) != 0) {
STRNCAT_TO_BUFFER("ERROR");
loader_strncat(cmd_line_msg, cmd_line_size, "ERROR", sizeof("ERROR"));
}
if ((msg_type & VULKAN_LOADER_WARN_BIT) != 0) {
if (*num_used > 1) {
STRNCAT_TO_BUFFER(" | ");
if (strlen(cmd_line_msg) > 0) {
loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
}
STRNCAT_TO_BUFFER("WARNING");
loader_strncat(cmd_line_msg, cmd_line_size, "WARNING", sizeof("WARNING"));
}
if ((msg_type & VULKAN_LOADER_INFO_BIT) != 0) {
if (*num_used > 1) {
STRNCAT_TO_BUFFER(" | ");
if (strlen(cmd_line_msg) > 0) {
loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
}
STRNCAT_TO_BUFFER("INFO");
loader_strncat(cmd_line_msg, cmd_line_size, "INFO", sizeof("INFO"));
}
if ((msg_type & VULKAN_LOADER_DEBUG_BIT) != 0) {
if (*num_used > 1) {
STRNCAT_TO_BUFFER(" | ");
if (strlen(cmd_line_msg) > 0) {
loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
}
STRNCAT_TO_BUFFER("DEBUG");
loader_strncat(cmd_line_msg, cmd_line_size, "DEBUG", sizeof("DEBUG"));
}
if ((msg_type & VULKAN_LOADER_PERF_BIT) != 0) {
if (*num_used > 1) {
STRNCAT_TO_BUFFER(" | ");
if (strlen(cmd_line_msg) > 0) {
loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
}
STRNCAT_TO_BUFFER("PERF");
loader_strncat(cmd_line_msg, cmd_line_size, "PERF", sizeof("PERF"));
}
if ((msg_type & VULKAN_LOADER_DRIVER_BIT) != 0) {
if (*num_used > 1) {
STRNCAT_TO_BUFFER(" | ");
if (strlen(cmd_line_msg) > 0) {
loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
}
STRNCAT_TO_BUFFER("DRIVER");
loader_strncat(cmd_line_msg, cmd_line_size, "DRIVER", sizeof("DRIVER"));
}
if ((msg_type & VULKAN_LOADER_LAYER_BIT) != 0) {
if (*num_used > 1) {
STRNCAT_TO_BUFFER(" | ");
if (strlen(cmd_line_msg) > 0) {
loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
}
STRNCAT_TO_BUFFER("LAYER");
loader_strncat(cmd_line_msg, cmd_line_size, "LAYER", sizeof("LAYER"));
}

#undef STRNCAT_TO_BUFFER
Expand Down Expand Up @@ -219,22 +211,51 @@ void DECORATE_PRINTF(4, 5)

// Only need enough space to create the filter description header for log messages
// Also use the same header for all output
char cmd_line_msg[64];
char cmd_line_msg[64] = {0};
size_t cmd_line_size = sizeof(cmd_line_msg);
size_t num_used = 0;

generate_debug_flag_str(msg_type, cmd_line_size, cmd_line_msg, &num_used);
loader_strncat(cmd_line_msg, cmd_line_size, "[Vulkan Loader] ", sizeof("[Vulkan Loader] "));

bool need_separator = false;
if ((msg_type & VULKAN_LOADER_ERROR_BIT) != 0) {
loader_strncat(cmd_line_msg, cmd_line_size, "ERROR", sizeof("ERROR"));
need_separator = true;
} else if ((msg_type & VULKAN_LOADER_WARN_BIT) != 0) {
loader_strncat(cmd_line_msg, cmd_line_size, "WARNING", sizeof("WARNING"));
need_separator = true;
} else if ((msg_type & VULKAN_LOADER_INFO_BIT) != 0) {
loader_strncat(cmd_line_msg, cmd_line_size, "INFO", sizeof("INFO"));
need_separator = true;
} else if ((msg_type & VULKAN_LOADER_DEBUG_BIT) != 0) {
loader_strncat(cmd_line_msg, cmd_line_size, "DEBUG", sizeof("DEBUG"));
need_separator = true;
}

if ((msg_type & VULKAN_LOADER_PERF_BIT) != 0) {
if (need_separator) {
loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
}
loader_strncat(cmd_line_msg, cmd_line_size, "PERF", sizeof("PERF"));
} else if ((msg_type & VULKAN_LOADER_DRIVER_BIT) != 0) {
if (need_separator) {
loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
}
loader_strncat(cmd_line_msg, cmd_line_size, "DRIVER", sizeof("DRIVER"));
} else if ((msg_type & VULKAN_LOADER_LAYER_BIT) != 0) {
if (need_separator) {
loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
}
loader_strncat(cmd_line_msg, cmd_line_size, "LAYER", sizeof("LAYER"));
}

// Appends a : to the end of the debug flags used
loader_strncat(cmd_line_msg + num_used, cmd_line_size - num_used, ": ", sizeof(": "));
num_used += sizeof(": ") - 1;
loader_strncat(cmd_line_msg, cmd_line_size, ": ", sizeof(": "));
size_t num_used = strlen(cmd_line_msg);

// Justifies the output to at least 19 spaces
if (num_used < 19) {
const char space_buffer[] = " ";
// Only write (19 - num_used) spaces
loader_strncat(cmd_line_msg + num_used, cmd_line_size - num_used, space_buffer, sizeof(space_buffer) - 1 - num_used);
num_used += sizeof(space_buffer) - 1 - num_used;
// Justifies the output to at least 29 spaces
if (num_used < 32) {
const char space_buffer[] = " ";
// Only write (32 - num_used) spaces
loader_strncat(cmd_line_msg, cmd_line_size, space_buffer, sizeof(space_buffer) - 1 - num_used);
}
// Assert that we didn't write more than what is available in cmd_line_msg
assert(cmd_line_size > num_used);
Expand Down
5 changes: 2 additions & 3 deletions loader/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ void loader_init_global_debug_level(void);
// Sets the global debug level - used by global settings files
void loader_set_global_debug_level(uint32_t new_loader_debug);

// Writes a stringified version of enum vulkan_loader_debug_flags into cmd_line_msg, and writes the number of characters written in
// num_used
void generate_debug_flag_str(VkFlags msg_type, size_t cmd_line_size, char *cmd_line_msg, size_t *num_used);
// Writes a stringified version of enum vulkan_loader_debug_flags into a char array cmd_line_msg of length cmd_line_size
void generate_debug_flag_str(VkFlags msg_type, size_t cmd_line_size, char *cmd_line_msg);

// The asm declaration prevents name mangling which is necessary for macOS
#if defined(MODIFY_UNKNOWN_FUNCTION_DECLS)
Expand Down
7 changes: 3 additions & 4 deletions loader/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,13 @@ void log_settings(const struct loader_instance* inst, loader_settings* settings)
loader_log(inst, VULKAN_LOADER_INFO_BIT, 0, "Using layer configurations found in loader settings from %s",
settings->settings_file_path);

char cmd_line_msg[64];
char cmd_line_msg[64] = {0};
size_t cmd_line_size = sizeof(cmd_line_msg);
size_t num_used = 0;

cmd_line_msg[0] = '\0';

generate_debug_flag_str(settings->debug_level, cmd_line_size, cmd_line_msg, &num_used);
if (num_used > 0) {
generate_debug_flag_str(settings->debug_level, cmd_line_size, cmd_line_msg);
if (strlen(cmd_line_msg)) {
loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Loader Settings Filters for Logging to Standard Error: %s", cmd_line_msg);
}

Expand Down
Loading

0 comments on commit 9d96399

Please sign in to comment.