1 /*
2      ccp4_errno.h: Header file for error handling routines
3      Copyright (C) 2001  CCLRC, Charles Ballard and Martyn Winn
4 
5      This library is free software: you can redistribute it and/or
6      modify it under the terms of the GNU Lesser General Public License
7      version 3, modified in accordance with the provisions of the
8      license to address the requirements of UK law.
9 
10      You should have received a copy of the modified GNU Lesser General
11      Public License along with this library.  If not, copies may be
12      downloaded from http://www.ccp4.ac.uk/ccp4license.php
13 
14      This program is distributed in the hope that it will be useful,
15      but WITHOUT ANY WARRANTY; without even the implied warranty of
16      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17      GNU Lesser General Public License for more details.
18 */
19 /** @file ccp4_errno.h
20  *  Header file for error handling routines
21  *  base error codes on system errors.
22  */
23 
24 #ifndef __CCP4_ERROR_GUARD
25 #define __CCP4_ERROR_GUARD
26 
27 #include <errno.h>
28 
29 /* rcsidhe[] = "$Id$" */
30 
31 #ifndef CCP4_ERRSYSTEM
32 #define CCP4_ERRSYSTEM(x) (((x)&0xfff)<<24)
33 #endif
34 #ifndef CCP4_ERRLEVEL
35 #define CCP4_ERRLEVEL(x)  (((x)&0xf)<<16)
36 #endif
37 #ifndef CCP4_ERRSETLEVEL
38 #define CCP4_ERRSETLEVEL(y,x) ((y) & (~CCP4_ERRLEVEL(0xf)) | CCP4_ERRLEVEL(x)))
39 #endif
40 #ifndef CCP4_ERRGETSYS
41 #define CCP4_ERRGETSYS(x)   (((x)>>24)&0xfff)
42 #endif
43 #ifndef CCP4_ERRGETLEVEL
44 #define CCP4_ERRGETLEVEL(x) (((x)>>16)&0xf)
45 #endif
46 #ifndef CCP4_ERRGETCODE
47 #define CCP4_ERRGETCODE(x)  ((x)&0xffff)
48 #endif
49 
50 #define CCP4_ERR_SYS CCP4_ERRSYSTEM(0x0)
51 #define CCP4_ERR_FILE CCP4_ERRSYSTEM(0x1)
52 #define CCP4_ERR_COORD CCP4_ERRSYSTEM(0x2)
53 #define CCP4_ERR_MTZ CCP4_ERRSYSTEM(0x3)
54 #define CCP4_ERR_MAP CCP4_ERRSYSTEM(0x4)
55 #define CCP4_ERR_UTILS CCP4_ERRSYSTEM(0x5)
56 #define CCP4_ERR_PARS CCP4_ERRSYSTEM(0x6)
57 #define CCP4_ERR_SYM CCP4_ERRSYSTEM(0x7)
58 #define CCP4_ERR_GEN CCP4_ERRSYSTEM(0x8)
59 
60 #define CCP4_COUNT(x) sizeof(x)/sizeof(x[0])
61 
62 /** @global ccp4_errno: global variable that stores the error last error
63  *           code from the ccp4 libraries
64  * | 12 bits - library | 4 bits - level | 16 bits - code |
65  *
66  *  associated macros
67  *    CCP4_ERR_SYS   0     OS error
68  *    CCP4_ERR_FILE  1     io library
69  *    CCP4_ERR_COORD 2     mmdb
70  *    CCP4_ERR_MTZ   3     cmtz
71  *    CCP4_ERR_MAP   4     map io
72  *    CCP4_ERR_UTILS 5     utility routines
73  *    CCP4_ERR_PARS  6     parser routines
74  *    CCP4_ERR_SYM   7     csymlib
75  *
76  * and bit manipulation
77  *    CCP4_ERRSYSTEM   system mask for setting
78  *    CCP4_ERRLEVEL    error level mask
79  *    CCP4_ERRSETLEVEL error level mask for setting error level
80  *    CCP4_ERRGETSYS   mask for returning system
81  *    CCP4_ERRGETLEVEL mask for returning level
82  *    CCP4_ERRGETCODE  mask for returning the code
83  *
84  * error levels
85  *    0  Success
86  *    1  Informational
87  *    2  Warning
88  *    3  Error
89  *    4  Fatal
90  */
91 #ifdef __cplusplus
92 extern "C" {
93 #endif
94 extern int ccp4_errno;
95 #ifdef __cplusplus
96 }
97 #endif
98 
99 #ifdef __cplusplus
100 namespace CCP4 {
101 extern "C" {
102 #endif
103 
104 /** Print out passed message and internal message based upon
105  *  ccp4_errno
106  *            "message : error message "
107  * @param message (const char *)
108  * @return void
109  */
110 void ccp4_error( const char *);
111 
112 /** Obtain character string based upon error code.
113  *  Typical use ccp4_strerror(ccp4_errno)
114  *  The returned string is statically allocated in the
115  *  library_err.c file and should not be freed.
116  * @param error code (int)
117  * @return const pointer to error message (const char *)
118  */
119 const char *ccp4_strerror( int);
120 
121 /** Wrapper for ccp4_error which also calls exit(1)
122  * @param message (const char *)
123  * @return void
124  */
125 void ccp4_fatal(const char *);
126 
127 /** Function to set verbosity level for messages from
128  *  ccp4_signal. Currently just off (0) and on (1).
129  *  It should be generalised to be able to switch
130  *  individual components on and off, i.e. replace 1 by
131  *  a mask.
132  *  cf. ccp4VerbosityLevel which sets the verbosity level
133  *  for ccp4printf  These are separate as they may be used
134  *  differently.
135  * @param iverb If >= 0 then set the verbosity level to the
136  *  value of iverb. If < 0 (by convention -1) then report
137  *  current level.
138  * @return current verbosity level
139  */
140 int ccp4_liberr_verbosity(int iverb);
141 
142 /** Routine to set ccp4_errno and print out message for
143  *  error tracing. This should be the only way in
144  *  which ccp4_errno is set.
145  *  See error codes above for levels and systems.
146  *  A callback with prototype void function(void)
147  *  may also be passed to the routine.
148  *  Note: FATAL calls exit(1).
149  *  If ccp4_liberr_verbosity returns 0, then ccp4_signal sets
150  *  ccp4_errno and returns without doing anything else.
151  * @param error code (int)
152  * @param message (const char * const)
153  * @param callback (point to routine void (*)(void) )
154  * @return void
155  */
156 void ccp4_signal(const int, const char *const, void (*)());
157 
158 int cfile_perror(const char *);
159 
160 #ifdef __cplusplus
161 }
162 }
163 #endif
164 
165 #endif  /*!CCP4_ERROR_GUARD */
166