xref: /petsc/src/sys/info/verboseinfo.c (revision 2205254efee3a00a594e5e2a3a70f74dcb40bc03)
1 
2 /*
3       PetscInfo() is contained in a different file from the other profiling to
4    allow it to be replaced at link time by an alternative routine.
5 */
6 #include <petscsys.h>        /*I    "petscsys.h"   I*/
7 #include <stdarg.h>
8 #include <sys/types.h>
9 #if defined(PETSC_HAVE_STDLIB_H)
10 #include <stdlib.h>
11 #endif
12 #if defined(PETSC_HAVE_MALLOC_H)
13 #include <malloc.h>
14 #endif
15 
16 /*
17   The next three variables determine which, if any, PetscInfo() calls are used.
18   If PetscLogPrintInfo is zero, no info messages are printed.
19   If PetscLogPrintInfoNull is zero, no info messages associated with a null object are printed.
20 
21   If PetscInfoFlags[OBJECT_CLASSID - PETSC_SMALLEST_CLASSID] is zero, no messages related
22   to that object are printed. OBJECT_CLASSID is, for example, MAT_CLASSID.
23 */
24 PetscBool PetscLogPrintInfo     = PETSC_FALSE;
25 PetscBool PetscLogPrintInfoNull = PETSC_FALSE;
26 int       PetscInfoFlags[]      = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
27                                    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
28                                    1,1,1,1,1,1,1,1,1,1,1,1};
29 FILE      *PetscInfoFile = PETSC_NULL;
30 
31 #undef __FUNCT__
32 #define __FUNCT__ "PetscInfoAllow"
33 /*@C
34     PetscInfoAllow - Causes PetscInfo() messages to be printed to standard output.
35 
36     Not Collective, each processor may call this separately, but printing is only
37     turned on if the lowest processor number associated with the PetscObject associated
38     with the call to PetscInfo() has called this routine.
39 
40     Input Parameter:
41 +   flag - PETSC_TRUE or PETSC_FALSE
42 -   filename - optional name of file to write output to (defaults to stdout)
43 
44     Options Database Key:
45 .   -info [optional filename] - Activates PetscInfoAllow()
46 
47     Level: advanced
48 
49    Concepts: debugging^detailed runtime information
50    Concepts: dumping detailed runtime information
51 
52 .seealso: PetscInfo()
53 @*/
54 PetscErrorCode  PetscInfoAllow(PetscBool flag, const char filename[])
55 {
56   char           fname[PETSC_MAX_PATH_LEN], tname[5];
57   PetscMPIInt    rank;
58   PetscErrorCode ierr;
59 
60   PetscFunctionBegin;
61   if (flag && filename) {
62     ierr = PetscFixFilename(filename, fname);CHKERRQ(ierr);
63     ierr = MPI_Comm_rank(PETSC_COMM_WORLD, &rank);CHKERRQ(ierr);
64     sprintf(tname, ".%d", rank);
65     ierr = PetscStrcat(fname, tname);CHKERRQ(ierr);
66     ierr = PetscFOpen(MPI_COMM_SELF, fname, "w", &PetscInfoFile);CHKERRQ(ierr);
67     if (!PetscInfoFile) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN, "Cannot open requested file for writing: %s",fname);
68   } else if (flag) PetscInfoFile = PETSC_STDOUT;
69 
70   PetscLogPrintInfo     = flag;
71   PetscLogPrintInfoNull = flag;
72   PetscFunctionReturn(0);
73 }
74 
75 #undef __FUNCT__
76 #define __FUNCT__ "PetscInfoDeactivateClass"
77 /*@
78   PetscInfoDeactivateClass - Deactivates PlogInfo() messages for a PETSc object class.
79 
80   Not Collective
81 
82   Input Parameter:
83 . objclass - The object class,  e.g., MAT_CLASSID, SNES_CLASSID, etc.
84 
85   Notes:
86   One can pass 0 to deactivate all messages that are not associated with an object.
87 
88   Level: developer
89 
90 .keywords: allow, information, printing, monitoring
91 .seealso: PetscInfoActivateClass(), PetscInfo(), PetscInfoAllow()
92 @*/
93 PetscErrorCode  PetscInfoDeactivateClass(int objclass)
94 {
95   PetscFunctionBegin;
96   if (!objclass) {
97     PetscLogPrintInfoNull = PETSC_FALSE;
98     PetscFunctionReturn(0);
99   }
100   PetscInfoFlags[objclass - PETSC_SMALLEST_CLASSID - 1] = 0;
101   PetscFunctionReturn(0);
102 }
103 
104 #undef __FUNCT__
105 #define __FUNCT__ "PetscInfoActivateClass"
106 /*@
107   PetscInfoActivateClass - Activates PlogInfo() messages for a PETSc object class.
108 
109   Not Collective
110 
111   Input Parameter:
112 . objclass - The object class, e.g., MAT_CLASSID, SNES_CLASSID, etc.
113 
114   Notes:
115   One can pass 0 to activate all messages that are not associated with an object.
116 
117   Level: developer
118 
119 .keywords: allow, information, printing, monitoring
120 .seealso: PetscInfoDeactivateClass(), PetscInfo(), PetscInfoAllow()
121 @*/
122 PetscErrorCode  PetscInfoActivateClass(int objclass)
123 {
124   PetscFunctionBegin;
125   if (!objclass) PetscLogPrintInfoNull = PETSC_TRUE;
126   else PetscInfoFlags[objclass - PETSC_SMALLEST_CLASSID - 1] = 1;
127 
128   PetscFunctionReturn(0);
129 }
130 
131 /*
132    If the option -history was used, then all printed PetscInfo()
133   messages are also printed to the history file, called by default
134   .petschistory in ones home directory.
135 */
136 extern FILE *petsc_history;
137 
138 #undef __FUNCT__
139 #define __FUNCT__ "PetscInfo_Private"
140 /*MC
141     PetscInfo - Logs informative data, which is printed to standard output
142     or a file when the option -info <file> is specified.
143 
144    Synopsis:
145        #include "petscsys.h"
146        PetscErrorCode PetscInfo(void *vobj, const char message[])
147        PetscErrorCode PetscInfo1(void *vobj, const char formatmessage[],arg1)
148        PetscErrorCode PetscInfo2(void *vobj, const char formatmessage[],arg1,arg2)
149        etc
150 
151     Collective over PetscObject argument
152 
153     Input Parameter:
154 +   vobj - object most closely associated with the logging statement or PETSC_NULL
155 .   message - logging message
156 -   formatmessage - logging message using standard "printf" format
157 
158     Options Database Key:
159 $    -info : activates printing of PetscInfo() messages
160 
161     Level: intermediate
162 
163     Fortran Note: This function does not take the vobj argument, there is only the PetscInfo()
164      version, not PetscInfo1() etc.
165 
166     Example of Usage:
167 $
168 $     Mat A
169 $     double alpha
170 $     PetscInfo1(A,"Matrix uses parameter alpha=%g\n",alpha);
171 $
172 
173    Concepts: runtime information
174 
175 .seealso: PetscInfoAllow()
176 M*/
177 PetscErrorCode  PetscInfo_Private(const char func[],void *vobj, const char message[], ...)
178 {
179   va_list        Argp;
180   PetscMPIInt    rank,urank;
181   size_t         len;
182   PetscObject    obj = (PetscObject)vobj;
183   char           string[8*1024];
184   PetscErrorCode ierr;
185   size_t         fullLength;
186   int            err;
187 
188   PetscFunctionBegin;
189   if (obj) PetscValidHeader(obj,1);
190   PetscValidCharPointer(message,2);
191   if (!PetscLogPrintInfo) PetscFunctionReturn(0);
192   if ((!PetscLogPrintInfoNull) && !vobj) PetscFunctionReturn(0);
193   if (obj && !PetscInfoFlags[obj->classid - PETSC_SMALLEST_CLASSID - 1]) PetscFunctionReturn(0);
194   if (!obj) rank = 0;
195   else {
196     ierr = MPI_Comm_rank(obj->comm, &rank);CHKERRQ(ierr);
197   }
198   if (rank) PetscFunctionReturn(0);
199 
200   ierr = MPI_Comm_rank(MPI_COMM_WORLD, &urank);CHKERRQ(ierr);
201   va_start(Argp, message);
202   sprintf(string, "[%d] %s(): ", urank,func);
203   ierr = PetscStrlen(string, &len);CHKERRQ(ierr);
204   ierr = PetscVSNPrintf(string+len, 8*1024-len,message,&fullLength, Argp);CHKERRQ(ierr);
205   ierr = PetscFPrintf(PETSC_COMM_SELF,PetscInfoFile, "%s", string);CHKERRQ(ierr);
206   err  = fflush(PetscInfoFile);
207   if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
208   if (petsc_history) {
209     va_start(Argp, message);
210     ierr = (*PetscVFPrintf)(petsc_history, message, Argp);CHKERRQ(ierr);
211   }
212   va_end(Argp);
213   PetscFunctionReturn(0);
214 }
215