misc/openalbridge/errlib.c
changeset 3364 e5403e2bf02c
parent 3363 bcd6d76db4f7
child 3365 37ac593e9027
equal deleted inserted replaced
3363:bcd6d76db4f7 3364:e5403e2bf02c
     1 /*
       
     2 
       
     3  module: errlib.c
       
     4 
       
     5  purpose: library of error functions
       
     6 
       
     7  reference: Stevens, Unix network programming (2ed), p.922
       
     8 
       
     9  */
       
    10 
       
    11 #include "errlib.h"
       
    12 
       
    13 
       
    14 #define MAXLINE 4095
       
    15 
       
    16 int daemon_proc = 0; /* set to 0 if stdout/stderr available, else set to 1 */
       
    17 
       
    18 static void err_doit (int errnoflag, int level, const char *fmt, va_list ap) {
       
    19     int errno_save = errno, n;
       
    20     char buf[MAXLINE+1];
       
    21 
       
    22     vsnprintf (buf, MAXLINE, fmt, ap);
       
    23     n = strlen(buf);
       
    24     if (errnoflag)
       
    25         snprintf (buf+n, MAXLINE-n, ": %s", strerror(errno_save));
       
    26     strcat (buf, "\n");
       
    27 
       
    28     if (daemon_proc)
       
    29         syslog (level, buf);
       
    30     else {
       
    31         fflush (stdout);
       
    32         fprintf (stderr, "%s", buf);
       
    33         fflush (stderr);
       
    34     }
       
    35 
       
    36     return;
       
    37 }
       
    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 }