Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Error processing for admins (i.e. keep last error message) to provide back to admin.
0003  *
0004  * @file admerror.c
0005  */
0006 /* -----------------------------------------------------------------------------
0007  * Enduro/X Middleware Platform for Distributed Transaction Processing
0008  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0009  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0010  * This software is released under one of the following licenses:
0011  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0012  * See LICENSE file for full text.
0013  * -----------------------------------------------------------------------------
0014  * AGPL license:
0015  *
0016  * This program is free software; you can redistribute it and/or modify it under
0017  * the terms of the GNU Affero General Public License, version 3 as published
0018  * by the Free Software Foundation;
0019  *
0020  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0021  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0022  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0023  * for more details.
0024  *
0025  * You should have received a copy of the GNU Affero General Public License along 
0026  * with this program; if not, write to the Free Software Foundation, Inc.,
0027  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0028  *
0029  * -----------------------------------------------------------------------------
0030  * A commercial use license is available from Mavimax, Ltd
0031  * contact@mavimax.com
0032  * -----------------------------------------------------------------------------
0033  */
0034 #include <string.h>
0035 #include <stdio.h>
0036 #include <stdlib.h>
0037 #include <memory.h>
0038 
0039 #include <ndrstandard.h>
0040 #include <ndrxdcmn.h>
0041 #include <ndebug.h>
0042 #include <stdarg.h>
0043 #include <ndrxd.h>
0044 /*---------------------------Externs------------------------------------*/
0045 /*---------------------------Macros-------------------------------------*/
0046 #define NDRXD_ERROR_DESCRIPTION(X) (M_ndrxd_error_defs[X<NDRXD_EMINVAL?TPMINVAL:(X>NDRXD_EMAXVAL?NDRXD_EMAXVAL:X)].msg)
0047 #define NDRXD_ERROR(X) X, #X
0048 /*---------------------------Enums--------------------------------------*/
0049 /*---------------------------Typedefs-----------------------------------*/
0050 /*---------------------------Globals------------------------------------*/
0051 /*---------------------------Statics------------------------------------*/
0052 /*---------------------------Prototypes---------------------------------*/
0053 
0054 char M_ndrxd_error_msg_buf[MAX_NDRXD_ERROR_LEN+1] = {EXEOS};
0055 int M_ndrxd_error = NDRXD_EMINVAL;
0056 
0057 /* Do we need this to be in message catalogue?
0058  * table bellow is indexed by id (i.e. direct array erorr lookup should work)
0059  */
0060 struct err_msg
0061 {
0062     int id;
0063     char *msg;
0064 } M_ndrxd_error_defs [] =
0065 {
0066     {NDRXD_ERROR(NDRXD_EMINVAL)},   /* minimum error message */
0067 
0068     {NDRXD_ERROR(NDRXD_ESRVCIDDUP)},
0069     {NDRXD_ERROR(NDRXD_ESRVCIDINV)},
0070     {NDRXD_ERROR(NDRXD_EOS)},
0071     {NDRXD_ERROR(NDRXD_ECFGLDED)},
0072     {NDRXD_ERROR(NDRXD_ECFGINVLD)},
0073     {NDRXD_ERROR(NDRXD_EPMOD)},
0074     {NDRXD_ERROR(NDRXD_ESHMINIT)},
0075     {NDRXD_ERROR(NDRXD_NOTSTARTED)},
0076     {NDRXD_ERROR(NDRXD_ECMDNOTFOUND)},
0077     {NDRXD_ERROR(NDRXD_ENONICONTEXT)},
0078     {NDRXD_ERROR(NDRXD_EREBBINARYRUN)},
0079     {NDRXD_ERROR(NDRXD_EBINARYRUN)},
0080     {NDRXD_ERROR(NDRXD_ECONTEXT)},
0081     {NDRXD_ERROR(NDRXD_EINVPARAM)},
0082     {NDRXD_ERROR(NDRXD_EABORT)},
0083     {NDRXD_ERROR(NDRXD_EENVFAIL)},
0084     {NDRXD_ERROR(NDRXD_EINVAL)},
0085     {NDRXD_ERROR(NDRXD_ENORMAL)},
0086     {NDRXD_ERROR(NDRXD_ECFGDEFAULTS)},
0087     {NDRXD_ERROR(NDRXD_ECFGSERVER)},
0088     {NDRXD_ERROR(NDRXD_ECFGAPPCONFIG)},
0089     {NDRXD_ERROR(NDRXD_EACCES)},
0090     {NDRXD_ERROR(NDRXD_ESYNTAX)},
0091     {NDRXD_ERROR(NDRXD_ESYSTEM)},
0092     {NDRXD_ERROR(NDRXD_ENOCFGLD)},
0093     {NDRXD_ERROR(NDRXD_ENOENT)},
0094     {NDRXD_ERROR(NDRXD_EMAXVAL)}    /* maximum error message */
0095 };
0096 
0097 /*---------------------------Prototypes---------------------------------*/
0098 
0099 /**
0100  * Get error handling library resources, for direct access from other libraries
0101  * @param error_code_ptr double ptr to error code
0102  * @param errbufsz buffer size
0103  * @return error string buffer
0104  */
0105 expublic char * NDRXD_error_res_get(int **error_code_ptr, int *errbufsz)
0106 {
0107     *error_code_ptr = &M_ndrxd_error;
0108     *errbufsz = sizeof(M_ndrxd_error_msg_buf);
0109     return M_ndrxd_error_msg_buf;
0110 }
0111 /**
0112  * Standard.
0113  * Printer error to stderr
0114  * @param str
0115  */
0116 expublic void NDRXD_error (char *str)
0117 {
0118     if (EXEOS!=M_ndrxd_error_msg_buf[0])
0119         fprintf(stderr, "%s:%d:%s (%s)\n", str, M_ndrxd_error, 
0120                 NDRXD_ERROR_DESCRIPTION(M_ndrxd_error), M_ndrxd_error_msg_buf);
0121     else
0122         fprintf(stderr, "%s:%d:%s\n", str, M_ndrxd_error, 
0123                 NDRXD_ERROR_DESCRIPTION(M_ndrxd_error));
0124 }
0125 
0126 /**
0127  * Extended version. This will print internal error message what happened.
0128  * This is not thread safe (as all other functions).
0129  * @param error_code
0130  */
0131 expublic char * ndrxd_strerror (int err)
0132 {
0133     static char errbuf[MAX_NDRXD_ERROR_LEN+1];
0134 
0135     if (EXEOS!=M_ndrxd_error_msg_buf[0])
0136     {
0137         snprintf(errbuf, sizeof(errbuf), "%s (last error %d: %s)",
0138                         NDRXD_ERROR_DESCRIPTION(err),
0139                         M_ndrxd_error,
0140                         M_ndrxd_error_msg_buf);
0141     }
0142     else
0143     {
0144         snprintf(errbuf, sizeof(errbuf), "%d:%s",
0145                     err, NDRXD_ERROR_DESCRIPTION(err));
0146     }
0147 
0148     return errbuf;
0149 }
0150 
0151 /**
0152  * ATMI standard
0153  * @return - pointer to int holding error code?
0154  */
0155 expublic int * _ndrxd_getNDRXD_errno_addr (void)
0156 {
0157     return &M_ndrxd_error;
0158 }
0159 
0160 /**
0161  * Internetal function for setting
0162  * @param error_code
0163  * @param msg
0164  * @return
0165  */
0166 expublic void NDRXD_set_error(int error_code)
0167 {
0168     if (!M_ndrxd_error)
0169     {
0170         NDRX_LOG(log_warn, "NDRXD_set_error: %d (%s)",
0171                                 error_code, NDRXD_ERROR_DESCRIPTION(error_code));
0172         M_ndrxd_error_msg_buf[0] = EXEOS;
0173         M_ndrxd_error = error_code;
0174     }
0175 }
0176 
0177 /**
0178  * I nternetal function for setting
0179  * @param error_code
0180  * @param msg
0181  * @return
0182  */
0183 expublic void NDRXD_set_error_msg(int error_code, char *msg)
0184 {
0185     int msg_len;
0186     int err_len;
0187 
0188     if (!M_ndrxd_error)
0189     {
0190         msg_len = strlen(msg);
0191         err_len = (msg_len>MAX_NDRXD_ERROR_LEN)?MAX_NDRXD_ERROR_LEN:msg_len;
0192 
0193 
0194         NDRX_LOG(log_warn, "NDRXD_set_error_msg: %d (%s) [%s]", error_code,
0195                                 NDRXD_ERROR_DESCRIPTION(error_code), msg);
0196         M_ndrxd_error_msg_buf[0] = EXEOS;
0197         strncat(M_ndrxd_error_msg_buf, msg, err_len);
0198         M_ndrxd_error = error_code;
0199     }
0200 }
0201 
0202 /**
0203  * Advanced error setting function, uses format list
0204  * Use this only in case where it is really needed.
0205  * @param error_code - error code
0206  * @param fmt - format stirng
0207  * @param ... - format details
0208  */
0209 expublic void NDRXD_set_error_fmt(int error_code, const char *fmt, ...)
0210 {
0211     char msg[MAX_NDRXD_ERROR_LEN+1] = {EXEOS};
0212     va_list ap;
0213 
0214     if (!M_ndrxd_error)
0215     {
0216         va_start(ap, fmt);
0217         (void) vsnprintf(msg, sizeof(msg), fmt, ap);
0218         va_end(ap);
0219 
0220         NDRX_STRCPY_SAFE(M_ndrxd_error_msg_buf, msg);
0221         M_ndrxd_error = error_code;
0222 
0223         NDRX_LOG(log_warn, "NDRXD_set_error_fmt: %d (%s) [%s]",
0224                         error_code, NDRXD_ERROR_DESCRIPTION(error_code),
0225                         M_ndrxd_error_msg_buf);
0226     }
0227 }
0228 
0229 /**
0230  * Unset any error data currently in use
0231  */
0232 expublic void NDRXD_unset_error(void)
0233 {
0234     M_ndrxd_error_msg_buf[0]=EXEOS;
0235     M_ndrxd_error = BMINVAL;
0236 }
0237 /**
0238  * Return >0 if error is set
0239  * @return
0240  */
0241 expublic int NDRXD_is_error(void)
0242 {
0243     return M_ndrxd_error;
0244 }
0245 
0246 /**
0247  * Append error message
0248  * @param msg
0249  */
0250 expublic void NDRXD_append_error_msg(char *msg)
0251 {
0252     int free_space = MAX_NDRXD_ERROR_LEN-strlen(M_ndrxd_error_msg_buf);
0253     int app_error_len = strlen(msg);
0254     int n;
0255     n = free_space<app_error_len?free_space:app_error_len;
0256     strncat(M_ndrxd_error_msg_buf, msg, n);
0257 }
0258 
0259 /* vim: set ts=4 sw=4 et smartindent: */