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