diff options
author | Chad Versace <chad.versace@linux.intel.com> | 2012-04-03 13:31:50 -0700 |
---|---|---|
committer | Chad Versace <chad.versace@linux.intel.com> | 2012-04-08 12:37:34 -0700 |
commit | 981c48fdddfe5068e88876710bd36e7d3cc58d33 (patch) | |
tree | aca54dc18dcf40e80b723f0bc9abba79172caba9 | |
parent | 3e5c8b67522b7307e966f618d6cb553ba3b6dee0 (diff) |
core: Add wcore_error module
This adds the following functions:
wcore_error
wcore_errorf
wcore_error_get_code
wcore_error_get_message
Signed-off-by: Chad Versace <chad.versace@linux.intel.com>
-rw-r--r-- | src/waffle/core/wcore_error.c | 110 | ||||
-rw-r--r-- | src/waffle/core/wcore_error.h | 61 |
2 files changed, 171 insertions, 0 deletions
diff --git a/src/waffle/core/wcore_error.c b/src/waffle/core/wcore_error.c new file mode 100644 index 0000000..7d39f0a --- /dev/null +++ b/src/waffle/core/wcore_error.c @@ -0,0 +1,110 @@ +// Copyright 2012 Intel Corporation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "wcore_error.h" + +#include <stdarg.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> + +enum { + MESSAGE_MAX = 1023, +}; + +static bool wcore_error_is_enabled = true; +static int wcore_error_code = WAFFLE_NO_ERROR; +static char wcore_error_message[MESSAGE_MAX + 1]; // +1 for null + +void +_wcore_error_enable(void) +{ + wcore_error_is_enabled = true; +} + +void +_wcore_error_disable(void) +{ + wcore_error_is_enabled = false; +} + +void +wcore_error(int error) +{ + if (!wcore_error_is_enabled) + return; + + wcore_error_code = error; + wcore_error_message[0] = '\0'; +} + +void +wcore_errorf(int error, const char *format, ...) +{ + va_list ap; + + if (!wcore_error_is_enabled) + return; + + wcore_error_code = error; + va_start(ap, format); + vsnprintf(wcore_error_message, MESSAGE_MAX, format, ap); + va_end(ap); +} + +void +_wcore_error_internal(const char *file, int line, const char *format, ...) +{ + char *cur = wcore_error_message; + char *end = wcore_error_message + sizeof(wcore_error_message); + int printed; + + if (!wcore_error_is_enabled) + return; + + wcore_error_code = WAFFLE_INTERNAL_ERROR; + + printed = snprintf(cur, end - cur, + "waffle: internal error: %s:%d: ", file, line); + cur += printed; + + if (printed < 0 || cur >= end) + return; + + if (format) { + va_list ap; + + va_start(ap, format); + printed = vsnprintf(cur, end - cur, format, ap); + cur += printed; + va_end(ap); + + if (printed < 0 || cur >= end) + return; + } + + snprintf(cur, end - cur, " (report this bug to chad@chad-versace.us)"); +} + +int +wcore_error_get_code(void) +{ + return wcore_error_code; +} + +const char* +wcore_error_get_message(void) +{ + return wcore_error_message; +}
\ No newline at end of file diff --git a/src/waffle/core/wcore_error.h b/src/waffle/core/wcore_error.h new file mode 100644 index 0000000..3457842 --- /dev/null +++ b/src/waffle/core/wcore_error.h @@ -0,0 +1,61 @@ +// Copyright 2012 Intel Corporation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include <waffle/waffle_error.h> + +/// @brief Set error code for client. +/// +/// @param error is an `enum waffle_error`. +void +wcore_error(int error); + +/// @brief Set error code and message for client. +/// +/// @param error is an `enum waffle_error`. +/// @param format may be null. +void +wcore_errorf(int error, const char *format, ...); + +/// @brief Set error to WAFFLE_INTERNAL_ERROR with source location. +#define wcore_error_internal(format, ...) \ + _wcore_error_internal(__FILE__, __LINE__, format, __VA_ARGS__) + +/// @brief Execute a statement with errors disabled. +#define WCORE_ERROR_DISABLED(statement) \ + do { \ + _wcore_error_disable(); \ + statement \ + _wcore_error_enable(); \ + } while (0) + +/// @brief Get the last set error code. +int +wcore_error_get_code(void); + +/// @brief Get the last set error message. +const char* +wcore_error_get_message(void); + +/// @defgroup wcore_error private +/// @{ + +void +_wcore_error_internal(const char *file, int line, const char *format, ...); + +void _wcore_error_enable(void); +void _wcore_error_disable(void); + +/// @}
\ No newline at end of file |