The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

u/logging: Add function to log to file

Part-of: <https://gitlab.freedesktop.org/monado/monado/-/merge_requests/2619>

authored by

Jakob Bornecrantz and committed by
Marge Bot
3292394c 44773f80

+49 -2
+36 -2
src/xrt/auxiliary/util/u_logging.c
··· 1 1 // Copyright 2019-2025, Collabora, Ltd. 2 + // Copyright 2025, NVIDIA CORPORATION. 2 3 // SPDX-License-Identifier: BSL-1.0 3 4 /*! 4 5 * @file ··· 69 70 u_log_get_global_level(void) 70 71 { 71 72 return debug_get_log_option_global_log(); 73 + } 74 + 75 + 76 + /* 77 + * 78 + * Logging file code. 79 + * 80 + */ 81 + 82 + static FILE *g_log_file = NULL; 83 + 84 + void 85 + u_log_set_output_file(const char *filename) 86 + { 87 + // While not thread safe, this function is externally synchronized. 88 + if (g_log_file != NULL) { 89 + FILE *tmp = g_log_file; 90 + g_log_file = NULL; 91 + 92 + fflush(tmp); 93 + fclose(tmp); 94 + tmp = NULL; 95 + } 96 + 97 + if (filename == NULL) { 98 + return; // Turning off file logging. 99 + } 100 + 101 + g_log_file = fopen(filename, "w"); 102 + if (g_log_file == NULL) { 103 + U_LOG_E("Failed to open '%s'", filename); 104 + } 72 105 } 73 106 74 107 ··· 294 327 int ret = 0; 295 328 296 329 #ifdef XRT_FEATURE_COLOR_LOG 297 - if (isatty(STDERR_FILENO)) { 330 + if (g_log_file == NULL && isatty(STDERR_FILENO)) { 298 331 ret = print_prefix_color(func, level, buf, remaining); 299 332 } else { 300 333 ret = print_prefix_mono(func, level, buf, remaining); ··· 407 440 OutputDebugStringA(storage); 408 441 #endif 409 442 410 - fwrite(storage, printed, 1, stderr); 443 + FILE *output = g_log_file != NULL ? g_log_file : stderr; 444 + fwrite(storage, printed, 1, output); 411 445 412 446 #else 413 447 #error "Port needed for logging function"
+13
src/xrt/auxiliary/util/u_logging.h
··· 1 1 // Copyright 2020-2025, Collabora, Ltd. 2 + // Copyright 2025, NVIDIA CORPORATION. 2 3 // SPDX-License-Identifier: BSL-1.0 3 4 /*! 4 5 * @file ··· 279 280 */ 280 281 enum u_logging_level 281 282 u_log_get_global_level(void); 283 + 284 + /*! 285 + * Sets the output file for the logging instead of stderr, this function is 286 + * externally synchronized with ALL other logging functions. Which means do not 287 + * call any other logging function from different threads during a call to this 288 + * function. Also to avoid leaks call this function with NULL to close the 289 + * internally managed FILE object. 290 + * 291 + * WANRING THIS FUNCTION IS EXTERNALLY SYNCHRONIZED WITH ALL OTHER FUNCTIONS. 292 + */ 293 + void 294 + u_log_set_output_file(const char *filename); 282 295 283 296 /*! 284 297 * @brief Main non-device-related log implementation function: do not call directly, use a macro that wraps it.