| 1 | // **************************************************************************** |
| 2 | // Project: libguytools |
| 3 | // **************************************************************************** |
| 4 | // Programmer: Guy Voncken |
| 5 | // Police Grand-Ducale |
| 6 | // Service de Police Judiciaire |
| 7 | // Section Nouvelles Technologies |
| 8 | // **************************************************************************** |
| 9 | // Module: Error handling |
| 10 | // **************************************************************************** |
| 11 | |
| 12 | // Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, |
| 13 | // 2018, 2019 |
| 14 | // Guy Voncken |
| 15 | // |
| 16 | // This file is part of libguytools. |
| 17 | // |
| 18 | // libguytools is free software: you can redistribute it and/or modify |
| 19 | // it under the terms of the GNU General Public License as published by |
| 20 | // the Free Software Foundation, either version 2 of the License, or |
| 21 | // (at your option) any later version. |
| 22 | // |
| 23 | // libguytools is distributed in the hope that it will be useful, |
| 24 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 25 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 26 | // GNU General Public License for more details. |
| 27 | // |
| 28 | // You should have received a copy of the GNU General Public License |
| 29 | // along with libguytools. If not, see <http://www.gnu.org/licenses/>. |
| 30 | |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <stdarg.h> |
| 34 | #include <errno.h> |
| 35 | |
| 36 | #include "toolconstants.h" |
| 37 | #include "toolglobalid.h" |
| 38 | #include "toolerror.h" |
| 39 | |
| 40 | /* ------------------------------ */ |
| 41 | /* Type & structure definitions */ |
| 42 | /* ------------------------------ */ |
| 43 | |
| 44 | typedef struct |
| 45 | { |
| 46 | int ErrorCode; |
| 47 | const char *pMsg; |
| 48 | } t_ToolErrorEntry, *t_pToolErrorEntry; |
| 49 | |
| 50 | typedef struct |
| 51 | { |
| 52 | t_pToolErrorLogFn pLogFn; |
| 53 | t_pToolErrorEntry pErrorCodeArr; |
| 54 | unsigned int ErrorCodeArrEntries; |
| 55 | unsigned int ErrorCodeArrLen; |
| 56 | } t_ToolErrorLocal; |
| 57 | |
| 58 | /* ------------------------------ */ |
| 59 | /* Variables */ |
| 60 | /* ------------------------------ */ |
| 61 | |
| 62 | static t_ToolErrorLocal ToolErrorLocal; |
| 63 | static int Initialised = FALSE; |
| 64 | |
| 65 | APIRET ToolErrorLog (const char *pFileName, const char *pFunctionName, int LineNr, const char * pFormat, ...) |
| 66 | { |
| 67 | va_list VaList; |
| 68 | va_start(VaList, pFormat); |
| 69 | |
| 70 | if (ToolErrorLocal.pLogFn != nullptr) |
| 71 | { |
| 72 | ToolErrorLocal.pLogFn (pFileName, pFunctionName, LineNr, pFormat, VaList); |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | printf ("\nError in %s, %s, %d:", pFileName, pFunctionName, LineNr); |
| 77 | vprintf (pFormat, VaList); |
| 78 | } |
| 79 | va_end(VaList); |
| 80 | |
| 81 | return NO_ERROR; |
| 82 | } |
| 83 | |
| 84 | /* ------------------------------ */ |
| 85 | /* Functions */ |
| 86 | /* ------------------------------ */ |
| 87 | |
| 88 | static APIRET ToolErrorFindEntry (APIRET ErrorCode, t_pToolErrorEntry *ppEntry) |
| 89 | { |
| 90 | t_pToolErrorEntry pEntry = nullptr; |
| 91 | unsigned int i; |
| 92 | |
| 93 | for (i=0; i<ToolErrorLocal.ErrorCodeArrEntries; i++) |
| 94 | { |
| 95 | pEntry = ToolErrorLocal.pErrorCodeArr + i; |
| 96 | if ((pEntry)->ErrorCode == ErrorCode) |
| 97 | break; |
| 98 | } |
| 99 | |
| 100 | if (i >= ToolErrorLocal.ErrorCodeArrEntries) |
| 101 | return TOOL_ERROR_ENTRY_NOT_FOUND; |
| 102 | |
| 103 | if (ppEntry) |
| 104 | *ppEntry = pEntry; |
| 105 | |
| 106 | return NO_ERROR; |
| 107 | } |
| 108 | |
| 109 | APIRET ToolErrorRegisterError (int ErrorCode, const char *pMsg) |
| 110 | { |
| 111 | t_pToolErrorEntry pEntry; |
| 112 | |
| 113 | if (Initialised == FALSE) |
| 114 | return TOOL_ERROR_NOT_INITIALISED; |
| 115 | |
| 116 | APIRET rc = ToolErrorFindEntry (ErrorCode, nullptr); |
| 117 | if (rc == TOOL_ERROR_ENTRY_NOT_FOUND) |
| 118 | { |
| 119 | pEntry = &(ToolErrorLocal.pErrorCodeArr[ToolErrorLocal.ErrorCodeArrEntries++]); |
| 120 | pEntry->ErrorCode = ErrorCode; |
| 121 | pEntry->pMsg = pMsg; |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | ToolErrorLog (__FFL__, "Duplicate ErrorCode %d", ErrorCode); |
| 126 | return TOOL_ERROR_DUPLICATE_ENTRY; |
| 127 | } |
| 128 | return NO_ERROR; |
| 129 | } |
| 130 | |
| 131 | APIRET ToolErrorGetMessage (APIRET ErrorCode, const char **ppMsg) |
| 132 | { |
| 133 | if (ErrorCode == NO_ERROR) |
| 134 | { |
| 135 | *ppMsg = "NO_ERROR"; |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | t_pToolErrorEntry pEntry; |
| 140 | |
| 141 | APIRET rc = ToolErrorFindEntry (ErrorCode, &pEntry); |
| 142 | if (rc == TOOL_ERROR_ENTRY_NOT_FOUND) |
| 143 | *ppMsg = nullptr; |
| 144 | else *ppMsg = pEntry->pMsg; |
| 145 | } |
| 146 | return NO_ERROR; |
| 147 | } |
| 148 | |
| 149 | const char *ToolErrorMessage (APIRET ErrorCode) |
| 150 | { |
| 151 | static char TmpStr [20]; // Attention: This is not multi-threading compatible! Should be no problem, as TmpStr |
| 152 | const char *pMsg; // only is used in the really very unlikely case where the error module has errors... |
| 153 | |
| 154 | APIRET rc = ToolErrorGetMessage (ErrorCode, &pMsg); |
| 155 | if (rc != NO_ERROR) |
| 156 | { |
| 157 | snprintf(TmpStr, sizeof(TmpStr), "[%d]", ErrorCode); |
| 158 | pMsg = TmpStr; |
| 159 | } |
| 160 | return pMsg; |
| 161 | } |
| 162 | |
| 163 | APIRET ToolErrorCheck (char const *pFileName, char const *pFunctionName, int LineNr, APIRET ErrorCode) |
| 164 | { |
| 165 | const char *pMsg; |
| 166 | |
| 167 | if (ErrorCode == NO_ERROR) |
| 168 | { |
| 169 | ToolErrorLog (pFileName, pFunctionName, LineNr, "%s called with ErrorCode=0", __FUNCTION__); |
| 170 | } |
| 171 | else |
| 172 | { |
| 173 | APIRET rc = ToolErrorGetMessage (ErrorCode, &pMsg); |
| 174 | switch (rc) |
| 175 | { |
| 176 | case NO_ERROR: |
| 177 | ToolErrorLog(pFileName, pFunctionName, LineNr, "Error %d: %s", ErrorCode, pMsg); |
| 178 | break; |
| 179 | case TOOL_ERROR_ENTRY_NOT_FOUND: |
| 180 | ToolErrorLog(pFileName, pFunctionName, LineNr, "Error %d: -- unregistered error code --", ErrorCode); |
| 181 | break; |
| 182 | default: |
| 183 | ToolErrorLog(__FFL__, "Unexpected internal error %d...", rc); |
| 184 | ToolErrorLog(__FFL__, "...while trying to log problem %d coming from %s/%s/%d", ErrorCode, pFileName, pFunctionName, LineNr); |
| 185 | break; |
| 186 | } |
| 187 | } |
| 188 | return NO_ERROR; |
| 189 | } |
| 190 | |
| 191 | APIRET ToolErrorSetLogFn (t_pToolErrorLogFn pLogFn) |
| 192 | { |
| 193 | ToolErrorLocal.pLogFn = pLogFn; |
| 194 | return NO_ERROR; |
| 195 | } |
| 196 | |
| 197 | |
| 198 | const char *ToolErrorTranslateErrno (int Errno) |
| 199 | { |
| 200 | const char *pTxt; |
| 201 | |
| 202 | switch (Errno) |
| 203 | { |
| 204 | case EPERM : pTxt = "Operation not permitted"; break; |
| 205 | case ENOENT : pTxt = "No such file or directory"; break; |
| 206 | case ESRCH : pTxt = "No such process"; break; |
| 207 | case EINTR : pTxt = "Interrupted system call"; break; |
| 208 | case EIO : pTxt = "I/O error"; break; |
| 209 | case ENXIO : pTxt = "No such device or address"; break; |
| 210 | case E2BIG : pTxt = "Argument list too long"; break; |
| 211 | case ENOEXEC: pTxt = "Exec format error"; break; |
| 212 | case EBADF : pTxt = "Bad file number"; break; |
| 213 | case ECHILD : pTxt = "No child processes"; break; |
| 214 | case EAGAIN : pTxt = "Try again"; break; |
| 215 | case ENOMEM : pTxt = "Out of memory"; break; |
| 216 | case EACCES : pTxt = "Permission denied"; break; |
| 217 | case EFAULT : pTxt = "Bad address"; break; |
| 218 | case ENOTBLK: pTxt = "Block device required"; break; |
| 219 | case EBUSY : pTxt = "Device or resource busy"; break; |
| 220 | case EEXIST : pTxt = "File exists"; break; |
| 221 | case EXDEV : pTxt = "Cross-device link"; break; |
| 222 | case ENODEV : pTxt = "No such device"; break; |
| 223 | case ENOTDIR: pTxt = "Not a directory"; break; |
| 224 | case EISDIR : pTxt = "Is a directory"; break; |
| 225 | case EINVAL : pTxt = "Invalid argument"; break; |
| 226 | case ENFILE : pTxt = "File table overflow"; break; |
| 227 | case EMFILE : pTxt = "Too many open files"; break; |
| 228 | case ENOTTY : pTxt = "Not a typewriter"; break; |
| 229 | case ETXTBSY: pTxt = "Text file busy"; break; |
| 230 | case EFBIG : pTxt = "File too large"; break; |
| 231 | case ENOSPC : pTxt = "No space left on device"; break; |
| 232 | case ESPIPE : pTxt = "Illegal seek"; break; |
| 233 | case EROFS : pTxt = "Read-only file system"; break; |
| 234 | case EMLINK : pTxt = "Too many links"; break; |
| 235 | case EPIPE : pTxt = "Broken pipe"; break; |
| 236 | case EDOM : pTxt = "Math argument out of domain of func"; break; |
| 237 | case ERANGE : pTxt = "Math result not representable"; break; |
| 238 | default : pTxt = "toolerror.coo: None of the standard error codes"; break; |
| 239 | } |
| 240 | return pTxt; |
| 241 | } |
| 242 | |
| 243 | |
| 244 | /* --------------------------------------------------------------------------------- */ |
| 245 | /* Module initialisation */ |
| 246 | /* --------------------------------------------------------------------------------- */ |
| 247 | |
| 248 | APIRET ToolErrorInit (unsigned int MaxErrors) |
| 249 | { |
| 250 | |
| 251 | if (Initialised == TRUE) |
| 252 | return TOOL_ERROR_ALREADY_INITIALISED; |
| 253 | |
| 254 | ToolErrorLocal.pErrorCodeArr = (t_pToolErrorEntry) malloc (MaxErrors * sizeof(t_ToolErrorEntry)); |
| 255 | ToolErrorLocal.ErrorCodeArrLen = MaxErrors; |
| 256 | ToolErrorLocal.ErrorCodeArrEntries = 0; |
| 257 | |
| 258 | Initialised = TRUE; |
| 259 | |
| 260 | return NO_ERROR; |
| 261 | } |
| 262 | |
| 263 | APIRET ToolErrorDeInit () |
| 264 | { |
| 265 | if (Initialised == FALSE) |
| 266 | return TOOL_ERROR_NOT_INITIALISED; |
| 267 | |
| 268 | free (ToolErrorLocal.pErrorCodeArr); |
| 269 | ToolErrorLocal.pLogFn = nullptr; |
| 270 | Initialised = FALSE; |
| 271 | |
| 272 | return NO_ERROR; |
| 273 | } |
| 274 |