xref: /petsc/src/sys/info/verboseinfo.c (revision 0298fd7132830bec7daee99a80be0eddb2b310a5)
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 */
65c6c1daeSBarry Smith #include <petscsys.h>        /*I    "petscsys.h"   I*/
75c6c1daeSBarry Smith #include <stdarg.h>
85c6c1daeSBarry Smith #include <sys/types.h>
95c6c1daeSBarry Smith #if defined(PETSC_HAVE_STDLIB_H)
105c6c1daeSBarry Smith #include <stdlib.h>
115c6c1daeSBarry Smith #endif
125c6c1daeSBarry Smith #if defined(PETSC_HAVE_MALLOC_H)
135c6c1daeSBarry Smith #include <malloc.h>
145c6c1daeSBarry Smith #endif
155c6c1daeSBarry Smith 
165c6c1daeSBarry Smith /*
175c6c1daeSBarry Smith   The next three variables determine which, if any, PetscInfo() calls are used.
185c6c1daeSBarry Smith   If PetscLogPrintInfo is zero, no info messages are printed.
195c6c1daeSBarry Smith   If PetscLogPrintInfoNull is zero, no info messages associated with a null object are printed.
205c6c1daeSBarry Smith 
215c6c1daeSBarry Smith   If PetscInfoFlags[OBJECT_CLASSID - PETSC_SMALLEST_CLASSID] is zero, no messages related
225c6c1daeSBarry Smith   to that object are printed. OBJECT_CLASSID is, for example, MAT_CLASSID.
235c6c1daeSBarry Smith */
245c6c1daeSBarry Smith PetscBool PetscLogPrintInfo     = PETSC_FALSE;
255c6c1daeSBarry Smith PetscBool PetscLogPrintInfoNull = PETSC_FALSE;
265c6c1daeSBarry Smith 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,
275c6c1daeSBarry Smith                                    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
285c6c1daeSBarry Smith                                    1,1,1,1,1,1,1,1,1,1,1,1};
29*0298fd71SBarry Smith FILE      *PetscInfoFile = NULL;
305c6c1daeSBarry Smith 
315c6c1daeSBarry Smith #undef __FUNCT__
325c6c1daeSBarry Smith #define __FUNCT__ "PetscInfoAllow"
335c6c1daeSBarry Smith /*@C
345c6c1daeSBarry Smith     PetscInfoAllow - Causes PetscInfo() messages to be printed to standard output.
355c6c1daeSBarry Smith 
365c6c1daeSBarry Smith     Not Collective, each processor may call this separately, but printing is only
375c6c1daeSBarry Smith     turned on if the lowest processor number associated with the PetscObject associated
385c6c1daeSBarry Smith     with the call to PetscInfo() has called this routine.
395c6c1daeSBarry Smith 
405c6c1daeSBarry Smith     Input Parameter:
415c6c1daeSBarry Smith +   flag - PETSC_TRUE or PETSC_FALSE
425c6c1daeSBarry Smith -   filename - optional name of file to write output to (defaults to stdout)
435c6c1daeSBarry Smith 
445c6c1daeSBarry Smith     Options Database Key:
455c6c1daeSBarry Smith .   -info [optional filename] - Activates PetscInfoAllow()
465c6c1daeSBarry Smith 
475c6c1daeSBarry Smith     Level: advanced
485c6c1daeSBarry Smith 
495c6c1daeSBarry Smith    Concepts: debugging^detailed runtime information
505c6c1daeSBarry Smith    Concepts: dumping detailed runtime information
515c6c1daeSBarry Smith 
525c6c1daeSBarry Smith .seealso: PetscInfo()
535c6c1daeSBarry Smith @*/
545c6c1daeSBarry Smith PetscErrorCode  PetscInfoAllow(PetscBool flag, const char filename[])
555c6c1daeSBarry Smith {
565c6c1daeSBarry Smith   char           fname[PETSC_MAX_PATH_LEN], tname[5];
575c6c1daeSBarry Smith   PetscMPIInt    rank;
585c6c1daeSBarry Smith   PetscErrorCode ierr;
595c6c1daeSBarry Smith 
605c6c1daeSBarry Smith   PetscFunctionBegin;
615c6c1daeSBarry Smith   if (flag && filename) {
625c6c1daeSBarry Smith     ierr = PetscFixFilename(filename, fname);CHKERRQ(ierr);
635c6c1daeSBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD, &rank);CHKERRQ(ierr);
645c6c1daeSBarry Smith     sprintf(tname, ".%d", rank);
655c6c1daeSBarry Smith     ierr = PetscStrcat(fname, tname);CHKERRQ(ierr);
665c6c1daeSBarry Smith     ierr = PetscFOpen(MPI_COMM_SELF, fname, "w", &PetscInfoFile);CHKERRQ(ierr);
675c6c1daeSBarry Smith     if (!PetscInfoFile) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN, "Cannot open requested file for writing: %s",fname);
68a297a907SKarl Rupp   } else if (flag) PetscInfoFile = PETSC_STDOUT;
69a297a907SKarl Rupp 
705c6c1daeSBarry Smith   PetscLogPrintInfo     = flag;
715c6c1daeSBarry Smith   PetscLogPrintInfoNull = flag;
725c6c1daeSBarry Smith   PetscFunctionReturn(0);
735c6c1daeSBarry Smith }
745c6c1daeSBarry Smith 
755c6c1daeSBarry Smith #undef __FUNCT__
765c6c1daeSBarry Smith #define __FUNCT__ "PetscInfoDeactivateClass"
775c6c1daeSBarry Smith /*@
785c6c1daeSBarry Smith   PetscInfoDeactivateClass - Deactivates PlogInfo() messages for a PETSc object class.
795c6c1daeSBarry Smith 
805c6c1daeSBarry Smith   Not Collective
815c6c1daeSBarry Smith 
825c6c1daeSBarry Smith   Input Parameter:
835c6c1daeSBarry Smith . objclass - The object class,  e.g., MAT_CLASSID, SNES_CLASSID, etc.
845c6c1daeSBarry Smith 
855c6c1daeSBarry Smith   Notes:
865c6c1daeSBarry Smith   One can pass 0 to deactivate all messages that are not associated with an object.
875c6c1daeSBarry Smith 
885c6c1daeSBarry Smith   Level: developer
895c6c1daeSBarry Smith 
905c6c1daeSBarry Smith .keywords: allow, information, printing, monitoring
915c6c1daeSBarry Smith .seealso: PetscInfoActivateClass(), PetscInfo(), PetscInfoAllow()
925c6c1daeSBarry Smith @*/
935c6c1daeSBarry Smith PetscErrorCode  PetscInfoDeactivateClass(int objclass)
945c6c1daeSBarry Smith {
955c6c1daeSBarry Smith   PetscFunctionBegin;
965c6c1daeSBarry Smith   if (!objclass) {
975c6c1daeSBarry Smith     PetscLogPrintInfoNull = PETSC_FALSE;
985c6c1daeSBarry Smith     PetscFunctionReturn(0);
995c6c1daeSBarry Smith   }
1005c6c1daeSBarry Smith   PetscInfoFlags[objclass - PETSC_SMALLEST_CLASSID - 1] = 0;
1015c6c1daeSBarry Smith   PetscFunctionReturn(0);
1025c6c1daeSBarry Smith }
1035c6c1daeSBarry Smith 
1045c6c1daeSBarry Smith #undef __FUNCT__
1055c6c1daeSBarry Smith #define __FUNCT__ "PetscInfoActivateClass"
1065c6c1daeSBarry Smith /*@
1075c6c1daeSBarry Smith   PetscInfoActivateClass - Activates PlogInfo() messages for a PETSc object class.
1085c6c1daeSBarry Smith 
1095c6c1daeSBarry Smith   Not Collective
1105c6c1daeSBarry Smith 
1115c6c1daeSBarry Smith   Input Parameter:
1125c6c1daeSBarry Smith . objclass - The object class, e.g., MAT_CLASSID, SNES_CLASSID, etc.
1135c6c1daeSBarry Smith 
1145c6c1daeSBarry Smith   Notes:
1155c6c1daeSBarry Smith   One can pass 0 to activate all messages that are not associated with an object.
1165c6c1daeSBarry Smith 
1175c6c1daeSBarry Smith   Level: developer
1185c6c1daeSBarry Smith 
1195c6c1daeSBarry Smith .keywords: allow, information, printing, monitoring
1205c6c1daeSBarry Smith .seealso: PetscInfoDeactivateClass(), PetscInfo(), PetscInfoAllow()
1215c6c1daeSBarry Smith @*/
1225c6c1daeSBarry Smith PetscErrorCode  PetscInfoActivateClass(int objclass)
1235c6c1daeSBarry Smith {
1245c6c1daeSBarry Smith   PetscFunctionBegin;
125a297a907SKarl Rupp   if (!objclass) PetscLogPrintInfoNull = PETSC_TRUE;
126a297a907SKarl Rupp   else PetscInfoFlags[objclass - PETSC_SMALLEST_CLASSID - 1] = 1;
1275c6c1daeSBarry Smith   PetscFunctionReturn(0);
1285c6c1daeSBarry Smith }
1295c6c1daeSBarry Smith 
1305c6c1daeSBarry Smith /*
1315c6c1daeSBarry Smith    If the option -history was used, then all printed PetscInfo()
1325c6c1daeSBarry Smith   messages are also printed to the history file, called by default
1335c6c1daeSBarry Smith   .petschistory in ones home directory.
1345c6c1daeSBarry Smith */
1355c6c1daeSBarry Smith extern FILE *petsc_history;
1365c6c1daeSBarry Smith 
1375c6c1daeSBarry Smith #undef __FUNCT__
1385c6c1daeSBarry Smith #define __FUNCT__ "PetscInfo_Private"
1395c6c1daeSBarry Smith /*MC
1405c6c1daeSBarry Smith     PetscInfo - Logs informative data, which is printed to standard output
1415c6c1daeSBarry Smith     or a file when the option -info <file> is specified.
1425c6c1daeSBarry Smith 
1435c6c1daeSBarry Smith    Synopsis:
144f2ba6396SBarry Smith        #include "petscsys.h"
1455c6c1daeSBarry Smith        PetscErrorCode PetscInfo(void *vobj, const char message[])
1465c6c1daeSBarry Smith        PetscErrorCode PetscInfo1(void *vobj, const char formatmessage[],arg1)
1475c6c1daeSBarry Smith        PetscErrorCode PetscInfo2(void *vobj, const char formatmessage[],arg1,arg2)
1485c6c1daeSBarry Smith        etc
1495c6c1daeSBarry Smith 
1505c6c1daeSBarry Smith     Collective over PetscObject argument
1515c6c1daeSBarry Smith 
1525c6c1daeSBarry Smith     Input Parameter:
153*0298fd71SBarry Smith +   vobj - object most closely associated with the logging statement or NULL
1545c6c1daeSBarry Smith .   message - logging message
1555c6c1daeSBarry Smith -   formatmessage - logging message using standard "printf" format
1565c6c1daeSBarry Smith 
1575c6c1daeSBarry Smith     Options Database Key:
1585c6c1daeSBarry Smith $    -info : activates printing of PetscInfo() messages
1595c6c1daeSBarry Smith 
1605c6c1daeSBarry Smith     Level: intermediate
1615c6c1daeSBarry Smith 
1625c6c1daeSBarry Smith     Fortran Note: This function does not take the vobj argument, there is only the PetscInfo()
1635c6c1daeSBarry Smith      version, not PetscInfo1() etc.
1645c6c1daeSBarry Smith 
1655c6c1daeSBarry Smith     Example of Usage:
1665c6c1daeSBarry Smith $
1675c6c1daeSBarry Smith $     Mat A
1685c6c1daeSBarry Smith $     double alpha
1695c6c1daeSBarry Smith $     PetscInfo1(A,"Matrix uses parameter alpha=%g\n",alpha);
1705c6c1daeSBarry Smith $
1715c6c1daeSBarry Smith 
1725c6c1daeSBarry Smith    Concepts: runtime information
1735c6c1daeSBarry Smith 
1745c6c1daeSBarry Smith .seealso: PetscInfoAllow()
1755c6c1daeSBarry Smith M*/
1765c6c1daeSBarry Smith PetscErrorCode  PetscInfo_Private(const char func[],void *vobj, const char message[], ...)
1775c6c1daeSBarry Smith {
1785c6c1daeSBarry Smith   va_list        Argp;
1795c6c1daeSBarry Smith   PetscMPIInt    rank,urank;
1805c6c1daeSBarry Smith   size_t         len;
1815c6c1daeSBarry Smith   PetscObject    obj = (PetscObject)vobj;
1825c6c1daeSBarry Smith   char           string[8*1024];
1835c6c1daeSBarry Smith   PetscErrorCode ierr;
1845c6c1daeSBarry Smith   size_t         fullLength;
1855c6c1daeSBarry Smith   int            err;
1865c6c1daeSBarry Smith 
1875c6c1daeSBarry Smith   PetscFunctionBegin;
1885c6c1daeSBarry Smith   if (obj) PetscValidHeader(obj,1);
1895c6c1daeSBarry Smith   PetscValidCharPointer(message,2);
1905c6c1daeSBarry Smith   if (!PetscLogPrintInfo) PetscFunctionReturn(0);
1915c6c1daeSBarry Smith   if ((!PetscLogPrintInfoNull) && !vobj) PetscFunctionReturn(0);
1925c6c1daeSBarry Smith   if (obj && !PetscInfoFlags[obj->classid - PETSC_SMALLEST_CLASSID - 1]) PetscFunctionReturn(0);
193a297a907SKarl Rupp   if (!obj) rank = 0;
194a297a907SKarl Rupp   else {
1955c6c1daeSBarry Smith     ierr = MPI_Comm_rank(obj->comm, &rank);CHKERRQ(ierr);
1965c6c1daeSBarry Smith   }
1975c6c1daeSBarry Smith   if (rank) PetscFunctionReturn(0);
1985c6c1daeSBarry Smith 
1995c6c1daeSBarry Smith   ierr = MPI_Comm_rank(MPI_COMM_WORLD, &urank);CHKERRQ(ierr);
2005c6c1daeSBarry Smith   va_start(Argp, message);
2015c6c1daeSBarry Smith   sprintf(string, "[%d] %s(): ", urank,func);
2025c6c1daeSBarry Smith   ierr = PetscStrlen(string, &len);CHKERRQ(ierr);
203a2ea699eSBarry Smith   ierr = PetscVSNPrintf(string+len, 8*1024-len,message,&fullLength, Argp);CHKERRQ(ierr);
2045c6c1daeSBarry Smith   ierr = PetscFPrintf(PETSC_COMM_SELF,PetscInfoFile, "%s", string);CHKERRQ(ierr);
2055c6c1daeSBarry Smith   err  = fflush(PetscInfoFile);
2065c6c1daeSBarry Smith   if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
2075c6c1daeSBarry Smith   if (petsc_history) {
2085c6c1daeSBarry Smith     va_start(Argp, message);
209a2ea699eSBarry Smith     ierr = (*PetscVFPrintf)(petsc_history, message, Argp);CHKERRQ(ierr);
2105c6c1daeSBarry Smith   }
2115c6c1daeSBarry Smith   va_end(Argp);
2125c6c1daeSBarry Smith   PetscFunctionReturn(0);
2135c6c1daeSBarry Smith }
214