| 1 | /* |
|---|
| 2 | Copyright (C) 2011 J. Alberdi, M. Oliveira, Y. Pouillon, and M. Verstraete |
|---|
| 3 | |
|---|
| 4 | This program is free software; you can redistribute it and/or modify |
|---|
| 5 | it under the terms of the GNU Lesser General Public License as published by |
|---|
| 6 | the Free Software Foundation; either version 3 of the License, or |
|---|
| 7 | (at your option) any later version. |
|---|
| 8 | |
|---|
| 9 | This program is distributed in the hope that it will be useful, |
|---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | GNU Lesser General Public License for more details. |
|---|
| 13 | |
|---|
| 14 | You should have received a copy of the GNU Lesser General Public License |
|---|
| 15 | along with this program; if not, write to the Free Software |
|---|
| 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 17 | |
|---|
| 18 | $Id$ |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | #ifndef PSPIO_ERROR_H |
|---|
| 22 | #define PSPIO_ERROR_H |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * @file pspio_error.h |
|---|
| 26 | * @brief Error codes and handlers |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | #include <stdlib.h> |
|---|
| 30 | |
|---|
| 31 | #if defined HAVE_CONFIG_H |
|---|
| 32 | #include "config.h" |
|---|
| 33 | #endif |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | #define PSPIO_SUCCESS 0 |
|---|
| 37 | #define PSPIO_ERROR -1 |
|---|
| 38 | #define PSPIO_ENOFILE 1 |
|---|
| 39 | #define PSPIO_EIO 2 |
|---|
| 40 | #define PSPIO_EVALUE 3 |
|---|
| 41 | #define PSPIO_EFILE_FORMAT 4 |
|---|
| 42 | #define PSPIO_ETYPE 5 |
|---|
| 43 | #define PSPIO_EGSL 6 |
|---|
| 44 | #define PSPIO_ENOMEM 7 |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | /********************************************************************** |
|---|
| 48 | * Data structures * |
|---|
| 49 | **********************************************************************/ |
|---|
| 50 | |
|---|
| 51 | /** |
|---|
| 52 | * Global error handling structure |
|---|
| 53 | */ |
|---|
| 54 | typedef struct { |
|---|
| 55 | int id; /**< ID of the error */ |
|---|
| 56 | char *filename; /**< name of the file where the error appeared */ |
|---|
| 57 | int line; /**< line number in the file where the error appeared */ |
|---|
| 58 | struct pspio_error_t *next; /**< next error in the chain */ |
|---|
| 59 | } pspio_error_t; |
|---|
| 60 | |
|---|
| 61 | /* The following is a VERY bad trick. Whoever finds better is welcome to |
|---|
| 62 | * speak. */ |
|---|
| 63 | static int pspio_error_tmp_id = 0; |
|---|
| 64 | static pspio_error_t *pspio_error_chain = NULL; |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | /********************************************************************** |
|---|
| 68 | * Routines * |
|---|
| 69 | **********************************************************************/ |
|---|
| 70 | |
|---|
| 71 | /** |
|---|
| 72 | * Add an error to the chain |
|---|
| 73 | * @param[in] new_error: new error to add |
|---|
| 74 | * @return error code |
|---|
| 75 | */ |
|---|
| 76 | int pspio_error_add(const char *filename, const int line); |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | /** |
|---|
| 80 | * Clear the error chain |
|---|
| 81 | * @return error code |
|---|
| 82 | */ |
|---|
| 83 | int pspio_error_free(void); |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | /** |
|---|
| 87 | * Pop the first available error |
|---|
| 88 | * @return error structure pointer |
|---|
| 89 | */ |
|---|
| 90 | pspio_error_t *pspio_error_pop(void); |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | /** |
|---|
| 94 | * Displays an error message. |
|---|
| 95 | * @param[in] error_id: integer identifying the error. |
|---|
| 96 | * @param[in] filename: source filename (use NULL if none). |
|---|
| 97 | * @param[in] line: line number in the source file (ignored if filename |
|---|
| 98 | * is NULL). |
|---|
| 99 | * @return string with error message. |
|---|
| 100 | */ |
|---|
| 101 | void pspio_error_show(const int error_id, const char *filename, |
|---|
| 102 | const int line); |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | /** |
|---|
| 106 | * Returns a string with error description. |
|---|
| 107 | * @param[in] error_id: integer identifying the error. |
|---|
| 108 | * @return string with error message. |
|---|
| 109 | */ |
|---|
| 110 | const char *pspio_error_str(const int pspio_errorid); |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | /********************************************************************** |
|---|
| 114 | * Macros * |
|---|
| 115 | **********************************************************************/ |
|---|
| 116 | |
|---|
| 117 | /** |
|---|
| 118 | * Libpspio-specific assert |
|---|
| 119 | * @param[in] condition: condition to check |
|---|
| 120 | * @param[in] error_id: error code to set if condition is false |
|---|
| 121 | */ |
|---|
| 122 | #define ASSERT(condition, error_id) \ |
|---|
| 123 | pspio_error_tmp_id = ( condition ) ? PSPIO_SUCCESS : error_id; \ |
|---|
| 124 | HANDLE_ERROR(pspio_error_tmp_id) |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | /** |
|---|
| 128 | * Error handler macro that frees memory upon error after calling a |
|---|
| 129 | * function |
|---|
| 130 | * @param[in] function_call: the function to be called with all parameters |
|---|
| 131 | * @param[in] type_to_free: type of variable to free when error |
|---|
| 132 | * @param[in] var_to_free: name of the variable to free when error |
|---|
| 133 | */ |
|---|
| 134 | #define HANDLE_ALLOC_ERROR(function_call, type_to_free, var_to_free) \ |
|---|
| 135 | pspio_error_tmp_id = function_call; \ |
|---|
| 136 | if ( pspio_error_tmp_id != PSPIO_SUCCESS ) { \ |
|---|
| 137 | pspio_ ## type_to_free ## _free(var_to_free); \ |
|---|
| 138 | pspio_error_add(__FILE__, __LINE__); \ |
|---|
| 139 | return pspio_error_tmp_id; \ |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | /** |
|---|
| 144 | * Error handler macro for fatal errors |
|---|
| 145 | * @param[in] error_id: error code to set before aborting |
|---|
| 146 | */ |
|---|
| 147 | #define HANDLE_FATAL_ERROR(condition, error_id) \ |
|---|
| 148 | pspio_error_tmp_id = ( condition ) ? error_id : PSPIO_SUCCESS; \ |
|---|
| 149 | if ( pspio_error_tmp_id != PSPIO_SUCCESS ) { \ |
|---|
| 150 | pspio_error_show(pspio_error_tmp_id, __FILE__, __LINE__); \ |
|---|
| 151 | exit(1); \ |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | /** |
|---|
| 156 | * Error handler macro for function calls |
|---|
| 157 | * @param[in] function_call: the function to be called with all parameters |
|---|
| 158 | */ |
|---|
| 159 | #define HANDLE_FUNC_ERROR(function_call) \ |
|---|
| 160 | pspio_error_tmp_id = function_call; \ |
|---|
| 161 | if ( pspio_error_tmp_id != PSPIO_SUCCESS ) { \ |
|---|
| 162 | pspio_error_add(__FILE__, __LINE__); \ |
|---|
| 163 | return pspio_error_tmp_id; \ |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | /** |
|---|
| 168 | * Basic error handler macro |
|---|
| 169 | * @param[id] error_id: error code to check |
|---|
| 170 | */ |
|---|
| 171 | #define HANDLE_ERROR(error_id) \ |
|---|
| 172 | if ( error_id != PSPIO_SUCCESS ) { \ |
|---|
| 173 | pspio_error_tmp_id = error_id; \ |
|---|
| 174 | pspio_error_add(__FILE__, __LINE__); \ |
|---|
| 175 | return error_id; \ |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | #endif |
|---|