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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
/*****************************************************************************
* Johns Hopkins University
* Center For Astrophysical Sciences
* FUSE
*****************************************************************************
*
* Synopsis: cf_error_init(char *prgmid, char *vernum, FILE *errfile)
*
* Description: Writes a timestamp, the calling program name, and an
* error message to stdout for a cfitsio routine.
*
* Arguments: char *progid Name of the calling program
* char *vernum Version of the calling program
* (should be version control number)
* FILE *errfile Output file name (default is stderr)
*
* Returns: none
*
* History: 02/22/99 peb Some simple functions to make error
* reporting easier. Deficiencies in
* cfitsio make printing comprehensive
* messages difficult.
* 03/15/99 emm Added cf_if_memory_error.
* 04/30/99 peb Added cf_malloc and cf_calloc.
* 06/07/99 peb Added reporting of version number
* 09/01/99 emm Added fflush to see if it helps print
* out error messages on failure.
* 11/08/99 emm Added !!!!!!!! to error reporting to
* better delimit the error message and
* to make them more obvious.
* 04/23/01 1.10 wvd Make cf_malloc & cf_calloc print error
* message returned by malloc & calloc
* 03/07/03 1.2 peb Added cf_verbose and external variable
* verbose_level
* 03/13/03 1.3 peb Enabled cf_verbose, cf_if_warning, and
* cf_if_error to accept a variable
* number of parameters. Changed the
* output format to print month, day, time
* hostname, function name and message.
* 03/25/03 1.4 peb Changed output to print time, program,
* and version number for warning and
* error messages.
* Changed output to use verbose_level
* to modify output format:
* level==1 prints program, version, and
* message.
* level>=2 prints only message.
* 10/30/03 1.5 peb Replaced cftime function with strftime
* for UNIX compatibility.
* Removed unnecessary header files,
* malloc.h and unistd.h
* 04/07/04 1.6 bjg fflush stdout and error_file
*
****************************************************************************/
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "fitsio.h"
#define N_STYM 40
int verbose_level=0;
static char *program_name=NULL;
static char *version_no=NULL;
static FILE *error_file=NULL;
void cf_error_init(const char *progid, const char *vernum, FILE *errfile)
{
static char unknown[] = "unknown file";
static char version[] = "1.6";
if (progid) {
program_name = malloc(strlen(progid)+1);
strcpy(program_name, progid);
}
else {
program_name = malloc(strlen(unknown)+1);
strcpy(program_name, unknown);
}
if (vernum) {
version_no = malloc(strlen(vernum)+1);
strcpy(version_no, vernum);
}
else {
version_no = malloc(strlen(version)+1);
strcpy(version_no, version);
}
if (errfile)
error_file = errfile;
else
error_file = stderr;
}
void cf_verbose(int level, const char *format, ...)
{
va_list args;
va_start(args, format);
if (verbose_level >= level) {
if (level == 1)
printf(" %s-%s: ", program_name, version_no);
else if (level >= 2)
printf("\t");
vprintf(format, args);
printf("\n");
fflush(stdout);
}
va_end(args);
}
void cf_if_warning(char *format, ...)
{
char stym[N_STYM]={'\0'};
time_t tym;
va_list args;
va_start(args, format);
time(&tym);
strftime(stym, N_STYM, "%Y %b %e %T", localtime(&tym));
fprintf(error_file, "%s %s-%s: WARNING - ",
stym, program_name, version_no);
vfprintf(error_file, format, args);
fprintf(error_file, "\n");
fflush(error_file);
va_end(args);
}
void cf_if_error(char *format, ...)
{
char stym[N_STYM]={'\0'};
time_t tym;
va_list args;
va_start(args, format);
time(&tym);
strftime(stym, N_STYM, "%Y %b %e %T", localtime(&tym));
fprintf(error_file, "%s %s-%s: ERROR - ",
stym, program_name, version_no);
vfprintf(error_file, format, args);
fprintf(error_file, "\n");
fflush(error_file);
va_end(args);
exit(1);
}
void cf_if_fits_warning(int status)
{
if (status) {
char stym[N_STYM]={'\0'};
time_t tym;
time(&tym);
strftime(stym, N_STYM, "%Y %b %e %T", localtime(&tym));
fprintf(error_file, "%s %s-%s: FITS WARNING - ",
stym, program_name, version_no);
fits_report_error(error_file, status);
fflush(error_file);
}
}
void cf_if_fits_error(int status)
{
if (status) {
char stym[N_STYM]={'\0'};
time_t tym;
time(&tym);
strftime(stym, N_STYM, "%Y %b %e %T", localtime(&tym));
fprintf(error_file, "%s %s-%s: FITS ERROR - ",
stym, program_name, version_no);
fits_report_error(error_file, status);
fflush(error_file);
exit(1);
}
}
void *cf_malloc(size_t size)
{
void *ptr;
if (!(ptr = malloc(size))) {
char stym[N_STYM]={'\0'};
time_t tym;
time(&tym);
strftime(stym, N_STYM, "%Y %b %e %T", localtime(&tym));
fprintf(error_file, "%s %s-%s: ERROR - %s\n",
stym, "cf_malloc", "1.0", strerror(errno));
fflush(error_file);
exit(1);
/*
fprintf(error_file,
" \n malloc: Attemping to allocate %d bytes", size);
*/
}
return ptr;
}
void *cf_calloc(size_t nelem, size_t elsize)
{
void *ptr;
if (!(ptr = calloc(nelem, elsize))) {
char stym[N_STYM]={'\0'};
time_t tym;
time(&tym);
strftime(stym, N_STYM, "%Y %b %e %T", localtime(&tym));
fprintf(error_file, "%s %s-%s: ERROR - %s\n",
stym, "cf_calloc", "1.0", strerror(errno));
fflush(error_file);
exit(1);
/*
fprintf(error_file,
" \n calloc: Attemping to allocate %d bytes",
nelem * elsize);
*/
}
return ptr;
}
void cf_if_memory_error(int status)
{
time_t tym;
if (!status) {
time(&tym);
fprintf(error_file, "Memory allocation error in %s-%s: %s",
program_name, version_no, ctime(&tym));
/* Print out cfitsio error messages and continue */
fits_report_error(error_file, status);
fflush(error_file);
}
}
/*
int main()
{
int i;
cf_error_init("test program", "1.0", stdout);
cf_if_warning("Non-fatal error occurred");
for (i=0; i < 10; i++)
cf_if_fits_warning(i);
exit(0);
}
*/
|