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