From 8c8356840a1aaf661b426648d015d04836675079 Mon Sep 17 00:00:00 2001 From: Jakob Bornecrantz Date: Sat, 17 Jun 2023 19:36:40 +0100 Subject: [PATCH] u/debug: Tidy file and export more converters --- src/xrt/auxiliary/util/u_debug.c | 417 ++++++++++++++++--------------- src/xrt/auxiliary/util/u_debug.h | 41 ++- 2 files changed, 257 insertions(+), 201 deletions(-) diff --git a/src/xrt/auxiliary/util/u_debug.c b/src/xrt/auxiliary/util/u_debug.c index c14efe980..cf227aeb0 100644 --- a/src/xrt/auxiliary/util/u_debug.c +++ b/src/xrt/auxiliary/util/u_debug.c @@ -1,4 +1,4 @@ -// Copyright 2019-2020, Collabora, Ltd. +// Copyright 2019-2023, Collabora, Ltd. // SPDX-License-Identifier: BSL-1.0 /*! * @file @@ -18,193 +18,48 @@ #include #include +#include #include #include DEBUG_GET_ONCE_BOOL_OPTION(print, "XRT_PRINT_OPTIONS", false) + +/* + * + * Helpers + * + */ + static const char * os_getenv(const char *name) { return getenv(name); } -const char * -debug_get_option(const char *name, const char *_default) +static const char * +level_to_str(enum u_logging_level level) { - const char *raw = getenv(name); - const char *ret; - - if (raw == NULL) { - ret = _default; - } else { - ret = raw; + switch (level) { + case U_LOGGING_TRACE: return "trace"; + case U_LOGGING_DEBUG: return "debug"; + case U_LOGGING_INFO: return "info"; + case U_LOGGING_WARN: return "warn"; + case U_LOGGING_ERROR: return "error"; + default: return "invalid"; } - - if (debug_get_bool_option_print()) { - U_LOG_RAW("%s=%s (%s)", name, ret, raw == NULL ? "nil" : raw); - } - - return ret; } -bool -debug_string_to_bool(const char *raw) +static const char * +tristate_to_str(enum debug_tristate_option tristate) { - bool ret; - if (raw == NULL) { - ret = false; - } else if (!strcmp(raw, "false")) { - ret = false; - } else if (!strcmp(raw, "FALSE")) { - ret = false; - } else if (!strcmp(raw, "off")) { - ret = false; - } else if (!strcmp(raw, "OFF")) { - ret = false; - } else if (!strcmp(raw, "no")) { - ret = false; - } else if (!strcmp(raw, "NO")) { - ret = false; - } else if (!strcmp(raw, "n")) { - ret = false; - } else if (!strcmp(raw, "N")) { - ret = false; - } else if (!strcmp(raw, "f")) { - ret = false; - } else if (!strcmp(raw, "F")) { - ret = false; - } else if (!strcmp(raw, "0")) { - ret = false; - } else { - ret = true; + switch (tristate) { + case DEBUG_TRISTATE_OFF: return "OFF"; + case DEBUG_TRISTATE_AUTO: return "AUTO"; + case DEBUG_TRISTATE_ON: return "ON"; + default: return "invalid"; } - return ret; -} - -bool -debug_get_bool_option(const char *name, bool _default) -{ - const char *raw = os_getenv(name); - bool ret = raw == NULL ? _default : debug_string_to_bool(raw); - - if (debug_get_bool_option_print()) { - U_LOG_RAW("%s=%s (%s)", name, ret ? "TRUE" : "FALSE", raw == NULL ? "nil" : raw); - } - - return ret; -} - - -enum debug_tristate_option -debug_string_to_tristate(const char *raw) -{ - enum debug_tristate_option ret; - if (raw == NULL) { - ret = DEBUG_TRISTATE_AUTO; - } else if (!strcmp(raw, "AUTO")) { - ret = DEBUG_TRISTATE_AUTO; - } else if (!strcmp(raw, "auto")) { - ret = DEBUG_TRISTATE_AUTO; - } else if (!strcmp(raw, "a")) { - ret = DEBUG_TRISTATE_AUTO; - } else if (!strcmp(raw, "A")) { - ret = DEBUG_TRISTATE_AUTO; - } else { - bool bool_ret = debug_string_to_bool(raw); - if (bool_ret) { - ret = DEBUG_TRISTATE_ON; - } else { - ret = DEBUG_TRISTATE_OFF; - } - } - return ret; -} - -enum debug_tristate_option -debug_get_tristate_option(const char *name) -{ - const char *raw = os_getenv(name); - enum debug_tristate_option ret = debug_string_to_tristate(raw); - - if (debug_get_bool_option_print()) { - const char *pretty_val; - switch (ret) { - case DEBUG_TRISTATE_OFF: { - pretty_val = "OFF"; - break; - } - case DEBUG_TRISTATE_AUTO: { - pretty_val = "AUTO"; - break; - } - case DEBUG_TRISTATE_ON: { - pretty_val = "ON"; - break; - } - default: pretty_val = "invalid"; - } - U_LOG_RAW("%s=%s (%s)", name, pretty_val, raw == NULL ? "nil" : raw); - } - - return ret; -} - -long -debug_string_to_num(const char *raw, long _default) -{ - if (raw == NULL) { - return _default; - } - - char *endptr; - long ret = strtol(raw, &endptr, 0); - - // Restore the default value when no digits were found. - if (raw == endptr) { - ret = _default; - } - - return ret; -} - -long -debug_get_num_option(const char *name, long _default) -{ - const char *raw = os_getenv(name); - long ret = debug_string_to_num(raw, _default); - - if (debug_get_bool_option_print()) { - U_LOG_RAW("%s=%li (%s)", name, ret, raw == NULL ? "nil" : raw); - } - - return ret; -} - -float -debug_get_float_option(const char *name, float _default) -{ - const char *raw = os_getenv(name); - float ret; - - if (raw == NULL) { - ret = _default; - } else { - char *endptr; - - ret = strtof(raw, &endptr); - // Restore the default value when no digits were found. - if (raw == endptr) { - ret = _default; - } - } - - if (debug_get_bool_option_print()) { - U_LOG_RAW("%s=%f (%s)", name, ret, raw == NULL ? "nil" : raw); - } - - return ret; } /*! @@ -231,43 +86,211 @@ is_str_in_start_of(const char *str, const char *matches) return true; } -static const char * -level_to_str(enum u_logging_level level) + +/* + * + * 'Exported' conversion functions. + * + */ + +bool +debug_string_to_bool(const char *string) { - switch (level) { - case U_LOGGING_TRACE: return "trace"; - case U_LOGGING_DEBUG: return "debug"; - case U_LOGGING_INFO: return "info"; - case U_LOGGING_WARN: return "warn"; - case U_LOGGING_ERROR: return "error"; - default: return "???"; + if (string == NULL) { + return false; + } else if (!strcmp(string, "false")) { + return false; + } else if (!strcmp(string, "FALSE")) { + return false; + } else if (!strcmp(string, "off")) { + return false; + } else if (!strcmp(string, "OFF")) { + return false; + } else if (!strcmp(string, "no")) { + return false; + } else if (!strcmp(string, "NO")) { + return false; + } else if (!strcmp(string, "n")) { + return false; + } else if (!strcmp(string, "N")) { + return false; + } else if (!strcmp(string, "f")) { + return false; + } else if (!strcmp(string, "F")) { + return false; + } else if (!strcmp(string, "0")) { + return false; + } else { + return true; } } +enum debug_tristate_option +debug_string_to_tristate(const char *string) +{ + if (string == NULL) { + return DEBUG_TRISTATE_AUTO; + } else if (!strcmp(string, "AUTO")) { + return DEBUG_TRISTATE_AUTO; + } else if (!strcmp(string, "auto")) { + return DEBUG_TRISTATE_AUTO; + } else if (!strcmp(string, "a")) { + return DEBUG_TRISTATE_AUTO; + } else if (!strcmp(string, "A")) { + return DEBUG_TRISTATE_AUTO; + } else { + if (debug_string_to_bool(string)) { + return DEBUG_TRISTATE_ON; + } else { + return DEBUG_TRISTATE_OFF; + } + } +} + +long +debug_string_to_num(const char *string, long _default) +{ + if (string == NULL) { + return _default; + } + + char *endptr; + long ret = strtol(string, &endptr, 0); + + // Restore the default value when no digits were found. + if (string == endptr) { + return _default; + } + + return ret; +} + +float +debug_string_to_float(const char *string, float _default) +{ + if (string == NULL) { + return _default; + } + + char *endptr; + float ret = strtof(string, &endptr); + + // Restore the default value when no digits were found. + if (string == endptr) { + return _default; + } + + return ret; +} + +enum u_logging_level +debug_string_to_log_level(const char *string, enum u_logging_level _default) +{ + if (string == NULL) { + return _default; + } else if (is_str_in_start_of(string, "trace")) { + return U_LOGGING_TRACE; + } else if (is_str_in_start_of(string, "debug")) { + return U_LOGGING_DEBUG; + } else if (is_str_in_start_of(string, "info")) { + return U_LOGGING_INFO; + } else if (is_str_in_start_of(string, "warn")) { + return U_LOGGING_WARN; + } else if (is_str_in_start_of(string, "error")) { + return U_LOGGING_ERROR; + } else { + return _default; + } +} + + +/* + * + * 'Exported' debug value getters. + * + */ + +const char * +debug_get_option(const char *name, const char *_default) +{ + const char *raw = os_getenv(name); + const char *ret; + + if (raw == NULL) { + ret = _default; + } else { + ret = raw; + } + + if (debug_get_bool_option_print()) { + U_LOG_RAW("%s=%s (%s)", name, ret, raw == NULL ? "nil" : raw); + } + + return ret; +} + +bool +debug_get_bool_option(const char *name, bool _default) +{ + const char *raw = os_getenv(name); + bool ret = raw == NULL ? _default : debug_string_to_bool(raw); + + if (debug_get_bool_option_print()) { + U_LOG_RAW("%s=%s (%s)", name, ret ? "TRUE" : "FALSE", raw == NULL ? "nil" : raw); + } + + return ret; +} + +enum debug_tristate_option +debug_get_tristate_option(const char *name) +{ + const char *raw = os_getenv(name); + enum debug_tristate_option ret = debug_string_to_tristate(raw); + + if (debug_get_bool_option_print()) { + const char *pretty_val = tristate_to_str(ret); + U_LOG_RAW("%s=%s (%s)", name, pretty_val, raw == NULL ? "nil" : raw); + } + + return ret; +} + +long +debug_get_num_option(const char *name, long _default) +{ + const char *raw = os_getenv(name); + long ret = debug_string_to_num(raw, _default); + + if (debug_get_bool_option_print()) { + U_LOG_RAW("%s=%li (%s)", name, ret, raw == NULL ? "nil" : raw); + } + + return ret; +} + +float +debug_get_float_option(const char *name, float _default) +{ + const char *raw = os_getenv(name); + float ret = debug_string_to_float(raw, _default); + + if (debug_get_bool_option_print()) { + U_LOG_RAW("%s=%f (%s)", name, ret, raw == NULL ? "nil" : raw); + } + + return ret; +} + enum u_logging_level debug_get_log_option(const char *name, enum u_logging_level _default) { const char *raw = os_getenv(name); - enum u_logging_level ret; - - if (raw == NULL) { - ret = _default; - } else if (is_str_in_start_of(raw, "trace")) { - ret = U_LOGGING_TRACE; - } else if (is_str_in_start_of(raw, "debug")) { - ret = U_LOGGING_DEBUG; - } else if (is_str_in_start_of(raw, "info")) { - ret = U_LOGGING_INFO; - } else if (is_str_in_start_of(raw, "warn")) { - ret = U_LOGGING_WARN; - } else if (is_str_in_start_of(raw, "error")) { - ret = U_LOGGING_ERROR; - } else { - ret = _default; - } + enum u_logging_level ret = debug_string_to_log_level(raw, _default); if (debug_get_bool_option_print()) { - U_LOG_RAW("%s=%s (%s)", name, level_to_str(ret), raw == NULL ? "nil" : raw); + const char *pretty_val = level_to_str(ret); + U_LOG_RAW("%s=%s (%s)", name, pretty_val, raw == NULL ? "nil" : raw); } return ret; diff --git a/src/xrt/auxiliary/util/u_debug.h b/src/xrt/auxiliary/util/u_debug.h index 40cd37dac..543edcf15 100644 --- a/src/xrt/auxiliary/util/u_debug.h +++ b/src/xrt/auxiliary/util/u_debug.h @@ -1,4 +1,4 @@ -// Copyright 2019, Collabora, Ltd. +// Copyright 2019-2023, Collabora, Ltd. // SPDX-License-Identifier: BSL-1.0 /*! * @file @@ -26,18 +26,44 @@ enum debug_tristate_option DEBUG_TRISTATE_ON }; -const char * -debug_get_option(const char *name, const char *_default); + +/* + * + * Conversion functions. + * + */ bool debug_string_to_bool(const char *string); enum debug_tristate_option -debug_get_tristate_option(const char *name); +debug_string_to_tristate(const char *string); + +long +debug_string_to_num(const char *string, long _default); + +float +debug_string_to_float(const char *string, float _default); + +enum u_logging_level +debug_string_to_log_level(const char *string, enum u_logging_level _default); + + +/* + * + * Get functions. + * + */ + +const char * +debug_get_option(const char *name, const char *_default); bool debug_get_bool_option(const char *name, bool _default); +enum debug_tristate_option +debug_get_tristate_option(const char *name); + long debug_string_to_num(const char *string, long _default); @@ -50,6 +76,13 @@ debug_get_float_option(const char *name, float _default); enum u_logging_level debug_get_log_option(const char *name, enum u_logging_level _default); + +/* + * + * Get once helpers. + * + */ + #define DEBUG_GET_ONCE_OPTION(suffix, name, _default) \ static const char *debug_get_option_##suffix(void) \ { \