From 03c549700b06d7be69a4d3e3aeda5fd181cab918 Mon Sep 17 00:00:00 2001 From: Chia-I Wu Date: Sat, 3 Jan 2015 14:47:32 +0800 Subject: icd: clean up logging functions Clean up and move logging functions to icd-log.[ch]. --- icd/common/CMakeLists.txt | 1 + icd/common/icd-enumerate-drm.c | 1 + icd/common/icd-log.c | 185 +++++++++++++++++++++++++++++++++++++++++ icd/common/icd-log.h | 54 ++++++++++++ icd/common/icd.c | 153 +--------------------------------- icd/common/icd.h | 25 ------ 6 files changed, 245 insertions(+), 174 deletions(-) create mode 100644 icd/common/icd-log.c create mode 100644 icd/common/icd-log.h diff --git a/icd/common/CMakeLists.txt b/icd/common/CMakeLists.txt index cd78903a..b0ce4032 100644 --- a/icd/common/CMakeLists.txt +++ b/icd/common/CMakeLists.txt @@ -8,6 +8,7 @@ set(ICD_SOURCES icd-alloc.c icd-dispatch-entrypoints.c icd-format.c + icd-log.c icd-utils.c) set(ICD_REQUIRED_MODULES) diff --git a/icd/common/icd-enumerate-drm.c b/icd/common/icd-enumerate-drm.c index c00bd5d1..f65ceca4 100644 --- a/icd/common/icd-enumerate-drm.c +++ b/icd/common/icd-enumerate-drm.c @@ -31,6 +31,7 @@ #include #include "icd-alloc.h" +#include "icd-log.h" #include "icd-utils.h" #include "icd-enumerate-drm.h" diff --git a/icd/common/icd-log.c b/icd/common/icd-log.c new file mode 100644 index 00000000..15d8ac87 --- /dev/null +++ b/icd/common/icd-log.c @@ -0,0 +1,185 @@ +/* + * XGL + * + * Copyright (C) 2014 LunarG, Inc. + * + * 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 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. + * + * Authors: + * Chia-I Wu + */ + +#include +#include +#include "icd-log.h" + +struct icd_logger_callback { + XGL_DBG_MSG_CALLBACK_FUNCTION func; + XGL_VOID *user_data; + + struct icd_logger_callback *next; +}; + +struct icd_logger { + bool debug_echo_enable; + bool break_on_error; + bool break_on_warning; + + struct icd_logger_callback *callbacks; +}; + +static struct icd_logger icd_logger; + +XGL_RESULT icd_logger_set_bool(XGL_DBG_GLOBAL_OPTION option, bool enable) +{ + XGL_RESULT res = XGL_SUCCESS; + + switch (option) { + case XGL_DBG_OPTION_DEBUG_ECHO_ENABLE: + icd_logger.debug_echo_enable = enable; + break; + case XGL_DBG_OPTION_BREAK_ON_ERROR: + icd_logger.break_on_error = enable; + break; + case XGL_DBG_OPTION_BREAK_ON_WARNING: + icd_logger.break_on_warning = enable; + break; + default: + res = XGL_ERROR_INVALID_VALUE; + break; + } + + return res; +} + +XGL_RESULT icd_logger_add_callback(XGL_DBG_MSG_CALLBACK_FUNCTION func, + void *user_data) +{ + struct icd_logger_callback *cb; + + /* use malloc() as allocator may not be initialized yet */ + cb = malloc(sizeof(*cb)); + if (!cb) + return XGL_ERROR_OUT_OF_MEMORY; + + cb->func = func; + cb->user_data = user_data; + + cb->next = icd_logger.callbacks; + icd_logger.callbacks = cb; + + return XGL_SUCCESS; +} + +XGL_RESULT icd_logger_remove_callback(XGL_DBG_MSG_CALLBACK_FUNCTION func) +{ + struct icd_logger_callback *cb = icd_logger.callbacks; + bool found = false; + + /* remove all matches */ + while (cb) { + struct icd_logger_callback *next = cb->next; + + if (cb->func == func) { + free(cb); + found = true; + } + + cb = next; + } + + return (found) ? XGL_SUCCESS : XGL_ERROR_INVALID_POINTER; +} + +void icd_logger_clear_callbacks(void) +{ + struct icd_logger_callback *cb = icd_logger.callbacks; + + while (cb) { + struct icd_logger_callback *next = cb->next; + free(cb); + cb = next; + } + + icd_logger.callbacks = NULL; +} + +static void icd_log_str(XGL_DBG_MSG_TYPE msg_type, + XGL_VALIDATION_LEVEL validation_level, + XGL_BASE_OBJECT src_object, + size_t location, int32_t msg_code, + const char *msg) +{ + const struct icd_logger_callback *cb = icd_logger.callbacks; + + if (icd_logger.debug_echo_enable || !cb) { + fputs(msg, stderr); + fputc('\n', stderr); + } + + while (cb) { + cb->func(msg_type, XGL_VALIDATION_LEVEL_0, XGL_NULL_HANDLE, 0, + msg_code, msg, cb->user_data); + cb = cb->next; + } + + switch (msg_type) { + case XGL_DBG_MSG_ERROR: + if (icd_logger.break_on_error) + abort(); + /* fall through */ + case XGL_DBG_MSG_WARNING: + if (icd_logger.break_on_warning) + abort(); + break; + default: + break; + } +} + +void icd_logv(XGL_DBG_MSG_TYPE msg_type, + XGL_VALIDATION_LEVEL validation_level, + XGL_BASE_OBJECT src_object, + size_t location, int32_t msg_code, + const char *format, va_list ap) +{ + char msg[256]; + int ret; + + ret = vsnprintf(msg, sizeof(msg), format, ap); + if (ret >= sizeof(msg) || ret < 0) + msg[sizeof(msg) - 1] = '\0'; + + icd_log_str(msg_type, validation_level, src_object, + location, msg_code, msg); +} + +void icd_log(XGL_DBG_MSG_TYPE msg_type, + XGL_VALIDATION_LEVEL validation_level, + XGL_BASE_OBJECT src_object, + size_t location, int32_t msg_code, + const char *format, ...) +{ + va_list ap; + + va_start(ap, format); + icd_logv(msg_type, validation_level, src_object, + location, msg_code, format, ap); + va_end(ap); +} diff --git a/icd/common/icd-log.h b/icd/common/icd-log.h new file mode 100644 index 00000000..7d88f819 --- /dev/null +++ b/icd/common/icd-log.h @@ -0,0 +1,54 @@ +/* + * XGL + * + * Copyright (C) 2014 LunarG, Inc. + * + * 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 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. + * + * Authors: + * Chia-I Wu + */ + +#ifndef ICD_LOG_H +#define ICD_LOG_H + +#include +#include "icd-utils.h" +#include "icd.h" + +XGL_RESULT icd_logger_set_bool(XGL_DBG_GLOBAL_OPTION option, bool enable); + +XGL_RESULT icd_logger_add_callback(XGL_DBG_MSG_CALLBACK_FUNCTION func, + void *user_data); +XGL_RESULT icd_logger_remove_callback(XGL_DBG_MSG_CALLBACK_FUNCTION func); +void icd_logger_clear_callbacks(void); + +void icd_logv(XGL_DBG_MSG_TYPE msg_type, + XGL_VALIDATION_LEVEL validation_level, + XGL_BASE_OBJECT src_object, + size_t location, int32_t msg_code, + const char *format, va_list ap); + +void icd_log(XGL_DBG_MSG_TYPE msg_type, + XGL_VALIDATION_LEVEL validation_level, + XGL_BASE_OBJECT src_object, + size_t location, int32_t msg_code, + const char *format, ...); + +#endif /* ICD_LOG_H */ diff --git a/icd/common/icd.c b/icd/common/icd.c index ee19dcdf..bc8af77e 100644 --- a/icd/common/icd.c +++ b/icd/common/icd.c @@ -25,158 +25,17 @@ * Chia-I Wu */ -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "icd-utils.h" +#include "icd-log.h" #include "icd.h" -struct icd_msg_callback { - XGL_DBG_MSG_CALLBACK_FUNCTION func; - XGL_VOID *data; - - struct icd_msg_callback *next; -}; - -struct icd { - struct icd_msg_callback *msg_callbacks; - - bool debug_echo_enable; - bool break_on_error; - bool break_on_warning; -}; - -static struct icd icd; - -void icd_msg(XGL_DBG_MSG_TYPE msg_type, - XGL_VALIDATION_LEVEL validation_level, - XGL_BASE_OBJECT src_object, - XGL_SIZE location, - XGL_INT msg_code, - const char *msg) -{ - const struct icd_msg_callback *cb = icd.msg_callbacks; - - if (icd.debug_echo_enable || !cb) { - fputs(msg, stderr); - fputc('\n', stderr); - } - - while (cb) { - cb->func(msg_type, XGL_VALIDATION_LEVEL_0, XGL_NULL_HANDLE, 0, - msg_code, msg, cb->data); - cb = cb->next; - } - - switch (msg_type) { - case XGL_DBG_MSG_ERROR: - if (icd.break_on_error) { - exit(1); - } - /* fall through */ - case XGL_DBG_MSG_WARNING: - if (icd.break_on_warning) { - exit(1); - } - break; - default: - break; - } -} - -void icd_vlog(XGL_DBG_MSG_TYPE msg_type, - XGL_VALIDATION_LEVEL validation_level, - XGL_BASE_OBJECT src_object, - XGL_SIZE location, - XGL_INT msg_code, - const char *format, va_list ap) -{ - char msg[256]; - int ret; - - ret = vsnprintf(msg, sizeof(msg), format, ap); - if (ret >= sizeof(msg) || ret < 0) { - msg[sizeof(msg) - 1] = '\0'; - } - - icd_msg(msg_type, validation_level, src_object, location, msg_code, msg); -} - -void icd_log(XGL_DBG_MSG_TYPE msg_type, - XGL_VALIDATION_LEVEL validation_level, - XGL_BASE_OBJECT src_object, - XGL_SIZE location, - XGL_INT msg_code, - const char *format, ...) -{ - va_list ap; - - va_start(ap, format); - icd_vlog(msg_type, validation_level, src_object, - location, msg_code, format, ap); - va_end(ap); -} - -void icd_clear_msg_callbacks(void) -{ - struct icd_msg_callback *cb = icd.msg_callbacks; - - while (cb) { - struct icd_msg_callback *next = cb->next; - free(cb); - cb = next; - } - - icd.msg_callbacks = NULL; -} - XGL_RESULT XGLAPI icdDbgRegisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, XGL_VOID* pUserData) { - struct icd_msg_callback *cb; - - cb = malloc(sizeof(*cb)); - if (!cb) - return XGL_ERROR_OUT_OF_MEMORY; - - cb->func = pfnMsgCallback; - cb->data = pUserData; - - cb->next = icd.msg_callbacks; - icd.msg_callbacks = cb; - - return XGL_SUCCESS; + return icd_logger_add_callback(pfnMsgCallback, pUserData); } XGL_RESULT XGLAPI icdDbgUnregisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback) { - struct icd_msg_callback *cb = icd.msg_callbacks; - - /* - * Find the first match (last registered). - * - * XXX What if the same callback function is registered more than once? - */ - while (cb) { - if (cb->func == pfnMsgCallback) { - break; - } - - cb = cb->next; - } - - if (!cb) - return XGL_ERROR_INVALID_POINTER; - - free(cb); - - return XGL_SUCCESS; + return icd_logger_remove_callback(pfnMsgCallback); } XGL_RESULT XGLAPI icdDbgSetGlobalOption(XGL_DBG_GLOBAL_OPTION dbgOption, XGL_SIZE dataSize, const XGL_VOID* pData) @@ -188,13 +47,9 @@ XGL_RESULT XGLAPI icdDbgSetGlobalOption(XGL_DBG_GLOBAL_OPTION dbgOption, XGL_SIZ switch (dbgOption) { case XGL_DBG_OPTION_DEBUG_ECHO_ENABLE: - icd.debug_echo_enable = *((const bool *) pData); - break; case XGL_DBG_OPTION_BREAK_ON_ERROR: - icd.break_on_error = *((const bool *) pData); - break; case XGL_DBG_OPTION_BREAK_ON_WARNING: - icd.break_on_warning = *((const bool *) pData); + res = icd_logger_set_bool(dbgOption, *((const bool *) pData)); break; default: res = XGL_ERROR_INVALID_VALUE; diff --git a/icd/common/icd.h b/icd/common/icd.h index 4e7906a0..56cb1429 100644 --- a/icd/common/icd.h +++ b/icd/common/icd.h @@ -28,8 +28,6 @@ #ifndef ICD_H #define ICD_H -#include - #include #include #include @@ -46,27 +44,4 @@ XGL_RESULT XGLAPI icdDbgRegisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsg XGL_RESULT XGLAPI icdDbgUnregisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback); XGL_RESULT XGLAPI icdDbgSetGlobalOption(XGL_DBG_GLOBAL_OPTION dbgOption, XGL_SIZE dataSize, const XGL_VOID* pData); -void icd_msg(XGL_DBG_MSG_TYPE msg_type, - XGL_VALIDATION_LEVEL validation_level, - XGL_BASE_OBJECT src_object, - XGL_SIZE location, - XGL_INT msg_code, - const char *msg); - -void icd_vlog(XGL_DBG_MSG_TYPE msg_type, - XGL_VALIDATION_LEVEL validation_level, - XGL_BASE_OBJECT src_object, - XGL_SIZE location, - XGL_INT msg_code, - const char *format, va_list ap); - -void icd_log(XGL_DBG_MSG_TYPE msg_type, - XGL_VALIDATION_LEVEL validation_level, - XGL_BASE_OBJECT src_object, - XGL_SIZE location, - XGL_INT msg_code, - const char *format, ...); - -void icd_clear_msg_callbacks(void); - #endif /* ICD_H */ -- cgit v1.2.3