misc/openalbridge/errlib.c
changeset 3362 8d3b4d19ce27
parent 3353 a767dd3786b5
equal deleted inserted replaced
3361:cfc6cd502f85 3362:8d3b4d19ce27
     1 /*
     1 /*
     2  
     2 
     3  module: errlib.c
     3  module: errlib.c
     4  
     4 
     5  purpose: library of error functions
     5  purpose: library of error functions
     6  
     6 
     7  reference: Stevens, Unix network programming (2ed), p.922
     7  reference: Stevens, Unix network programming (2ed), p.922
     8  
     8 
     9  */
     9  */
    10 
    10 
    11 #include "errlib.h"
    11 #include "errlib.h"
    12 
    12 
    13 
    13 
    14 #define MAXLINE 4095
    14 #define MAXLINE 4095
    15 
    15 
    16 #ifdef __CPLUSPLUS
    16 int daemon_proc = 0; /* set to 0 if stdout/stderr available, else set to 1 */
    17 extern "C" {
    17 
    18 #endif 
    18 static void err_doit (int errnoflag, int level, const char *fmt, va_list ap) {
    19     
    19     int errno_save = errno, n;
    20     int daemon_proc = 0; /* set to 0 if stdout/stderr available, else set to 1 */
    20     char buf[MAXLINE+1];
    21     
    21 
    22     static void err_doit (int errnoflag, int level, const char *fmt, va_list ap)
    22     vsnprintf (buf, MAXLINE, fmt, ap);
    23     {
    23     n = strlen(buf);
    24         int errno_save = errno, n;
    24     if (errnoflag)
    25         char buf[MAXLINE+1];
    25         snprintf (buf+n, MAXLINE-n, ": %s", strerror(errno_save));
    26         
    26     strcat (buf, "\n");
    27         vsnprintf (buf, MAXLINE, fmt, ap);
    27 
    28         n = strlen(buf);
    28     if (daemon_proc)
    29         if (errnoflag)
    29         syslog (level, buf);
    30             snprintf (buf+n, MAXLINE-n, ": %s", strerror(errno_save));
    30     else {
    31         strcat (buf, "\n");
    31         fflush (stdout);
    32         
    32         fprintf (stderr, "%s", buf);
    33         if (daemon_proc)
    33         fflush (stderr);
    34             syslog (level, buf);
       
    35         else {
       
    36             fflush (stdout);
       
    37             fprintf (stderr, "%s", buf);
       
    38             fflush (stderr);
       
    39         }
       
    40         
       
    41         return;
       
    42     }
    34     }
    43     
    35 
    44     void err_ret (const char *fmt, ...)
    36     return;
    45     {
       
    46         va_list ap;
       
    47         
       
    48         va_start (ap, fmt);
       
    49         err_doit (1, LOG_INFO, fmt, ap);
       
    50         va_end (ap);
       
    51         return;
       
    52     }
       
    53     
       
    54     void err_sys (const char *fmt, ...)
       
    55     {
       
    56         va_list ap;
       
    57         
       
    58         va_start (ap, fmt);
       
    59         err_doit (1, LOG_ERR, fmt, ap);
       
    60         va_end (ap);
       
    61         exit (1);
       
    62     }
       
    63     
       
    64     void err_msg (const char *fmt, ...)
       
    65     {
       
    66         va_list ap;
       
    67         
       
    68         va_start (ap, fmt);
       
    69         err_doit (0, LOG_INFO, fmt, ap);
       
    70         va_end (ap);
       
    71         return;
       
    72     }
       
    73     
       
    74     void err_quit (const char *fmt, ...)
       
    75     {
       
    76         va_list ap;
       
    77         
       
    78         va_start (ap, fmt);
       
    79         err_doit (0, LOG_ERR, fmt, ap);
       
    80         va_end (ap);
       
    81         exit (1);
       
    82     }
       
    83     
       
    84     void err_dump (const char *fmt, ...)
       
    85     {
       
    86         va_list ap;
       
    87         
       
    88         va_start (ap, fmt);
       
    89         err_doit (1, LOG_ERR, fmt, ap);
       
    90         va_end (ap);
       
    91         abort();
       
    92     }
       
    93     
       
    94 #ifdef __CPLUSPLUS
       
    95 }
    37 }
    96 #endif
    38 
       
    39 void err_ret (const char *fmt, ...) {
       
    40     va_list ap;
       
    41 
       
    42     va_start (ap, fmt);
       
    43     err_doit (1, LOG_INFO, fmt, ap);
       
    44     va_end (ap);
       
    45     return;
       
    46 }
       
    47 
       
    48 void err_sys (const char *fmt, ...) {
       
    49     va_list ap;
       
    50 
       
    51     va_start (ap, fmt);
       
    52     err_doit (1, LOG_ERR, fmt, ap);
       
    53     va_end (ap);
       
    54     exit (1);
       
    55 }
       
    56 
       
    57 void err_msg (const char *fmt, ...) {
       
    58     va_list ap;
       
    59 
       
    60     va_start (ap, fmt);
       
    61     err_doit (0, LOG_INFO, fmt, ap);
       
    62     va_end (ap);
       
    63     return;
       
    64 }
       
    65 
       
    66 void err_quit (const char *fmt, ...) {
       
    67     va_list ap;
       
    68 
       
    69     va_start (ap, fmt);
       
    70     err_doit (0, LOG_ERR, fmt, ap);
       
    71     va_end (ap);
       
    72     exit (1);
       
    73 }
       
    74 
       
    75 void err_dump (const char *fmt, ...) {
       
    76     va_list ap;
       
    77 
       
    78     va_start (ap, fmt);
       
    79     err_doit (1, LOG_ERR, fmt, ap);
       
    80     va_end (ap);
       
    81     abort();
       
    82 }