The open source OpenXR runtime
0
fork

Configure Feed

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

st/oxr: Use common helper versions of truncating Xprintf

authored by

Jakob Bornecrantz and committed by
Ryan Pavlik
e466551d b33e6569

+7 -58
+7 -58
src/xrt/state_trackers/oxr/oxr_logger.c
··· 1 - // Copyright 2018-2022, Collabora, Ltd. 1 + // Copyright 2018-2023, Collabora, Ltd. 2 2 // SPDX-License-Identifier: BSL-1.0 3 3 /*! 4 4 * @file ··· 11 11 12 12 #include "util/u_misc.h" 13 13 #include "util/u_debug.h" 14 + #include "util/u_truncate_printf.h" 14 15 15 16 #include "oxr_objects.h" 16 17 #include "oxr_logger.h" ··· 60 61 } 61 62 62 63 /*! 63 - * We want to truncate the value, not get the possible written. 64 - * 65 - * There are no version of the *many* Windows versions of this functions that 66 - * truncates and returns the number of bytes written (not including null). 67 - */ 68 - static int 69 - truncate_vsnprintf(char *chars, size_t char_count, const char *fmt, va_list args) 70 - { 71 - /* 72 - * We always want to be able to write null terminator, and 73 - * something propbly went wrong if char_count larger then INT_MAX. 74 - */ 75 - if (char_count == 0 || char_count > INT_MAX) { 76 - return -1; 77 - } 78 - 79 - // Will always be able to write null terminator. 80 - int ret = vsnprintf(chars, char_count, fmt, args); 81 - if (ret < 0) { 82 - return ret; 83 - } 84 - 85 - // Safe, ret is checked for negative above. 86 - if ((size_t)ret > char_count - 1) { 87 - return (int)char_count - 1; 88 - } 89 - 90 - return ret; 91 - } 92 - 93 - /*! 94 - * We want to truncate the value, not get the possible written. 95 - */ 96 - static int 97 - truncate_snprintf(char *chars, size_t char_count, const char *fmt, ...) 98 - { 99 - /* 100 - * We always want to be able to write null terminator, and 101 - * something propbly went wrong if char_count larger then INT_MAX. 102 - */ 103 - if (char_count == 0 || char_count > INT_MAX) { 104 - return -1; 105 - } 106 - 107 - va_list args; 108 - va_start(args, fmt); 109 - int ret = truncate_vsnprintf(chars, char_count, fmt, args); 110 - va_end(args); 111 - return ret; 112 - } 113 - 114 - /*! 115 64 * Prints the first part of a logging message, has three forms. 116 65 * 117 66 * ```c++ ··· 130 79 { 131 80 if (logger->api_func_name != NULL) { 132 81 if (is_fmt_func_arg_start(fmt)) { 133 - return truncate_snprintf(buf, remaining, "%s: %s", prefix, logger->api_func_name); 82 + return u_truncate_snprintf(buf, remaining, "%s: %s", prefix, logger->api_func_name); 134 83 } else { 135 - return truncate_snprintf(buf, remaining, "%s in %s: ", prefix, logger->api_func_name); 84 + return u_truncate_snprintf(buf, remaining, "%s in %s: ", prefix, logger->api_func_name); 136 85 } 137 86 } else { 138 - return truncate_snprintf(buf, remaining, "%s: ", prefix); 87 + return u_truncate_snprintf(buf, remaining, "%s: ", prefix); 139 88 } 140 89 } 141 90 ··· 169 118 } 170 119 printed += ret; 171 120 172 - ret = truncate_vsnprintf(buf + printed, remaining - printed, fmt, args); 121 + ret = u_truncate_vsnprintf(buf + printed, remaining - printed, fmt, args); 173 122 if (ret < 0) { 174 123 U_LOG_E("Internal OpenXR logging error!"); 175 124 return; ··· 187 136 do_print_func(const char *api_func_name) 188 137 { 189 138 char buf[LOG_BUFFER_SIZE]; 190 - truncate_snprintf(buf, sizeof(buf), "%s\n", api_func_name); 139 + u_truncate_snprintf(buf, sizeof(buf), "%s\n", api_func_name); 191 140 do_output(buf); 192 141 } 193 142