File: error.c

package info (click to toggle)
shush 1.2.3-2
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 884 kB
  • ctags: 559
  • sloc: ansic: 5,502; sh: 153; lex: 69; makefile: 68; yacc: 54; awk: 31
file content (100 lines) | stat: -rw-r--r-- 2,082 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
** Copyright (C) 2003-2006 Christophe Kalt
**
** This file is part of shush,
** see the LICENSE file for details on your rights.
*/

#include "os.h"

#include <stdarg.h>
#include <syslog.h>

#include "error.h"
#include "gethostname.h"

#ifndef lint
static char const rcsid[] = "@(#)$Id: error.c 1404 2008-03-08 23:25:46Z kalt $";
#endif

static int prefix, tosyslog;
static char *task, *id;

/*
** error_init
**	Whether or not to write a header prior to any error message
*/
void
error_init(char *name, char *jid, int copytosyslog)
{
    task = name;
    id = jid;
    if (name == NULL)
	prefix = 1;
    else
	prefix = 0;
    tosyslog = copytosyslog;
}

/*
** error
**	output a message optionally preceeded by a header
*/
void
error(char *format, ...)
{
    va_list va;

    if (task != NULL)
      {
        if (*id != '\0')
            fprintf(stderr, "%s[%s] encountered the following error(s) while running \"%s\" on host %s:\n\n", myname, task, id, get_hostname(0));
        else
            fprintf(stderr, "%s encountered the following error(s) while running \"%s\" on host %s:\n\n", myname, task, get_hostname(0));
	task = NULL;
      }

    if (prefix)
	fprintf(stderr, "%s: ", myname);
    va_start(va, format);
    vfprintf(stderr, format, va);
    fprintf(stderr, "\n");
    fflush(stderr);

    if (tosyslog)
	vsyslog(LOG_ERR, format, va);

    va_end(va);
}

#undef abort

/*
** myassert
**	_assert() replacement
*/
void
myassert(char *assertion, char *file, char *function, int line)
{
    if (function != NULL)
	error("Assertion failed: %s, file %s, function %s, line %d",
	      assertion, file, function, line);
    else
	error("Assertion failed: %s, file %s, line %d", assertion, file, line);
    abort();
}

/*
** myabort()
**	abort() replacement
*/
void
myabort(char *file, char *function, int line)
{
    if (function != NULL)
	error("The impossible just happened: line %d in function \"%s\" in file %s was reached!", line, function, file);
    else
	error("The impossible just happened: line %d in file %s was reached!",
	      line, file);
    abort();
}