src/lib/error.h
branchnew-lib-errors
changeset 217 7728d6ec3abf
parent 216 a10ba529ae39
child 218 5229a5d098b2
equal deleted inserted replaced
216:a10ba529ae39 217:7728d6ec3abf
     9  * This is designed to support multiple "namespaces" of errors, which are entirely independent of eachother. These
     9  * This is designed to support multiple "namespaces" of errors, which are entirely independent of eachother. These
    10  * namespaces can then reference one another, to build a 'tree' of useable error codes. Hence, each 'level' of this
    10  * namespaces can then reference one another, to build a 'tree' of useable error codes. Hence, each 'level' of this
    11  * tree has it's own set of error codes, and then error info contexts contain a stack of these error codes, which can
    11  * tree has it's own set of error codes, and then error info contexts contain a stack of these error codes, which can
    12  * then be used to trace the error down.
    12  * then be used to trace the error down.
    13  */
    13  */
       
    14 #include <stdbool.h>
    14 
    15 
    15 /**
    16 /**
    16  * The type used to represent a scalar error code, there are only unique per level.
    17  * The type used to represent a scalar error code, there are only unique per level.
    17  *
    18  *
    18  * Note that this type is signed to avoid nastyness with negating error codes, but all valid ERR_* codes are *always*
    19  * Note that this type is signed to avoid nastyness with negating error codes, but all valid ERR_* codes are *always*
   131         const struct error_list *list;
   132         const struct error_list *list;
   132 
   133 
   133     } stack[ERROR_DEPTH_MAX];
   134     } stack[ERROR_DEPTH_MAX];
   134 
   135 
   135     /** Current top of stack for accumulating errors, NULL means empty stack */
   136     /** Current top of stack for accumulating errors, NULL means empty stack */
   136     struct error_info_level *cur;
   137     struct error_item *cur;
   137 
   138 
   138     /** Type info for external error info */
   139     /** Type info for external error info */
   139     const struct error_extra_type *extra_type;
   140     const struct error_extra_type *extra_type;
   140 
   141 
   141     /** Value of external error info */
   142     /** Value of external error info */
   152  * Look up the error_type for for the given root table and error_info.
   153  * Look up the error_type for for the given root table and error_info.
   153  */
   154  */
   154 const struct error_type* error_lookup (const error_t *err);
   155 const struct error_type* error_lookup (const error_t *err);
   155 
   156 
   156 /**
   157 /**
   157  * Return the error name for the lowest level error in the given state's stack.
   158  * Return the error name for the given code.
   158  */
   159  */
   159 const char* error_name (const error_t *err);
   160 const char* error_name (const struct error_list *list, err_t code);
   160 
   161 
   161 /**
   162 /**
   162  * Maximum length of messages returned by error_msg
   163  * Maximum length of messages returned by error_msg
   163  */
   164  */
   164 #define ERROR_MSG_MAX 1024
   165 #define ERROR_MSG_MAX 1024
   173  */
   174  */
   174 bool error_cmp_eq (const error_t *a, const error_t *b);
   175 bool error_cmp_eq (const error_t *a, const error_t *b);
   175 
   176 
   176 
   177 
   177 /**
   178 /**
   178  * Reset the given error state to an empty state
   179  * Reset the given error state to an empty state.
   179  */
   180  *
   180 static inline void error_reset (error_t *err)
   181  * This is just the same as memset() with zero.
   181 {
   182  */
   182     memset(err, 0, sizeof(*err));
   183 void error_reset (error_t *err);
   183 }
       
   184 
   184 
   185 /**
   185 /**
   186  * Evaluates to the current top of the error stack, 
   186  * Evaluates to the current top of the error stack, 
   187  */
   187  */
   188 static struct error_item* error_top (const error_t *err)
   188 static struct error_item* error_top (error_t *err)
   189 {
   189 {
   190     return err->cur ? err->cur : err->stack;
   190     return err->cur ? err->cur : err->stack;
   191 }
   191 }
   192 
   192 
   193 /**
   193 /**
   273 /**
   273 /**
   274  * Used to mark unexpcted conditions for switch-default. The given val must be an integer, as passed to switch
   274  * Used to mark unexpcted conditions for switch-default. The given val must be an integer, as passed to switch
   275  */
   275  */
   276 #define NOT_REACHED(val) error_abort("%s = %#x", #val, (int) (val));
   276 #define NOT_REACHED(val) error_abort("%s = %#x", #val, (int) (val));
   277 
   277 
       
   278 /**
       
   279  * General-purpose errors that may be useful and don't belong in any more specific namespace.
       
   280  */
       
   281 enum general_error_code {
       
   282     ERR_GENERAL_NONE,
       
   283     ERR_MEM,                ///< memory allocation error
       
   284     ERR_NOT_IMPLEMENTED,    ///< function not implmented: <func>
       
   285     ERR_MISC,               ///< miscellaneous error: <error>
       
   286     ERR_CMD_OPT,            ///< invalid command line option: <error> - XXX: replace with something getopt
       
   287     ERR_UNKNOWN,            ///< unknown error
       
   288 };
       
   289 
       
   290 const struct error_list general_errors;
       
   291 
   278 #endif /* LIBQMSK_ERROR_H */
   292 #endif /* LIBQMSK_ERROR_H */