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