forked from jmrosinski/GPTL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
140 lines (119 loc) · 2.92 KB
/
util.c
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
** $Id: util.c,v 1.13 2010-01-01 01:34:07 rosinski Exp $
*/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "private.h"
static bool abort_on_error = false; /* flag says to abort on any error */
static int max_errors = 10; /* max number of error print msgs */
static int num_errors = 0; /* number of times GPTLerror was called */
static int max_warn = 10; /* max number of warning messages */
static int num_warn = 0; /* number of times GPTLwarn was called */
/*
** GPTLerror: error return routine to print a message and return a failure
** value.
**
** Input arguments:
** fmt: format string
** variable list of additional arguments for vfprintf
**
** Return value: -1 (failure)
*/
int GPTLerror (const char *fmt, ...)
{
va_list args;
va_start (args, fmt);
if (fmt != NULL && num_errors < max_errors) {
#ifdef HAVE_VPRINTF
(void) fprintf (stderr, "GPTL error:");
(void) vfprintf (stderr, fmt, args);
#else
(void) fprintf (stderr, "GPTLerror: no vfprintf: fmt is %s\n", fmt);
#endif
if (num_errors == max_errors)
(void) fprintf (stderr, "Truncating further error print now after %d msgs",
num_errors);
}
va_end (args);
if (abort_on_error)
exit (-1);
++num_errors;
return (-1);
}
/*
** GPTLwarn: print a warning message
** value.
**
** Input arguments:
** fmt: format string
** variable list of additional arguments for vfprintf
*/
void GPTLwarn (const char *fmt, ...)
{
va_list args;
va_start (args, fmt);
if (fmt != NULL && num_warn < max_warn) {
#ifdef HAVE_VPRINTF
(void) fprintf (stderr, "GPTL warning:");
(void) vfprintf (stderr, fmt, args);
#else
(void) fprintf (stderr, "GPTLwarning: no vfprintf: fmt is %s\n", fmt);
#endif
if (num_warn == max_warn)
(void) fprintf (stderr, "Truncating further warning print now after %d msgs",
num_warn);
}
va_end (args);
++num_warn;
return;
}
/*
** GPTLset_abort_on_error: User-visible routine to set abort_on_error flag
**
** Input arguments:
** val: true (abort on error) or false (don't)
*/
void GPTLset_abort_on_error (bool val)
{
abort_on_error = val;
}
/*
** GPTLreset_errors: reset error state to no errors
**
*/
void GPTLreset_errors (void)
{
num_errors = 0;
}
/*
** GPTLnum_errors: User-visible routine returns number of times GPTLerror() called
**
*/
int GPTLnum_errors (void)
{
return num_errors;
}
/*
** GPTLnum_errors: User-visible routine returns number of times GPTLerror() called
**
*/
int GPTLnum_warn (void)
{
return num_warn;
}
/*
** GPTLallocate: wrapper utility for malloc
**
** Input arguments:
** nbytes: size to allocate
**
** Return value: pointer to the new space (or NULL)
*/
void *GPTLallocate (const int nbytes, const char *caller)
{
void *ptr;
if ( nbytes <= 0 || ! (ptr = malloc (nbytes)))
(void) GPTLerror ("GPTLallocate from %s: malloc failed for %d bytes\n", nbytes, caller);
return ptr;
}