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