Debian Codegraph @

dune/uggrid/low/debug.cc

raw
1 // SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LGPL-2.1-or-later
3 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 // vi: set et ts=4 sw=2 sts=2:
5 /****************************************************************************/
6 /* */
7 /* File: debug.c */
8 /* */
9 /* Purpose: ug internal debugger functions */
10 /* */
11 /* Author: Stefan Lang */
12 /* Institut fuer Computeranwendungen III */
13 /* Universitaet Stuttgart */
14 /* Pfaffenwaldring 27 */
15 /* 70550 Stuttgart */
16 /* email: [email protected] */
17 /* phone: 0049-(0)711-685-7003 */
18 /* fax : 0049-(0)711-685-7000 */
19 /* */
20 /* History: 10.07.95 begin */
21 /* */
22 /* Remarks: */
23 /* */
24 /****************************************************************************/
25
26 /****************************************************************************/
27 /* */
28 /* include files */
29 /* system include files */
30 /* application include files */
31 /* */
32 /****************************************************************************/
33
34 #include <config.h>
35
36 #ifdef Debug
37 #define compile_debug
38
39 #include <cstring>
40 #include <cstdio>
41 #include <stdarg.h>
42
43 #include <dune/uggrid/ugdevices.h>
44 #include "fileopen.h"
45 #include "debug.h"
46
47 #include <dune/uggrid/parallel/ppif/ppif.h>
48
49 USING_UG_NAMESPACE
50
51 /****************************************************************************/
52 /* */
53 /* defines in the following order */
54 /* */
55 /* compile time constants defining static data size (i.e. arrays) */
56 /* other constants */
57 /* macros */
58 /* */
59 /****************************************************************************/
60
61
62
63 /****************************************************************************/
64 /* */
65 /* data structures used in this source file (exported data structures are */
66 /* in the corresponding include file!) */
67 /* */
68 /****************************************************************************/
69
70 /****************************************************************************/
71 /* */
72 /* definition of exported global variables */
73 /* */
74 /****************************************************************************/
75
76 int NS_PREFIX Debuginit = 0;
77 int NS_PREFIX Debugdddif = 0; /* temporary setting for debugging ModelP */
78 int NS_PREFIX Debugdev = 0;
79 int NS_PREFIX Debugdom = 0;
80 int NS_PREFIX Debuggm = 0;
81 int NS_PREFIX Debuglow = 0;
82
83 int NS_PREFIX rep_err_count;
84 int NS_PREFIX rep_err_line[REP_ERR_MAX];
85 const char* NS_PREFIX rep_err_file[REP_ERR_MAX];
86
87 /****************************************************************************/
88 /* */
89 /* definition of variables global to this source file only (static!) */
90 /* */
91 /****************************************************************************/
92
93 static PrintfProcPtr printdebug=printf;
94 static FILE *debugfile=NULL;
95 static char *debugfilename;
96
97
98 /****************************************************************************/
99 /* */
100 /* forward declarations of functions used before they are defined */
101 /* */
102 /****************************************************************************/
103
104
105 /****************************************************************************/
106 /* */
107 /* Function: PrintDebug */
108 /* */
109 /* Purpose: Print debugging information to ugshell or logfile */
110 /* */
111 /* Input: arguments are passed to PrintDebug in a printf like manner. */
112 /* char *format: format, which contains the debugging info */
113 /* ... : list of arguments for format string */
114 /* */
115 /* Output: void */
116 /* */
117 /****************************************************************************/
118
119 int NS_PREFIX PrintDebug (const char *format, ...)
120 {
121 char buffer[4096];
122 va_list args;
123
124 va_start(args,format);
125
126 vsprintf(buffer,format,args);
127 #ifdef ModelP
128 if (PPIF::me==PPIF::master) {
129 #endif
130
131 /* use specific debug function for displaying */
132 (*printdebug)(buffer);
133 WriteLogFile((const char *)buffer);
134
135 #ifdef ModelP
136 }
137 else
138 {
139 printf(buffer);
140 fflush(stdout);
141 WriteLogFile(buffer);
142 }
143
144 #endif
145
146 va_end(args);
147 return (0);
148 }
149
150 void NS_PREFIX SetPrintDebugProc (PrintfProcPtr print)
151 {
152 printdebug = print;
153 }
154
155 int NS_PREFIX PrintDebugToFile (const char *format, ...)
156 {
157 va_list args;
158
159 /* initialize args */
160 va_start(args,format);
161
162 vfprintf(debugfile,format,args);
163 fflush(debugfile);
164
165 /* garbage collection */
166 va_end(args);
167
168 return (0);
169 }
170
171 int NS_PREFIX SetPrintDebugToFile (const char *fname)
172 {
173 if (debugfile!=NULL)
174 return (1);
175 if ((debugfile=fileopen(fname,"w"))==NULL)
176 return (1);
177
178 debugfilename = strdup(fname);
179 SetPrintDebugProc(PrintDebugToFile);
180
181 return (0);
182 }
183
184 int PostprocessDebugFile (const char *newname)
185 {
186 # ifndef ModelP
187 char c;
188
189 if (debugfile==NULL)
190 return (1);
191 if (debugfilename==NULL)
192 return (1);
193 if (fclose(debugfile))
194 return (1);
195 if ((debugfile=fileopen(debugfilename,"r"))==NULL)
196 return (1);
197 if ((c=getc(debugfile))==EOF)
198 {
199 /* remove empty file */
200 if (fclose(debugfile))
201 return (1);
202 if (remove(debugfilename))
203 return (1);
204 }
205 else if (newname!=NULL)
206 {
207 /* rename nonemty file */
208 if (fclose(debugfile))
209 return (1);
210 remove(newname);
211 if (rename(debugfilename,newname))
212 return (1);
213 }
214 # endif
215 return (0);
216 }
217
218 INT PrintRepErrStack (PrintfProcPtr print)
219 {
220 if (rep_err_count==0)
221 print("no errors are reported\n");
222 else
223 {
224 INT i;
225
226 print("reported errors are:\n\n");
227
228 for (i=0; i<rep_err_count; i++)
229 print("%2d: File: %20s, Line: %5d\n",i,rep_err_file[i],rep_err_line[i]);
230 }
231
232 return (0);
233 }
234
235 /* TODO: delete this */
236 /*
237
238 static int InitDebug()
239 {
240 SetPrintDebugProc(printf);
241 return(0);
242 }
243
244 main()
245 {
246 char string[10]= "Hallo";
247 int n=1234;
248 InitDebug();
249 PrintDebug("This is a string:%s\n",string);
250 PrintDebug("This is a integer:%d\n",n);
251 }
252 */
253
254 #endif /* Debug */