source: branches/daemon/daemon/errors.c @ 1623

Last change on this file since 1623 was 1617, checked in by joshe, 15 years ago

Add a daemon frontend.

  • Property svn:keywords set to Date Rev Author Id
File size: 2.7 KB
Line 
1/******************************************************************************
2 * $Id: errors.c 1617 2007-03-31 20:00:40Z joshe $
3 *
4 * Copyright (c) 2007 Joshua Elsasser
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#include <errno.h>
26#include <stdarg.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <syslog.h>
31
32#include "errors.h"
33
34static void verrmsg( int, const char *, va_list );
35
36static int  gl_syslog = 0;
37
38void
39errsyslog( int useit )
40{
41    gl_syslog = useit;
42
43    if( useit )
44    {
45        openlog( getmyname(), 0, LOG_DAEMON );
46    }
47}
48
49void
50errmsg( const char * fmt, ... )
51{
52    va_list ap;
53
54    va_start( ap, fmt );
55    verrmsg( -1, fmt, ap );
56    va_end( ap );
57}
58
59void
60errnomsg( const char * fmt, ... )
61{
62    va_list ap;
63
64    va_start( ap, fmt );
65    verrmsg( errno, fmt, ap );
66    va_end( ap );
67}
68
69void
70verrmsg( int errnum, const char * fmt, va_list ap )
71{
72    char buf[1024];
73
74    vsnprintf( buf, sizeof buf, fmt, ap );
75
76    if( gl_syslog )
77    {
78        if( 0 > errnum )
79        {
80            syslog( LOG_ERR, "%s", buf );
81        }
82        else
83        {
84            syslog( LOG_ERR, "%s: %s", buf, strerror( errnum ) );
85        }
86    }
87    else
88    {
89        if( 0 > errnum )
90        {
91            fprintf( stderr, "%s: %s\n", getmyname(), buf );
92        }
93        else
94        {
95            fprintf( stderr, "%s: %s: %s\n", getmyname(), buf,
96                     strerror( errnum ) );
97        }
98    }
99}
100
101void
102mallocmsg( size_t size )
103{
104    if( 0 < size )
105    {
106        errmsg( "failed to allocate %i bytes of memory", ( int )size );
107    }
108    else
109    {
110        errmsg( "failed to allocate memory" );
111    }
112}
Note: See TracBrowser for help on using the repository browser.