2020-07-03 13:28:55 +00:00
|
|
|
// Copyright 2019-2020, Collabora, Ltd.
|
2019-03-18 05:52:32 +00:00
|
|
|
// SPDX-License-Identifier: BSL-1.0
|
|
|
|
/*!
|
|
|
|
* @file
|
|
|
|
* @brief Small debug helpers.
|
|
|
|
* @author Jakob Bornecrantz <jakob@collabora.com>
|
2019-04-06 11:28:56 +00:00
|
|
|
* @ingroup aux_util
|
2019-03-18 05:52:32 +00:00
|
|
|
*
|
|
|
|
* Debug get option helpers heavily inspired from mesa ones.
|
|
|
|
*/
|
|
|
|
|
2020-07-03 13:28:55 +00:00
|
|
|
#include "util/u_debug.h"
|
|
|
|
#include "util/u_logging.h"
|
|
|
|
|
2019-03-18 05:52:32 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
|
|
DEBUG_GET_ONCE_BOOL_OPTION(print, "XRT_PRINT_OPTIONS", false)
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
os_getenv(const char *name)
|
|
|
|
{
|
|
|
|
return getenv(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
debug_get_option(const char *name, const char *_default)
|
|
|
|
{
|
|
|
|
const char *raw = getenv(name);
|
|
|
|
const char *ret;
|
|
|
|
|
|
|
|
if (raw == NULL) {
|
|
|
|
ret = _default;
|
|
|
|
} else {
|
|
|
|
ret = raw;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (debug_get_bool_option_print()) {
|
2020-07-03 13:28:55 +00:00
|
|
|
U_LOG_RAW("%s=%s (%s)", name, ret, raw == NULL ? "nil" : raw);
|
2019-03-18 05:52:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
debug_get_bool_option(const char *name, bool _default)
|
|
|
|
{
|
|
|
|
const char *raw = os_getenv(name);
|
|
|
|
bool ret;
|
|
|
|
|
|
|
|
if (raw == NULL) {
|
|
|
|
ret = _default;
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (debug_get_bool_option_print()) {
|
2020-07-03 13:28:55 +00:00
|
|
|
U_LOG_RAW("%s=%s (%s)", name, ret ? "TRUE" : "FALSE",
|
|
|
|
raw == NULL ? "nil" : raw);
|
2019-03-18 05:52:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
long
|
|
|
|
debug_get_num_option(const char *name, long _default)
|
|
|
|
{
|
|
|
|
const char *raw = os_getenv(name);
|
|
|
|
long ret;
|
|
|
|
|
|
|
|
if (raw == NULL) {
|
|
|
|
ret = _default;
|
|
|
|
} else {
|
|
|
|
char *endptr;
|
|
|
|
|
|
|
|
ret = strtol(raw, &endptr, 0);
|
|
|
|
// Restore the default value when no digits were found.
|
|
|
|
if (raw == endptr) {
|
|
|
|
ret = _default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (debug_get_bool_option_print()) {
|
2020-07-03 13:28:55 +00:00
|
|
|
U_LOG_RAW("%s=%li (%s)", name, ret, raw == NULL ? "nil" : raw);
|
2019-03-18 05:52:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()) {
|
2020-07-03 13:28:55 +00:00
|
|
|
U_LOG_RAW("%s=%f (%s)", name, ret, raw == NULL ? "nil" : raw);
|
2019-03-18 05:52:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|