openalbridge/errlib.c
author nemo
Mon, 12 Oct 2009 13:47:56 +0000
changeset 2425 a4af95e6382c
parent 2418 538a777f90c4
child 2437 10e4b5fc0d93
permissions -rw-r--r--
sheepluva's basic 45° test. NEEDS TESTING
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
     1
/*
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
     2
 
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
     3
 module: errlib.c
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
     4
 
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
     5
 purpose: library of error functions
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
     6
 
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
     7
 reference: Stevens, Unix network programming (2ed), p.922
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
     8
 
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
     9
 */
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    10
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    11
#include "errlib.h"
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    12
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    13
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    14
#define MAXLINE 4095
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    15
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    16
#ifdef __CPLUSPLUS
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    17
extern "C" {
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    18
#endif 
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    19
        
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    20
        int daemon_proc = 0; /* set to 0 if stdout/stderr available, else set to 1 */
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    21
        
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    22
        static void err_doit (int errnoflag, int level, const char *fmt, va_list ap)
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    23
        {
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    24
                int errno_save = errno, n;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    25
                char buf[MAXLINE+1];
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    26
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    27
                vsnprintf (buf, MAXLINE, fmt, ap);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    28
                n = strlen(buf);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    29
                if (errnoflag)
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    30
                        snprintf (buf+n, MAXLINE-n, ": %s", strerror(errno_save));
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    31
                strcat (buf, "\n");
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    32
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    33
                if (daemon_proc)
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    34
                        syslog (level, buf);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    35
                else {
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    36
                        fflush (stdout);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    37
                        fputs (buf, stderr);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    38
                        fflush (stderr);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    39
                }
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    40
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    41
                return;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    42
        }
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    43
        
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    44
        void err_ret (const char *fmt, ...)
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    45
        {
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    46
                va_list ap;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    47
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    48
                va_start (ap, fmt);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    49
                err_doit (1, LOG_INFO, fmt, ap);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    50
                va_end (ap);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    51
                return;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    52
        }
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    53
        
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    54
        void err_sys (const char *fmt, ...)
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    55
        {
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    56
                va_list ap;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    57
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    58
                va_start (ap, fmt);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    59
                err_doit (1, LOG_ERR, fmt, ap);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    60
                va_end (ap);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    61
                exit (1);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    62
        }
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    63
        
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    64
        void err_msg (const char *fmt, ...)
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    65
        {
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    66
                va_list ap;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    67
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    68
                va_start (ap, fmt);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    69
                err_doit (0, LOG_INFO, fmt, ap);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    70
                va_end (ap);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    71
                return;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    72
        }
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    73
        
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    74
        void err_quit (const char *fmt, ...)
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    75
        {
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    76
                va_list ap;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    77
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    78
                va_start (ap, fmt);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    79
                err_doit (0, LOG_ERR, fmt, ap);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    80
                va_end (ap);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    81
                exit (1);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    82
        }
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    83
        
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    84
        void err_dump (const char *fmt, ...)
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    85
        {
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    86
                va_list ap;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    87
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    88
                va_start (ap, fmt);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    89
                err_doit (1, LOG_ERR, fmt, ap);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    90
                va_end (ap);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    91
                abort();
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    92
        }
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    93
        
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    94
#ifdef __CPLUSPLUS
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    95
}
538a777f90c4 fix build and partially moves messages to errlib
koda
parents:
diff changeset
    96
#endif