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