root/src/libsir/include/sir/errors.h

/* [previous][next][first][last][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. _sir_validerror
  2. _sir_geterrcode
  3. __sir_fakefunc

   1 /*
   2  * errors.h
   3  *
   4  * Version: 2.2.5
   5  *
   6  * -----------------------------------------------------------------------------
   7  *
   8  * SPDX-License-Identifier: MIT
   9  *
  10  * Copyright (c) 2018-2024 Ryan M. Lederman <lederman@gmail.com>
  11  * Copyright (c) 2018-2024 Jeffrey H. Johnson <trnsz@pobox.com>
  12  *
  13  * Permission is hereby granted, free of charge, to any person obtaining a copy of
  14  * this software and associated documentation files (the "Software"), to deal in
  15  * the Software without restriction, including without limitation the rights to
  16  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  17  * the Software, and to permit persons to whom the Software is furnished to do so,
  18  * subject to the following conditions:
  19  *
  20  * The above copyright notice and this permission notice shall be included in all
  21  * copies or substantial portions of the Software.
  22  *
  23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  25  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  26  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  27  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  28  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29  *
  30  * -----------------------------------------------------------------------------
  31  */
  32 
  33 #ifndef _SIR_ERRORS_H_INCLUDED
  34 # define _SIR_ERRORS_H_INCLUDED
  35 
  36 # include "sir/types.h"
  37 
  38 /**
  39  * @addtogroup publictypes Types
  40  * @{
  41  */
  42 
  43 /** Error codes. */
  44 enum sir_errorcode {
  45     SIR_E_NOERROR   = 1,    /**< The operation completed successfully */
  46     SIR_E_NOTREADY  = 2,    /**< libsir has not been initialized */
  47     SIR_E_ALREADY   = 3,    /**< libsir is already initialized */
  48     SIR_E_DUPITEM   = 4,    /**< Item already managed by libsir */
  49     SIR_E_NOITEM    = 5,    /**< Item not managed by libsir */
  50     SIR_E_NOROOM    = 6,    /**< Maximum number of items already stored */
  51     SIR_E_OPTIONS   = 7,    /**< Option flags are invalid */
  52     SIR_E_LEVELS    = 8,    /**< Level flags are invalid */
  53     SIR_E_TEXTSTYLE = 9,    /**< Text style is invalid */
  54     SIR_E_STRING    = 10,   /**< Invalid string argument */
  55     SIR_E_NULLPTR   = 11,   /**< NULL pointer argument */
  56     SIR_E_INVALID   = 12,   /**< Invalid argument */
  57     SIR_E_NODEST    = 13,   /**< No destinations registered for level */
  58     SIR_E_UNAVAIL   = 14,   /**< Feature is disabled or unavailable */
  59     SIR_E_INTERNAL  = 15,   /**< An internal error has occurred */
  60     SIR_E_COLORMODE = 16,   /**< Color mode is invalid */
  61     SIR_E_TEXTATTR  = 17,   /**< Text attributes are invalid */
  62     SIR_E_TEXTCOLOR = 18,   /**< Text color is invalid for mode */
  63     SIR_E_PLUGINBAD = 19,   /**< Plugin module is malformed */
  64     SIR_E_PLUGINDAT = 20,   /**< Data produced by plugin is invalid */
  65     SIR_E_PLUGINVER = 21,   /**< Plugin interface version unsupported */
  66     SIR_E_PLUGINERR = 22,   /**< Plugin reported failure */
  67     SIR_E_PLATFORM  = 23,   /**< Platform error code %%d: %%s */
  68     SIR_E_UNKNOWN   = 4095, /**< Unknown error */
  69 };
  70 
  71 /** @} */
  72 
  73 # if defined(__cplusplus)
  74 extern "C" {
  75 # endif
  76 
  77 /** Creates an error code that (hopefully) doesn't conflict with any of those
  78  * defined by the platform. */
  79 # define _sir_mkerror(code) (((uint32_t)((code) & 0x7fffffffU) << 16) | 0x80000000U)
  80 
  81 /** Validates an internal error. */
  82 static inline
  83 bool _sir_validerror(uint32_t err) {
     /* [previous][next][first][last][top][bottom][index][help] */
  84     uint32_t masked = err & 0x8fffffffU;
  85     return masked >= 0x80000000U && masked <= 0x8fff0000U;
  86 }
  87 
  88 /** Extracts just the code from an internal error. */
  89 static inline
  90 uint16_t _sir_geterrcode(uint32_t err) {
     /* [previous][next][first][last][top][bottom][index][help] */
  91     return (err >> 16) & 0x7fffU;
  92 }
  93 
  94 /** Internal error codes. */
  95 # define _SIR_E_NOERROR   _sir_mkerror(SIR_E_NOERROR)
  96 # define _SIR_E_NOTREADY  _sir_mkerror(SIR_E_NOTREADY)
  97 # define _SIR_E_ALREADY   _sir_mkerror(SIR_E_ALREADY)
  98 # define _SIR_E_DUPITEM   _sir_mkerror(SIR_E_DUPITEM)
  99 # define _SIR_E_NOITEM    _sir_mkerror(SIR_E_NOITEM)
 100 # define _SIR_E_NOROOM    _sir_mkerror(SIR_E_NOROOM)
 101 # define _SIR_E_OPTIONS   _sir_mkerror(SIR_E_OPTIONS)
 102 # define _SIR_E_LEVELS    _sir_mkerror(SIR_E_LEVELS)
 103 # define _SIR_E_TEXTSTYLE _sir_mkerror(SIR_E_TEXTSTYLE)
 104 # define _SIR_E_STRING    _sir_mkerror(SIR_E_STRING)
 105 # define _SIR_E_NULLPTR   _sir_mkerror(SIR_E_NULLPTR)
 106 # define _SIR_E_INVALID   _sir_mkerror(SIR_E_INVALID)
 107 # define _SIR_E_NODEST    _sir_mkerror(SIR_E_NODEST)
 108 # define _SIR_E_UNAVAIL   _sir_mkerror(SIR_E_UNAVAIL)
 109 # define _SIR_E_INTERNAL  _sir_mkerror(SIR_E_INTERNAL)
 110 # define _SIR_E_COLORMODE _sir_mkerror(SIR_E_COLORMODE)
 111 # define _SIR_E_TEXTATTR  _sir_mkerror(SIR_E_TEXTATTR)
 112 # define _SIR_E_TEXTCOLOR _sir_mkerror(SIR_E_TEXTCOLOR)
 113 # define _SIR_E_PLUGINBAD _sir_mkerror(SIR_E_PLUGINBAD)
 114 # define _SIR_E_PLUGINDAT _sir_mkerror(SIR_E_PLUGINDAT)
 115 # define _SIR_E_PLUGINVER _sir_mkerror(SIR_E_PLUGINVER)
 116 # define _SIR_E_PLUGINERR _sir_mkerror(SIR_E_PLUGINERR)
 117 # define _SIR_E_PLATFORM  _sir_mkerror(SIR_E_PLATFORM)
 118 # define _SIR_E_UNKNOWN   _sir_mkerror(SIR_E_UNKNOWN)
 119 
 120 bool __sir_seterror(uint32_t err, const char* func, const char* file, uint32_t line);
 121 # define _sir_seterror(err) __sir_seterror(err, __func__, __file__, __LINE__)
 122 
 123 void __sir_setoserror(int code, const char* msg, const char* func,
 124     const char* file, uint32_t line);
 125 
 126 /** Handle an OS/libc error. */
 127 bool __sir_handleerr(int code, const char* func, const char* file, uint32_t line);
 128 # define _sir_handleerr(code) __sir_handleerr(code, __func__, __file__, __LINE__)
 129 
 130 # if defined(__WIN__)
 131 void _sir_invalidparameter(const wchar_t* expr, const wchar_t* func, const wchar_t* file,
 132     unsigned int line, uintptr_t reserved);
 133 
 134 bool __sir_handlewin32err(DWORD code, const char* func, const char* file, uint32_t line);
 135 #  define _sir_handlewin32err(code) __sir_handlewin32err((DWORD)code, __func__, __file__, __LINE__)
 136 # endif
 137 
 138 /** Retrieves a formatted message for the last error that occurred on the calling
 139  * thread and returns the associated internal error code. */
 140 uint32_t _sir_geterror(char message[SIR_MAXERROR]);
 141 
 142 /** Returns information about the last error that occurred on the calling thread. */
 143 void _sir_geterrorinfo(sir_errorinfo* err);
 144 
 145 /** Resets TLS error information. */
 146 void _sir_reset_tls_error(void);
 147 
 148 # if defined(SIR_SELFLOG)
 149 /** Log an internal debug message to stderr. */
 150 PRINTF_FORMAT_ATTR(4, 5)
 151 void __sir_selflog(const char* func, const char* file, uint32_t line, PRINTF_FORMAT const char* format, ...);
 152 #  define _sir_selflog(...) __sir_selflog(__func__, __file__, __LINE__, __VA_ARGS__)
 153 # else
 154 static inline
 155 void __sir_fakefunc(const char* format, ...) { (void)format; }
     /* [previous][next][first][last][top][bottom][index][help] */
 156 #  define _sir_selflog(...) __sir_fakefunc(__VA_ARGS__)
 157 # endif
 158 
 159 # if defined(__cplusplus)
 160 }
 161 # endif
 162 
 163 #endif /* !_SIR_ERRORS_H_INCLUDED */

/* [previous][next][first][last][top][bottom][index][help] */