xref: /petsc/src/sys/info/verboseinfo.c (revision 95c0884e6f7665b705eebf88174e89dc920c2fc0)
15c6c1daeSBarry Smith 
25c6c1daeSBarry Smith /*
35c6c1daeSBarry Smith       PetscInfo() is contained in a different file from the other profiling to
45c6c1daeSBarry Smith    allow it to be replaced at link time by an alternative routine.
55c6c1daeSBarry Smith */
6af0996ceSBarry Smith #include <petsc/private/petscimpl.h>        /*I    "petscsys.h"   I*/
75c6c1daeSBarry Smith 
85c6c1daeSBarry Smith /*
95c6c1daeSBarry Smith   The next three variables determine which, if any, PetscInfo() calls are used.
105c6c1daeSBarry Smith   If PetscLogPrintInfo is zero, no info messages are printed.
115c6c1daeSBarry Smith 
125c6c1daeSBarry Smith   If PetscInfoFlags[OBJECT_CLASSID - PETSC_SMALLEST_CLASSID] is zero, no messages related
135c6c1daeSBarry Smith   to that object are printed. OBJECT_CLASSID is, for example, MAT_CLASSID.
145c6c1daeSBarry Smith */
155c6c1daeSBarry Smith PetscBool PetscLogPrintInfo = PETSC_FALSE;
160298fd71SBarry Smith FILE     *PetscInfoFile     = NULL;
17fa2bb9feSLisandro Dalcin int       PetscInfoFlags[]  = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
18fa2bb9feSLisandro Dalcin                                1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
19fa2bb9feSLisandro Dalcin                                1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
20fa2bb9feSLisandro Dalcin                                1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
21fa2bb9feSLisandro Dalcin                                1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
225c6c1daeSBarry Smith 
235c6c1daeSBarry Smith /*@C
245c6c1daeSBarry Smith     PetscInfoAllow - Causes PetscInfo() messages to be printed to standard output.
255c6c1daeSBarry Smith 
265c6c1daeSBarry Smith     Not Collective, each processor may call this separately, but printing is only
275c6c1daeSBarry Smith     turned on if the lowest processor number associated with the PetscObject associated
285c6c1daeSBarry Smith     with the call to PetscInfo() has called this routine.
295c6c1daeSBarry Smith 
305c6c1daeSBarry Smith     Input Parameter:
315c6c1daeSBarry Smith +   flag - PETSC_TRUE or PETSC_FALSE
325c6c1daeSBarry Smith -   filename - optional name of file to write output to (defaults to stdout)
335c6c1daeSBarry Smith 
345c6c1daeSBarry Smith     Options Database Key:
355c6c1daeSBarry Smith .   -info [optional filename] - Activates PetscInfoAllow()
365c6c1daeSBarry Smith 
375c6c1daeSBarry Smith     Level: advanced
385c6c1daeSBarry Smith 
395c6c1daeSBarry Smith    Concepts: debugging^detailed runtime information
405c6c1daeSBarry Smith    Concepts: dumping detailed runtime information
415c6c1daeSBarry Smith 
425c6c1daeSBarry Smith .seealso: PetscInfo()
435c6c1daeSBarry Smith @*/
445c6c1daeSBarry Smith PetscErrorCode  PetscInfoAllow(PetscBool flag, const char filename[])
455c6c1daeSBarry Smith {
46fa2bb9feSLisandro Dalcin   char           fname[PETSC_MAX_PATH_LEN], tname[11];
475c6c1daeSBarry Smith   PetscMPIInt    rank;
485c6c1daeSBarry Smith   PetscErrorCode ierr;
495c6c1daeSBarry Smith 
505c6c1daeSBarry Smith   PetscFunctionBegin;
515c6c1daeSBarry Smith   if (flag && filename) {
525c6c1daeSBarry Smith     ierr = PetscFixFilename(filename, fname);CHKERRQ(ierr);
535c6c1daeSBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD, &rank);CHKERRQ(ierr);
545c6c1daeSBarry Smith     sprintf(tname, ".%d", rank);
555c6c1daeSBarry Smith     ierr = PetscStrcat(fname, tname);CHKERRQ(ierr);
565c6c1daeSBarry Smith     ierr = PetscFOpen(MPI_COMM_SELF, fname, "w", &PetscInfoFile);CHKERRQ(ierr);
575c6c1daeSBarry Smith     if (!PetscInfoFile) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN, "Cannot open requested file for writing: %s",fname);
58a297a907SKarl Rupp   } else if (flag) PetscInfoFile = PETSC_STDOUT;
59a297a907SKarl Rupp 
605c6c1daeSBarry Smith   PetscLogPrintInfo = flag;
615c6c1daeSBarry Smith   PetscFunctionReturn(0);
625c6c1daeSBarry Smith }
635c6c1daeSBarry Smith 
645c6c1daeSBarry Smith /*@
65fa2bb9feSLisandro Dalcin   PetscInfoDeactivateClass - Deactivates PetscInfo() messages for a PETSc object class.
665c6c1daeSBarry Smith 
675c6c1daeSBarry Smith   Not Collective
685c6c1daeSBarry Smith 
695c6c1daeSBarry Smith   Input Parameter:
70fa2bb9feSLisandro Dalcin . classid - The object class,  e.g., MAT_CLASSID, SNES_CLASSID, etc.
715c6c1daeSBarry Smith 
725c6c1daeSBarry Smith   Notes:
735c6c1daeSBarry Smith   One can pass 0 to deactivate all messages that are not associated with an object.
745c6c1daeSBarry Smith 
755c6c1daeSBarry Smith   Level: developer
765c6c1daeSBarry Smith 
775c6c1daeSBarry Smith .keywords: allow, information, printing, monitoring
785c6c1daeSBarry Smith .seealso: PetscInfoActivateClass(), PetscInfo(), PetscInfoAllow()
795c6c1daeSBarry Smith @*/
80fa2bb9feSLisandro Dalcin PetscErrorCode  PetscInfoDeactivateClass(PetscClassId classid)
815c6c1daeSBarry Smith {
825c6c1daeSBarry Smith   PetscFunctionBegin;
83fa2bb9feSLisandro Dalcin   if (!classid) classid = PETSC_SMALLEST_CLASSID;
84fa2bb9feSLisandro Dalcin   PetscInfoFlags[classid - PETSC_SMALLEST_CLASSID] = 0;
855c6c1daeSBarry Smith   PetscFunctionReturn(0);
865c6c1daeSBarry Smith }
875c6c1daeSBarry Smith 
885c6c1daeSBarry Smith /*@
89fa2bb9feSLisandro Dalcin   PetscInfoActivateClass - Activates PetscInfo() messages for a PETSc object class.
905c6c1daeSBarry Smith 
915c6c1daeSBarry Smith   Not Collective
925c6c1daeSBarry Smith 
935c6c1daeSBarry Smith   Input Parameter:
94fa2bb9feSLisandro Dalcin . classid - The object class, e.g., MAT_CLASSID, SNES_CLASSID, etc.
955c6c1daeSBarry Smith 
965c6c1daeSBarry Smith   Notes:
975c6c1daeSBarry Smith   One can pass 0 to activate all messages that are not associated with an object.
985c6c1daeSBarry Smith 
995c6c1daeSBarry Smith   Level: developer
1005c6c1daeSBarry Smith 
1015c6c1daeSBarry Smith .keywords: allow, information, printing, monitoring
1025c6c1daeSBarry Smith .seealso: PetscInfoDeactivateClass(), PetscInfo(), PetscInfoAllow()
1035c6c1daeSBarry Smith @*/
104fa2bb9feSLisandro Dalcin PetscErrorCode  PetscInfoActivateClass(PetscClassId classid)
1055c6c1daeSBarry Smith {
1065c6c1daeSBarry Smith   PetscFunctionBegin;
107fa2bb9feSLisandro Dalcin   if (!classid) classid = PETSC_SMALLEST_CLASSID;
108fa2bb9feSLisandro Dalcin   PetscInfoFlags[classid - PETSC_SMALLEST_CLASSID] = 1;
1095c6c1daeSBarry Smith   PetscFunctionReturn(0);
1105c6c1daeSBarry Smith }
1115c6c1daeSBarry Smith 
1125c6c1daeSBarry Smith /*
1135c6c1daeSBarry Smith    If the option -history was used, then all printed PetscInfo()
1145c6c1daeSBarry Smith   messages are also printed to the history file, called by default
1155c6c1daeSBarry Smith   .petschistory in ones home directory.
1165c6c1daeSBarry Smith */
117*95c0884eSLisandro Dalcin PETSC_INTERN FILE *petsc_history;
1185c6c1daeSBarry Smith 
1195c6c1daeSBarry Smith /*MC
1205c6c1daeSBarry Smith     PetscInfo - Logs informative data, which is printed to standard output
1215c6c1daeSBarry Smith     or a file when the option -info <file> is specified.
1225c6c1daeSBarry Smith 
1235c6c1daeSBarry Smith    Synopsis:
124aaa7dc30SBarry Smith        #include <petscsys.h>
1255c6c1daeSBarry Smith        PetscErrorCode PetscInfo(void *vobj, const char message[])
1265c6c1daeSBarry Smith        PetscErrorCode PetscInfo1(void *vobj, const char formatmessage[],arg1)
1275c6c1daeSBarry Smith        PetscErrorCode PetscInfo2(void *vobj, const char formatmessage[],arg1,arg2)
1285c6c1daeSBarry Smith        etc
1295c6c1daeSBarry Smith 
1305c6c1daeSBarry Smith     Collective over PetscObject argument
1315c6c1daeSBarry Smith 
1325c6c1daeSBarry Smith     Input Parameter:
1330298fd71SBarry Smith +   vobj - object most closely associated with the logging statement or NULL
1345c6c1daeSBarry Smith .   message - logging message
1355c6c1daeSBarry Smith -   formatmessage - logging message using standard "printf" format
1365c6c1daeSBarry Smith 
1375c6c1daeSBarry Smith     Options Database Key:
1385c6c1daeSBarry Smith $    -info : activates printing of PetscInfo() messages
1395c6c1daeSBarry Smith 
1405c6c1daeSBarry Smith     Level: intermediate
1415c6c1daeSBarry Smith 
1425c6c1daeSBarry Smith     Fortran Note: This function does not take the vobj argument, there is only the PetscInfo()
1435c6c1daeSBarry Smith      version, not PetscInfo1() etc.
1445c6c1daeSBarry Smith 
1455c6c1daeSBarry Smith     Example of Usage:
1465c6c1daeSBarry Smith $
1475c6c1daeSBarry Smith $     Mat A
1485c6c1daeSBarry Smith $     double alpha
1495c6c1daeSBarry Smith $     PetscInfo1(A,"Matrix uses parameter alpha=%g\n",alpha);
1505c6c1daeSBarry Smith $
1515c6c1daeSBarry Smith 
1525c6c1daeSBarry Smith    Concepts: runtime information
1535c6c1daeSBarry Smith 
1545c6c1daeSBarry Smith .seealso: PetscInfoAllow()
1555c6c1daeSBarry Smith M*/
1565c6c1daeSBarry Smith PetscErrorCode  PetscInfo_Private(const char func[],void *vobj, const char message[], ...)
1575c6c1daeSBarry Smith {
1585c6c1daeSBarry Smith   va_list        Argp;
159fa2bb9feSLisandro Dalcin   PetscMPIInt    rank = 0,urank;
1605c6c1daeSBarry Smith   size_t         len;
1615c6c1daeSBarry Smith   PetscObject    obj = (PetscObject)vobj;
162fa2bb9feSLisandro Dalcin   PetscClassId   classid;
1635c6c1daeSBarry Smith   char           string[8*1024];
1645c6c1daeSBarry Smith   PetscErrorCode ierr;
1655c6c1daeSBarry Smith   size_t         fullLength;
1665c6c1daeSBarry Smith   int            err;
1675c6c1daeSBarry Smith 
1685c6c1daeSBarry Smith   PetscFunctionBegin;
169940d77c0SToby Isaac   if (obj) PetscValidHeader(obj,2);
170940d77c0SToby Isaac   PetscValidCharPointer(message,3);
1715c6c1daeSBarry Smith   if (!PetscLogPrintInfo) PetscFunctionReturn(0);
172fa2bb9feSLisandro Dalcin   classid = obj ? obj->classid : PETSC_SMALLEST_CLASSID;
173fa2bb9feSLisandro Dalcin   if (!PetscInfoFlags[classid - PETSC_SMALLEST_CLASSID]) PetscFunctionReturn(0);
174fa2bb9feSLisandro Dalcin   if (obj) {ierr = MPI_Comm_rank(obj->comm, &rank);CHKERRQ(ierr);}
1755c6c1daeSBarry Smith   if (rank) PetscFunctionReturn(0);
1765c6c1daeSBarry Smith 
1775c6c1daeSBarry Smith   ierr = MPI_Comm_rank(MPI_COMM_WORLD, &urank);CHKERRQ(ierr);
1785c6c1daeSBarry Smith   va_start(Argp, message);
1795c6c1daeSBarry Smith   sprintf(string, "[%d] %s(): ", urank,func);
1805c6c1daeSBarry Smith   ierr = PetscStrlen(string, &len);CHKERRQ(ierr);
181a2ea699eSBarry Smith   ierr = PetscVSNPrintf(string+len, 8*1024-len,message,&fullLength, Argp);CHKERRQ(ierr);
1825c6c1daeSBarry Smith   ierr = PetscFPrintf(PETSC_COMM_SELF,PetscInfoFile, "%s", string);CHKERRQ(ierr);
1835c6c1daeSBarry Smith   err  = fflush(PetscInfoFile);
1845c6c1daeSBarry Smith   if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
1855c6c1daeSBarry Smith   if (petsc_history) {
1865c6c1daeSBarry Smith     va_start(Argp, message);
187a2ea699eSBarry Smith     ierr = (*PetscVFPrintf)(petsc_history, message, Argp);CHKERRQ(ierr);
1885c6c1daeSBarry Smith   }
1895c6c1daeSBarry Smith   va_end(Argp);
1905c6c1daeSBarry Smith   PetscFunctionReturn(0);
1915c6c1daeSBarry Smith }
192