WCSLIB  7.5
wcserr.h
Go to the documentation of this file.
1 /*============================================================================
2  WCSLIB 7.5 - an implementation of the FITS WCS standard.
3  Copyright (C) 1995-2021, Mark Calabretta
4 
5  This file is part of WCSLIB.
6 
7  WCSLIB is free software: you can redistribute it and/or modify it under the
8  terms of the GNU Lesser General Public License as published by the Free
9  Software Foundation, either version 3 of the License, or (at your option)
10  any later version.
11 
12  WCSLIB is distributed in the hope that it will be useful, but WITHOUT ANY
13  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15  more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with WCSLIB. If not, see http://www.gnu.org/licenses.
19 
20  Author: Mark Calabretta, Australia Telescope National Facility, CSIRO.
21  Module author: Michael Droettboom
22  http://www.atnf.csiro.au/people/Mark.Calabretta
23  $Id: wcserr.h,v 7.5 2021/03/20 05:54:58 mcalabre Exp $
24 *=============================================================================
25 *
26 * WCSLIB 7.5 - C routines that implement the FITS World Coordinate System
27 * (WCS) standard. Refer to the README file provided with WCSLIB for an
28 * overview of the library.
29 *
30 * Summary of the wcserr routines
31 * ------------------------------
32 * Most of the structs in WCSLIB contain a pointer to a wcserr struct as a
33 * member. Functions in WCSLIB that return an error status code can also
34 * allocate and set a detailed error message in this struct which also
35 * identifies the function, source file, and line number where the error
36 * occurred.
37 *
38 * For example:
39 *
40 = struct prjprm prj;
41 = wcserr_enable(1);
42 = if (prjini(&prj)) {
43 = // Print the error message to stderr.
44 = wcsprintf_set(stderr);
45 = wcserr_prt(prj.err, 0x0);
46 = }
47 *
48 * A number of utility functions used in managing the wcserr struct are for
49 * internal use only. They are documented here solely as an aid to
50 * understanding the code. They are not intended for external use - the API
51 * may change without notice!
52 *
53 *
54 * wcserr struct - Error message handling
55 * --------------------------------------
56 * The wcserr struct contains the numeric error code, a textual description of
57 * the error, and information about the function, source file, and line number
58 * where the error was generated.
59 *
60 * int status
61 * Numeric status code associated with the error, the meaning of which
62 * depends on the function that generated it. See the documentation for
63 * the particular function.
64 *
65 * int line_no
66 * Line number where the error occurred as given by the __LINE__
67 * preprocessor macro.
68 *
69 * const char *function
70 * Name of the function where the error occurred.
71 *
72 * const char *file
73 * Name of the source file where the error occurred as given by the
74 * __FILE__ preprocessor macro.
75 *
76 * char *msg
77 * Informative error message.
78 *
79 *
80 * wcserr_enable() - Enable/disable error messaging
81 * ------------------------------------------------
82 * wcserr_enable() enables or disables wcserr error messaging. By default it
83 * is disabled.
84 *
85 * PLEASE NOTE: This function is not thread-safe.
86 *
87 * Given:
88 * enable int If true (non-zero), enable error messaging, else
89 * disable it.
90 *
91 * Function return value:
92 * int Status return value:
93 * 0: Error messaging is disabled.
94 * 1: Error messaging is enabled.
95 *
96 *
97 * wcserr_prt() - Print a wcserr struct
98 * ------------------------------------
99 * wcserr_prt() prints the error message (if any) contained in a wcserr struct.
100 * It uses the wcsprintf() functions.
101 *
102 * Given:
103 * err const struct wcserr*
104 * The error object. If NULL, nothing is printed.
105 *
106 * prefix const char *
107 * If non-NULL, each output line will be prefixed with
108 * this string.
109 *
110 * Function return value:
111 * int Status return value:
112 * 0: Success.
113 * 2: Error messaging is not enabled.
114 *
115 *
116 * wcserr_clear() - Clear a wcserr struct
117 * --------------------------------------
118 * wcserr_clear() clears (deletes) a wcserr struct.
119 *
120 * Given and returned:
121 * err struct wcserr**
122 * The error object. If NULL, nothing is done. Set to
123 * NULL on return.
124 *
125 * Function return value:
126 * int Status return value:
127 * 0: Success.
128 *
129 *
130 * wcserr_set() - Fill in the contents of an error object
131 * ------------------------------------------------------
132 * INTERNAL USE ONLY.
133 *
134 * wcserr_set() fills a wcserr struct with information about an error.
135 *
136 * A convenience macro, WCSERR_SET, provides the source file and line number
137 * information automatically.
138 *
139 * Given and returned:
140 * err struct wcserr**
141 * Error object.
142 *
143 * If err is NULL, returns the status code given without
144 * setting an error message.
145 *
146 * If *err is NULL, allocates memory for a wcserr struct
147 * (provided that status is non-zero).
148 *
149 * Given:
150 * status int Numeric status code to set. If 0, then *err will be
151 * deleted and *err will be returned as NULL.
152 *
153 * function const char *
154 * Name of the function generating the error. This
155 * must point to a constant string, i.e. in the
156 * initialized read-only data section ("data") of the
157 * executable.
158 *
159 * file const char *
160 * Name of the source file generating the error. This
161 * must point to a constant string, i.e. in the
162 * initialized read-only data section ("data") of the
163 * executable such as given by the __FILE__ preprocessor
164 * macro.
165 *
166 * line_no int Line number in the source file generating the error
167 * such as given by the __LINE__ preprocessor macro.
168 *
169 * format const char *
170 * Format string of the error message. May contain
171 * printf-style %-formatting codes.
172 *
173 * ... mixed The remaining variable arguments are applied (like
174 * printf) to the format string to generate the error
175 * message.
176 *
177 * Function return value:
178 * int The status return code passed in.
179 *
180 *
181 * wcserr_copy() - Copy an error object
182 * ------------------------------------
183 * INTERNAL USE ONLY.
184 *
185 * wcserr_copy() copies one error object to another. Use of this function
186 * should be avoided in general since the function, source file, and line
187 * number information copied to the destination may lose its context.
188 *
189 * Given:
190 * src const struct wcserr*
191 * Source error object. If src is NULL, dst is cleared.
192 *
193 * Returned:
194 * dst struct wcserr*
195 * Destination error object. If NULL, no copy is made.
196 *
197 * Function return value:
198 * int Numeric status code of the source error object.
199 *
200 *
201 * WCSERR_SET() macro - Fill in the contents of an error object
202 * ------------------------------------------------------------
203 * INTERNAL USE ONLY.
204 *
205 * WCSERR_SET() is a preprocessor macro that helps to fill in the argument list
206 * of wcserr_set(). It takes status as an argument of its own and provides the
207 * name of the source file and the line number at the point where invoked. It
208 * assumes that the err and function arguments of wcserr_set() will be provided
209 * by variables of the same names.
210 *
211 *===========================================================================*/
212 
213 #ifndef WCSLIB_WCSERR
214 #define WCSLIB_WCSERR
215 
216 #ifdef __cplusplus
217 extern "C" {
218 #endif
219 
220 struct wcserr {
221  int status; // Status code for the error.
222  int line_no; // Line number where the error occurred.
223  const char *function; // Function name.
224  const char *file; // Source file name.
225  char *msg; // Informative error message.
226 };
227 
228 // Size of the wcserr struct in int units, used by the Fortran wrappers.
229 #define ERRLEN (sizeof(struct wcserr)/sizeof(int))
230 
231 int wcserr_enable(int enable);
232 
233 int wcserr_prt(const struct wcserr *err, const char *prefix);
234 
235 int wcserr_clear(struct wcserr **err);
236 
237 
238 // INTERNAL USE ONLY -------------------------------------------------------
239 
240 int wcserr_set(struct wcserr **err, int status, const char *function,
241  const char *file, int line_no, const char *format, ...);
242 
243 int wcserr_copy(const struct wcserr *src, struct wcserr *dst);
244 
245 // Convenience macro for invoking wcserr_set().
246 #define WCSERR_SET(status) err, status, function, __FILE__, __LINE__
247 
248 #ifdef __cplusplus
249 }
250 #endif
251 
252 #endif // WSCLIB_WCSERR
Error message handling.
Definition: wcserr.h:220
int line_no
Definition: wcserr.h:222
const char * file
Definition: wcserr.h:224
int status
Definition: wcserr.h:221
char * msg
Definition: wcserr.h:225
int wcserr_enable(int enable)
Enable/disable error messaging.
int wcserr_prt(const struct wcserr *err, const char *prefix)
Print a wcserr struct.
int wcserr_clear(struct wcserr **err)
Clear a wcserr struct.
int wcserr_set(struct wcserr **err, int status, const char *function, const char *file, int line_no, const char *format,...)
Fill in the contents of an error object.
int wcserr_copy(const struct wcserr *src, struct wcserr *dst)
Copy an error object.