diff options
author | Chad Versace <chad.versace@linux.intel.com> | 2014-03-22 10:53:52 -0700 |
---|---|---|
committer | Chad Versace <chad.versace@linux.intel.com> | 2014-04-17 13:12:40 -0700 |
commit | 13ea3cc10f05754b4385148b536acb515b75363c (patch) | |
tree | a3ae82c57f09cb5104955febeaad50b0150fc1b8 | |
parent | d2f431468f7dbf73a4144e6d93c31236c6eaf87a (diff) |
util/log: Add logging module
Add new header piglit-log.h which exposes the following functions:
piglit_log_get_opt()
piglit_log_set_opt()
piglit_loge()
piglit_logi()
This new module should help to reduce redundant logging boilerplate
found in many Piglit files.
Signed-off-by: Chad Versace <chad.versace@linux.intel.com>
-rw-r--r-- | tests/util/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/util/piglit-log.c | 117 | ||||
-rw-r--r-- | tests/util/piglit-log.h | 67 | ||||
-rw-r--r-- | tests/util/piglit-util.h | 2 |
4 files changed, 187 insertions, 0 deletions
diff --git a/tests/util/CMakeLists.txt b/tests/util/CMakeLists.txt index e678d5878..3a97daf8c 100644 --- a/tests/util/CMakeLists.txt +++ b/tests/util/CMakeLists.txt @@ -10,6 +10,7 @@ set(UTIL_INCLUDES ) set(UTIL_SOURCES + piglit-log.c piglit-util.c ) diff --git a/tests/util/piglit-log.c b/tests/util/piglit-log.c new file mode 100644 index 000000000..1cf9b10dd --- /dev/null +++ b/tests/util/piglit-log.c @@ -0,0 +1,117 @@ +/* + * Copyright 2014 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include <inttypes.h> +#include <stdio.h> +#include <stdarg.h> + +#include "piglit-log.h" +#include "piglit-util.h" + +struct piglit_log_opt_list { + intptr_t val; + bool is_env_set; +}; + +/** + * Array of logging options, one entry for each value of enum piglit_log_opt. + */ +static struct piglit_log_opt_list opts[PIGLIT_LOG_OPT_MAX + 1]; + +static void +get_env_overrides(void) +{ + static bool once = true; + const char *env = NULL; + + if (!once) { + return; + } + once = false; + + env = getenv("PIGLIT_LOG_PRINT_TID"); + if (env && !streq(env, "")) { + opts[PIGLIT_LOG_PRINT_TID].is_env_set = true; + opts[PIGLIT_LOG_PRINT_TID].val = atoi(env); + } +} + +/** Is option out of bounds? */ +static bool +is_opt_oob(enum piglit_log_opt opt) +{ + return opt < 0 || opt > PIGLIT_LOG_OPT_MAX; +} + +intptr_t +piglit_log_get_opt(enum piglit_log_opt opt) +{ + get_env_overrides(); + + if (is_opt_oob(opt)) { + return 0; + } + + return opts[opt].val; +} + +void +piglit_log_set_opt(enum piglit_log_opt opt, intptr_t value) { + get_env_overrides(); + + if (is_opt_oob(opt) || opts[opt].is_env_set) { + return; + } + + opts[opt].val = value; +} + +static void +piglit_log_tagv(const char *tag, const char *fmt, va_list ap) +{ + printf("piglit"); + if (piglit_log_get_opt(PIGLIT_LOG_PRINT_TID)) { + printf("(%"PRIu64")", piglit_gettid()); + } + printf(": %s: ", tag); + vprintf(fmt, ap); + printf("\n"); +} + +void +piglit_loge(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + piglit_log_tagv("error", fmt, ap); + va_end(ap); +} + +void +piglit_logi(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + piglit_log_tagv("info", fmt, ap); + va_end(ap); +} diff --git a/tests/util/piglit-log.h b/tests/util/piglit-log.h new file mode 100644 index 000000000..13037be54 --- /dev/null +++ b/tests/util/piglit-log.h @@ -0,0 +1,67 @@ +/* + * Copyright 2014 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#pragma once + +#include <stdint.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * \brief Piglit logging options + * + * Options can be set with the setter function piglit_log_set_opt() as well as + * environment variables. For each option, the environment variable of the + * same name, if set to a non-empty value, overrides any value set with the + * setter function. + */ +enum piglit_log_opt { + /** + * Print thread id in log messages. + * Option type: bool + */ + PIGLIT_LOG_PRINT_TID = 0, + + /** Fake option. This is the maximum value of piglit_log_opt. */ + PIGLIT_LOG_OPT_MAX = 0, +}; + +intptr_t +piglit_log_get_opt(enum piglit_log_opt); + +void +piglit_log_set_opt(enum piglit_log_opt opt, intptr_t value); + +/** Log an error.message. */ +void +piglit_loge(const char *fmt, ...); + +/** Log an info message. */ +void +piglit_logi(const char *fmt, ...); + +#ifdef __cplusplus +} /* end extern "C" */ +#endif diff --git a/tests/util/piglit-util.h b/tests/util/piglit-util.h index 779afca60..23e053995 100644 --- a/tests/util/piglit-util.h +++ b/tests/util/piglit-util.h @@ -49,6 +49,8 @@ extern "C" { #include <math.h> #include <float.h> +#include "piglit-log.h" + #if defined(_MSC_VER) #define snprintf sprintf_s |