Skip to content

Commit 9d96399

Browse files
Prepend [Vulkan Loader] to stderr log output
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) ```
1 parent 30aa753 commit 9d96399

File tree

4 files changed

+122
-96
lines changed

4 files changed

+122
-96
lines changed

loader/log.c

Lines changed: 61 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -88,55 +88,47 @@ void loader_init_global_debug_level(void) {
8888

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

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

94-
// Helper macro which strncat's the given string literal, then updates num_used & cmd_line_end
95-
// Assumes that we haven't used the entire buffer - must manually check this when adding new filter types
96-
// We concat at the end of cmd_line_msg, so that strncat isn't a victim of Schlemiel the Painter
97-
// We write to the end - 1 of cmd_line_msg, as the end is actually a null terminator
98-
#define STRNCAT_TO_BUFFER(string_literal_to_cat) \
99-
loader_strncat(cmd_line_msg + *num_used, cmd_line_size - *num_used, string_literal_to_cat, sizeof(string_literal_to_cat)); \
100-
*num_used += sizeof(string_literal_to_cat) - 1; // subtract one to remove the null terminator in the string literal
101-
10294
if ((msg_type & VULKAN_LOADER_ERROR_BIT) != 0) {
103-
STRNCAT_TO_BUFFER("ERROR");
95+
loader_strncat(cmd_line_msg, cmd_line_size, "ERROR", sizeof("ERROR"));
10496
}
10597
if ((msg_type & VULKAN_LOADER_WARN_BIT) != 0) {
106-
if (*num_used > 1) {
107-
STRNCAT_TO_BUFFER(" | ");
98+
if (strlen(cmd_line_msg) > 0) {
99+
loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
108100
}
109-
STRNCAT_TO_BUFFER("WARNING");
101+
loader_strncat(cmd_line_msg, cmd_line_size, "WARNING", sizeof("WARNING"));
110102
}
111103
if ((msg_type & VULKAN_LOADER_INFO_BIT) != 0) {
112-
if (*num_used > 1) {
113-
STRNCAT_TO_BUFFER(" | ");
104+
if (strlen(cmd_line_msg) > 0) {
105+
loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
114106
}
115-
STRNCAT_TO_BUFFER("INFO");
107+
loader_strncat(cmd_line_msg, cmd_line_size, "INFO", sizeof("INFO"));
116108
}
117109
if ((msg_type & VULKAN_LOADER_DEBUG_BIT) != 0) {
118-
if (*num_used > 1) {
119-
STRNCAT_TO_BUFFER(" | ");
110+
if (strlen(cmd_line_msg) > 0) {
111+
loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
120112
}
121-
STRNCAT_TO_BUFFER("DEBUG");
113+
loader_strncat(cmd_line_msg, cmd_line_size, "DEBUG", sizeof("DEBUG"));
122114
}
123115
if ((msg_type & VULKAN_LOADER_PERF_BIT) != 0) {
124-
if (*num_used > 1) {
125-
STRNCAT_TO_BUFFER(" | ");
116+
if (strlen(cmd_line_msg) > 0) {
117+
loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
126118
}
127-
STRNCAT_TO_BUFFER("PERF");
119+
loader_strncat(cmd_line_msg, cmd_line_size, "PERF", sizeof("PERF"));
128120
}
129121
if ((msg_type & VULKAN_LOADER_DRIVER_BIT) != 0) {
130-
if (*num_used > 1) {
131-
STRNCAT_TO_BUFFER(" | ");
122+
if (strlen(cmd_line_msg) > 0) {
123+
loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
132124
}
133-
STRNCAT_TO_BUFFER("DRIVER");
125+
loader_strncat(cmd_line_msg, cmd_line_size, "DRIVER", sizeof("DRIVER"));
134126
}
135127
if ((msg_type & VULKAN_LOADER_LAYER_BIT) != 0) {
136-
if (*num_used > 1) {
137-
STRNCAT_TO_BUFFER(" | ");
128+
if (strlen(cmd_line_msg) > 0) {
129+
loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
138130
}
139-
STRNCAT_TO_BUFFER("LAYER");
131+
loader_strncat(cmd_line_msg, cmd_line_size, "LAYER", sizeof("LAYER"));
140132
}
141133

142134
#undef STRNCAT_TO_BUFFER
@@ -219,22 +211,51 @@ void DECORATE_PRINTF(4, 5)
219211

220212
// Only need enough space to create the filter description header for log messages
221213
// Also use the same header for all output
222-
char cmd_line_msg[64];
214+
char cmd_line_msg[64] = {0};
223215
size_t cmd_line_size = sizeof(cmd_line_msg);
224-
size_t num_used = 0;
225216

226-
generate_debug_flag_str(msg_type, cmd_line_size, cmd_line_msg, &num_used);
217+
loader_strncat(cmd_line_msg, cmd_line_size, "[Vulkan Loader] ", sizeof("[Vulkan Loader] "));
218+
219+
bool need_separator = false;
220+
if ((msg_type & VULKAN_LOADER_ERROR_BIT) != 0) {
221+
loader_strncat(cmd_line_msg, cmd_line_size, "ERROR", sizeof("ERROR"));
222+
need_separator = true;
223+
} else if ((msg_type & VULKAN_LOADER_WARN_BIT) != 0) {
224+
loader_strncat(cmd_line_msg, cmd_line_size, "WARNING", sizeof("WARNING"));
225+
need_separator = true;
226+
} else if ((msg_type & VULKAN_LOADER_INFO_BIT) != 0) {
227+
loader_strncat(cmd_line_msg, cmd_line_size, "INFO", sizeof("INFO"));
228+
need_separator = true;
229+
} else if ((msg_type & VULKAN_LOADER_DEBUG_BIT) != 0) {
230+
loader_strncat(cmd_line_msg, cmd_line_size, "DEBUG", sizeof("DEBUG"));
231+
need_separator = true;
232+
}
233+
234+
if ((msg_type & VULKAN_LOADER_PERF_BIT) != 0) {
235+
if (need_separator) {
236+
loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
237+
}
238+
loader_strncat(cmd_line_msg, cmd_line_size, "PERF", sizeof("PERF"));
239+
} else if ((msg_type & VULKAN_LOADER_DRIVER_BIT) != 0) {
240+
if (need_separator) {
241+
loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
242+
}
243+
loader_strncat(cmd_line_msg, cmd_line_size, "DRIVER", sizeof("DRIVER"));
244+
} else if ((msg_type & VULKAN_LOADER_LAYER_BIT) != 0) {
245+
if (need_separator) {
246+
loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
247+
}
248+
loader_strncat(cmd_line_msg, cmd_line_size, "LAYER", sizeof("LAYER"));
249+
}
227250

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

232-
// Justifies the output to at least 19 spaces
233-
if (num_used < 19) {
234-
const char space_buffer[] = " ";
235-
// Only write (19 - num_used) spaces
236-
loader_strncat(cmd_line_msg + num_used, cmd_line_size - num_used, space_buffer, sizeof(space_buffer) - 1 - num_used);
237-
num_used += sizeof(space_buffer) - 1 - num_used;
254+
// Justifies the output to at least 29 spaces
255+
if (num_used < 32) {
256+
const char space_buffer[] = " ";
257+
// Only write (32 - num_used) spaces
258+
loader_strncat(cmd_line_msg, cmd_line_size, space_buffer, sizeof(space_buffer) - 1 - num_used);
238259
}
239260
// Assert that we didn't write more than what is available in cmd_line_msg
240261
assert(cmd_line_size > num_used);

loader/log.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ void loader_init_global_debug_level(void);
5454
// Sets the global debug level - used by global settings files
5555
void loader_set_global_debug_level(uint32_t new_loader_debug);
5656

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

6160
// The asm declaration prevents name mangling which is necessary for macOS
6261
#if defined(MODIFY_UNKNOWN_FUNCTION_DECLS)

loader/settings.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,14 +279,13 @@ void log_settings(const struct loader_instance* inst, loader_settings* settings)
279279
loader_log(inst, VULKAN_LOADER_INFO_BIT, 0, "Using layer configurations found in loader settings from %s",
280280
settings->settings_file_path);
281281

282-
char cmd_line_msg[64];
282+
char cmd_line_msg[64] = {0};
283283
size_t cmd_line_size = sizeof(cmd_line_msg);
284-
size_t num_used = 0;
285284

286285
cmd_line_msg[0] = '\0';
287286

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

0 commit comments

Comments
 (0)