Source File include/erroutines.h

    1    | /***************************************
    2    |   $Revision: 1.12 $
    3    |
    4    |   Error reporting (er) erroutines.h  - header file for error reporting.
    5    |
    6    |   Status: NOT REVUED, TESTED,
    7    |
    8    |   Design and implementation by: Marek Bukowy
    9    |
    10   |   ******************/ /******************
    11   |   Copyright (c) 1999                              RIPE NCC
    12   |
    13   |   All Rights Reserved
    14   |
    15   |   Permission to use, copy, modify, and distribute this software and its
    16   |   documentation for any purpose and without fee is hereby granted,
    17   |   provided that the above copyright notice appear in all copies and that
    18   |   both that copyright notice and this permission notice appear in
    19   |   supporting documentation, and that the name of the author not be
    20   |   used in advertising or publicity pertaining to distribution of the
    21   |   software without specific, written prior permission.
    22   |
    23   |   THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
    24   |   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
    25   |   AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
    26   |   DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
    27   |   AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    28   |   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    29   |   ***************************************/
    30   |
    31   | #ifndef ER_H
    32   | #define ER_H
    33   |
    34   | #include <stdio.h>
    35   | #include <unistd.h>
    36   | #include <stdlib.h>
    37   | #include <assert.h>
    38   | #include <time.h>
    39   | #include <stdarg.h>
    40   | #include <strings.h>
    41   |
    42   | #include <bitmask.h>
    43   | #include <er_aspects.h>
    44   | #include <stubs.h>
    45   |
    46   | #ifdef ER_IMPL
    47   | #define EXTDEF
    48   | #define EXTINI(a,b) a = b;
    49   | #else
    50   | #define EXTDEF extern
    51   | #define EXTINI(a,b) extern a;
    52   | #endif
    53   |
    54   | #ifdef __cplusplus
    55   | extern "C" {
    56   | #endif
    57   |
    58   |
    59   | typedef unsigned int er_mask_t;
    60   |
    61   | typedef int er_ret_t;
    62   |
    63   | #define MNELEN 16
    64   | typedef struct {
    65   |    er_ret_t	code;
    66   |    char		mnem[MNELEN];
    67   |    char		text[80];
    68   | } er_list_t;
    69   |
    70   |
    71   | typedef struct {
    72   |    er_ret_t	code;
    73   |    char		name[4];
    74   |    char 	desc[80];
    75   |    er_list_t   *errs;
    76   | } er_main_t;
    77   |
    78   | #define ER_SEV_C 0x40000000	/*+ circular buffer dump +*/
    79   | #define ER_SEV_F 0x20000000	/*+ fatal error +*/
    80   | #define ER_SEV_E 0x10000000	/*+ error +*/
    81   | #define ER_SEV_W 0x08000000	/*+ warning +*/
    82   | #define ER_SEV_I 0x04000000	/*+ information +*/
    83   | #define ER_SEV_D 0x02000000	/*+ debug message +*/
    84   | #define ER_SEV_L 0x01000000	/*+ library error +*/
    85   |
    86   |
    87   | /*  macro to see if the code is OK -- masks out the facility and compares,
    88   |     assuming all OK codes within the facilities are 0
    89   | */
    90   |
    91   |
    92   |
    93   | #define ER_SEV_TXT 20
    94   |
    95   | #define ER_MSGLEN 384
    96   | #define ER_ERRLEN 2048
    97   |
    98   | typedef struct {
    99   |     int 	sev;
    100  |     char 	chr[2];
    101  |     char 	txt[ER_SEV_TXT];
    102  | } er_level_t;
    103  |
    104  | #ifndef ER_IMPL /* for client modules */
    105  | extern er_level_t er_level_a[];
    106  | #else /* full definition */
    107  | er_level_t er_level_a[] = {
    108  |   { ER_SEV_C,	"?" , "BUG! bad sev: circ" },
    109  |   { ER_SEV_F,   "F" , "fatal error" },
    110  |   { ER_SEV_E, 	"E" , "error" },
    111  |   { ER_SEV_W, 	"W" , "warning" },
    112  |   { ER_SEV_I,	"I" , "information" },
    113  |   { ER_SEV_D, 	"D" , "debug msg" },
    114  |   { ER_SEV_L, 	"L" , "library err" },
    115  |   { 0,          "-" , "BUG! no such sev 0" }
    116  | };
    117  | #endif /* ER_IMPL */
    118  |
    119  | #define DEFFAC(a,b) { FAC_##a, #a, b, a##_mod_err }
    120  |
    121  | /* the macro expects two arguments:
    122  | 	 capital letters symbol of the facility
    123  | 	 short (<80 chars) description
    124  |    which then are expanded, eg. DEFFAC(TT, "test facility") expands to:
    125  |   { FAC_TT , "TT",   "test facility" ,  NULL} ,
    126  |   Therefore, the FAC_TT must be defined in the enum below.
    127  |   The  er_fac_code_t enum   must begin with FAC_NONE=0
    128  | 			and must end   with FAC_LAST.
    129  |   The  er_main_err array    must end   with FAC_NONE.
    130  |
    131  |   The user code must contain INITFAC(a) call early in the code that
    132  |   sets the pointer to the respective ??_mod_err array. There is nothing
    133  |   wrong in calling it twice, so don't hesitate if you must do it.
    134  |
    135  |   After a facility number changes (eg. because another one was added or
    136  |   deleted before yours) ALL your code must be recompiled before linking.
    137  | */
    138  |
    139  | #define ER_LASTTXT {-1}		/* macro for use in error text arrays */
    140  |
    141  | #define ERDUP(a) a, #a
    142  |
    143  | #include "er_facilities.h"
    144  |
    145  | /*************************************************************************/
    146  |
    147  | /* mode values (bitmap: describes what kind of severity and mnemonic we
    148  | want back. */
    149  |
    150  | /* bits 0-1 (values 0-3) - severity: */
    151  | #define ER_M_SEVCHAR 1      /* one-letter severity indication */
    152  | #define ER_M_SEVLONG 2      /* long severity indication */
    153  |
    154  | /* bit 2 (values 0 or 4) - mnemonic */
    155  | #define ER_M_MNEMONIC 4
    156  |
    157  | /* bit 3 (values 0 or 8) - error text */
    158  | #define ER_M_TEXTLONG 8
    159  |
    160  | /* bits 4-5 (values 0, 16, 32, 48) - user id's */
    161  | #define ER_M_UIDUID  16
    162  | #define ER_M_UIDEUID 32
    163  |
    164  | /* bit 6 (values 0 or 64) - process id's */
    165  | #define ER_M_PIDFULL 64
    166  |
    167  | /* bit 7 (values 0 or 128) - facility symbol */
    168  | #define ER_M_FACSYMB  128
    169  |
    170  |
    171  | /* bit 8 (values 0 or 256) - program name (argv[0]) */
    172  | #define ER_M_PROGNAME	256
    173  |
    174  | /* bit 9 (values 0 or 512) - current date and time */
    175  | #define ER_M_DATETIME   512
    176  |
    177  | #define ER_M_THR_ID    2048
    178  |
    179  | #define ER_M_DEFAULT  (ER_M_DATETIME | ER_M_FACSYMB  | ER_M_SEVCHAR \
    180  |                      | ER_M_MNEMONIC | ER_M_TEXTLONG | ER_M_PIDFULL \
    181  |                      | ER_M_THR_ID   | ER_M_PROGNAME )
    182  |
    183  |
    184  | int NOERR(er_ret_t a);
    185  | #define ERR(a) (!NOERR(a))
    186  |
    187  |
    188  | void ER_init(int argc, char **argv);
    189  |
    190  | #define ER_dbg_eq(mod, asp, typ, expr)  \
    191  |                 ER_dbg_va (mod, asp, #expr " = " typ, expr)
    192  |
    193  | void ER_perror(int facwhere, int errcode, char *format,...);
    194  | void ER_dbg_va( int facwhere, er_mask_t asp, char *txt, ...);
    195  | void ER_inf_va( int facwhere, er_mask_t asp, char *txt, ...);
    196  | int ER_anybody_wants( int facwhere, int errcode, er_mask_t asp );
    197  | int ER_is_traced(int facwhere, er_mask_t asp);
    198  |
    199  | /* This is just provisional ! */
    200  | typedef struct {
    201  |   FILE       *fdes;
    202  |                    /* file descriptor. This is REALLY provisional! */
    203  |   int        fac;  /* facility */
    204  |   er_mask_t  asp;  /* mask of aspects */
    205  |   int        sev;  /* minimal severity */
    206  |   int        mode; /* bitmask of output mode - bits of information that
    207  | 		      should be printed */
    208  | } er_path_t;
    209  |
    210  | void ER_setpath(er_path_t *newset);
    211  |
    212  | #ifdef ER_IMPL
    213  |
    214  | /* global vars !!!!! must be set for reporting purposes.
    215  |    Must be initialised in main() by ER_init().
    216  | */
    217  |  char er_progname[32];
    218  |  char er_pid[16];
    219  |
    220  | /* those are private variables */
    221  |
    222  | er_path_t        er_provisional_struct;
    223  | pthread_mutex_t  er_pathlist_mutex;
    224  | int              er_pathlist_mutex_initialised = 0;
    225  | #endif
    226  |
    227  | #if 0
    228  |
    229  | typedef struct {
    230  |   mask_t fac_mask;
    231  |   mask_t asp_mask;
    232  |   int    sev_min;
    233  |   int    sev_max;
    234  |   unsigned err;     /* a specific error code - or 0 to mean all errors */
    235  | } er_filter_t;
    236  |
    237  |
    238  | enum {
    239  |   ER_PATH_UNBUF=1,  /* unbuffered file/socket access via a file descriptor */
    240  |   ER_PATH_BUFPTR,   /* buffered   file access via a FILE structure  */
    241  |   ER_PATH_BUFNAM,   /* buffered   file access via a file name
    242  | 		       (file reopened for every message) */
    243  |   ER_PATH_EMAIL,    /* e-mail constructed, send at the end or after one message
    244  | 		       depending on options */
    245  |   ER_PATH_SYSLOG    /* syslog msg sent at every message */
    246  | } er_path_mt;
    247  |
    248  |
    249  | typedef struct {
    250  |   char              active;
    251  |   pthread_mutex_t   mutex;
    252  |   er_path_mt        type;
    253  |   er_option_mt    options;
    254  |   union {
    255  |     struct {
    256  |       int    fd;                /* int filedescr for FILEUNBUF */
    257  |     } unbuf;
    258  |     struct {
    259  |       FILE  *fp;                /* FILE* fp for FILEBUFPTR */
    260  |     } bufptr;
    261  |     struct {
    262  |       char  filename[80];       /* filename for FILEBUFNAM */
    263  |     } bufnam;
    264  |     struct {
    265  |       char  address[80];        /* address(es) for EMAIL */
    266  |     } email;
    267  |     struct {
    268  |       int facility;             /* openlog(3) parameters for SYSLOG */
    269  |       int logopt;
    270  |       char ident[32];
    271  |     } syslog;
    272  |   } desc;
    273  | } er_path_t;
    274  |
    275  |
    276  | #endif /* if 0 */
    277  |
    278  | #ifdef __cplusplus
    279  | }
    280  | #endif
    281  |
    282  |
    283  |
    284  | #undef EXTDEF
    285  | #undef EXTINI
    286  | #endif /* ER_H */