Bioplib
Protein Structure C Library
 All Data Structures Files Functions Variables Typedefs Macros Pages
ErrStack.c
Go to the documentation of this file.
1 /************************************************************************/
2 /**
3 
4  \file ErrStack.c
5 
6  \version V1.1
7  \date 07.07.14
8  \brief Build and print an error stack for program failure.
9 
10  \copyright (c) UCL / Dr. Andrew C. R. Martin 1994-2014
11  \author Dr. Andrew C. R. Martin
12  \par
13  Institute of Structural & Molecular Biology,
14  University College London,
15  Gower Street,
16  London.
17  WC1E 6BT.
18  \par
19  andrew@bioinf.org.uk
20  andrew.martin@ucl.ac.uk
21 
22 **************************************************************************
23 
24  This code is NOT IN THE PUBLIC DOMAIN, but it may be copied
25  according to the conditions laid out in the accompanying file
26  COPYING.DOC.
27 
28  The code may be modified as required, but any modifications must be
29  documented so that the person responsible can be identified.
30 
31  The code may not be sold commercially or included as part of a
32  commercial product except as described in the file COPYING.DOC.
33 
34 **************************************************************************
35 
36  Description:
37  ============
38 
39 
40  This set of routines allows a stack of errors to be created.
41  When a program has a fatal error, the StoreError() routine is called
42  to place the error on the stack. As the program un-winds, each
43  routine which fails stores it's error. Finally, before the program
44  actually exits, it calls ShowErrors() to display the error stack.
45 
46 **************************************************************************
47 
48  Usage:
49  ======
50 
51  blStoreError(char *routine, char *error)
52 
53  The routine is called with the name of the routine at fault and the
54  description of the fault.
55 
56  blShowErrors(void *PrintRoutine, BOOL Trace)
57 
58  The routine is called with a pointer to the routine which is to
59  do the actual error display and a flag to indicate whether the
60  faulty routine names should be displyed. This is only of use if
61  the user has access to the source code so should be used for
62  debugging purposes only.
63 
64  If PrintRoutine is supplied as NULL, the simple PrintAnError()
65  routine will be used which displays the error on stderr. More
66  complex routines could, for example, show the error in a requester
67  or output to a window.
68 
69  If a print routine is specified, the routine is called with:
70 
71  blShowErrors(void *)MyRoutine, TRUE);
72 
73 **************************************************************************
74 
75  Revision History:
76  =================
77 - V1.0 31.08.94 Original By: ACRM
78 - V1.1 07.07.14 Use bl prefix for functions By: CTP
79 
80 *************************************************************************/
81 /* Doxygen
82  -------
83  #GROUP General Programming
84  #SUBGROUP Handling errors
85  #FUNCTION blStoreError()
86  Stores an error on the error stack.
87 
88  #FUNCTION blShowErrors()
89  Display the error stack using the supplied print routine or the
90  simple default one if NULL is given.
91 */
92 /************************************************************************/
93 /* Includes
94 */
95 #include <stdio.h>
96 #include <stdlib.h>
97 #include <string.h>
98 
99 #include "SysDefs.h"
100 #include "macros.h"
101 
102 #include "ErrStack.h"
103 
104 /************************************************************************/
105 /* Defines and macros
106 */
107 typedef struct _errorstack
108 {
109  struct _errorstack *next;
110  char *error,
111  *routine;
112 } ERRORSTACK;
113 
114 /************************************************************************/
115 /* Globals
116 */
117 static ERRORSTACK *sErrorStack = NULL;
118 
119 /************************************************************************/
120 /* Prototypes
121 */
122 static void PrintAnError(char *error);
123 
124 /************************************************************************/
125 /*>void blStoreError(char *routine, char *error)
126  ---------------------------------------------
127 *//**
128 
129  \param[in] *routine Name of the routine generating the error
130  \param[in] *error Description of the error
131 
132  Stores an error on the error stack.
133 
134 - 31.08.94 Original By: ACRM
135 - 07.07.14 Use bl prefix for functions By: CTP
136 */
137 void blStoreError(char *routine, char *error)
138 {
139  static ERRORSTACK *p = NULL;
140 
141  if(sErrorStack == NULL)
142  {
143  INIT(sErrorStack, ERRORSTACK);
144  p = sErrorStack;
145  }
146  else
147  {
148  ALLOCNEXT(p, ERRORSTACK);
149  }
150 
151  if(p != NULL)
152  {
153  if((p->error = (char *)malloc((strlen(error)+1) * sizeof(char)))
154  != NULL)
155  {
156  strcpy(p->error, error);
157  }
158  if((p->routine = (char *)malloc((strlen(routine)+1) * sizeof(char)))
159  != NULL)
160  {
161  strcpy(p->routine, routine);
162  }
163  }
164 }
165 
166 /************************************************************************/
167 /*>void blShowErrors(void *PrintRoutine(char *), BOOL Trace)
168  ---------------------------------------------------------
169 *//**
170 
171  \param[in] *PrintRoutine The print routine or NULL
172  \param[in] Trace Flag to print routine names
173 
174  Display the error stack using the supplied print routine or the
175  simple default one if NULL is given.
176 
177 - 31.08.94 Original By: ACRM
178 - 06.09.94 No longer tries to set PrintRoutine if was NULL (strict ANSI
179  compliance)
180 - 07.07.14 Use bl prefix for functions By: CTP
181 */
182 void blShowErrors(void *PrintRoutine(char *), BOOL Trace)
183 {
184  ERRORSTACK *p;
185  char buffer[160];
186 
187  for(p=sErrorStack; p!=NULL; NEXT(p))
188  {
189  if(Trace)
190  sprintf(buffer,"%s : %s",p->routine,p->error);
191  else
192  strcpy(buffer,p->error);
193 
194  if(PrintRoutine == NULL)
195  PrintAnError(buffer);
196  else
197  (*PrintRoutine)(buffer);
198  }
199 }
200 
201 /************************************************************************/
202 /*>static void PrintAnError(char *string)
203  ---------------------------------------
204 *//**
205 
206  \param[in] *string A string to be printed
207 
208  A simple error printing routine used if NULL given as a parameter
209  to ShowErrors()
210 
211 - 31.08.94 Original By: ACRM
212 */
213 static void PrintAnError(char *string)
214 {
215  fprintf(stderr,"%s\n",string);
216 }
217 
#define ALLOCNEXT(x, y)
Definition: macros.h:251
short BOOL
Definition: SysDefs.h:64
char * routine
Definition: ErrStack.c:110
#define NULL
Definition: array2.c:99
#define NEXT(x)
Definition: macros.h:249
Useful macros.
void blShowErrors(void *PrintRoutine(char *), BOOL Trace)
Definition: ErrStack.c:182
char * error
Definition: ErrStack.c:110
void blStoreError(char *routine, char *error)
Definition: ErrStack.c:137
struct _errorstack ERRORSTACK
#define INIT(x, y)
Definition: macros.h:244
System-type variable type definitions.
struct _errorstack * next
Definition: ErrStack.c:109
Build and print an error stack for program failure.