System Preprocessors
tracing.c
Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include <string.h>
00003 #include <stdarg.h>
00004 #include "syspro.h"
00005 
00006 /*! \page tracing Tracing the preprocessors
00007   The SysPro package does not by default print out anything, other than
00008   severe error messages (Petsc macro SETERRQ) that accompany an abort.
00009 
00010   However, you can specify a trace function, which
00011   can further be tuned by specifying a trace context.
00012 
00013   See SysProDeclareTraceFunction(), SysProDeclareTraceContext(),
00014   SysProTraceMessage().
00015 */
00016 
00017 static PetscErrorCode(*sysprotrace)(void*,const char *fmt,va_list) = NULL;
00018 static size_t sysprotracectx = (size_t)NULL;
00019 
00020 #undef __FUNCT__
00021 #define __FUNCT__ "SysProDefaultTrace"
00022 PetscErrorCode SysProDefaultTrace(void *ctx,const char *fmt,va_list argp)
00023 {
00024   /*char *prefix = (char*)ctx;*/
00025   PetscFunctionBegin;
00026   printf("SysPro message: ");
00027   vprintf(fmt, argp);
00028   PetscFunctionReturn(0);
00029 }
00030 
00031 #undef __FUNCT__
00032 #define __FUNCT__ "SysProDeclareTraceFunction"
00033 /*! Specify a trace function.
00034 
00035   The trace function has a prototype 
00036 \verbatim
00037   PetscErrorCode tracefunction(void*,char*,va_list)
00038 \endverbatim
00039   which means that it has an arbitrary number of arguments, much like
00040   \c printf. The first argument is a context, which can be set by 
00041   SysProDeclareTraceContext().
00042 
00043   Here is an example of how you would write a trace function:
00044 \verbatim
00045 #include <stdarg.h>
00046 PetscErrorCode tracefunction(void *ctx,char *fmt,va_list argp)
00047 {
00048   char *prefix = (char*)ctx;
00049   PetscFunctionBegin;
00050   printf("%s ",prefix);
00051   vprintf(fmt, argp);
00052   PetscFunctionReturn(0);
00053 }
00054 \endverbatim
00055   Consult \c string.h (probably in \c /usr/include) to see which "v"
00056   versions of \c printf are available.
00057 
00058   There is a default trace function SysProDefaultTrace().
00059 
00060   You can undeclare a trace function by passing NULL.
00061 
00062   See also SysProTraceMessage().
00063 */
00064 PetscErrorCode SysProDeclareTraceFunction(PetscErrorCode(*fn)(void*,const char*,va_list))
00065 {
00066   PetscFunctionBegin;
00067   sysprotrace = fn;
00068   PetscFunctionReturn(0);
00069 }
00070 
00071 #undef __FUNCT__
00072 #define __FUNCT__ "SysProDeclareTraceContext"
00073 /* Specify a trace context.
00074   
00075   See SysProDeclareTraceFunction().
00076 */
00077 PetscErrorCode SysProDeclareTraceContext(void *ctx)
00078 {
00079   PetscFunctionBegin;
00080   sysprotracectx = (size_t)ctx;
00081   PetscFunctionReturn(0);
00082 }
00083 
00084 #undef __FUNCT__
00085 #define __FUNCT__ "SysProTraceMessage"
00086 /*! This function prints a trace message if a trace function has been
00087   declared; see SysProDeclareTraceFunction().
00088 */
00089 PetscErrorCode SysProTraceMessage(const char *fmt,...)
00090 {
00091   va_list argp; PetscErrorCode ierr;
00092   PetscFunctionBegin;
00093   if (sysprotrace) {
00094     va_start(argp, fmt);
00095     ierr = (*sysprotrace)((void*)sysprotracectx,fmt,argp); CHKERRQ(ierr);
00096     va_end(argp);
00097   }
00098   PetscFunctionReturn(0);
00099 }
00100 
00101 #undef __FUNCT__
00102 #define __FUNCT__ "SysProHasTrace"
00103 /*! Test whether a trace function has been declared; 
00104   see SysProDeclareTraceFunction(). Normally you would use SysProTraceMessage()
00105   which performs this test internally,
00106   but this function can be useful if a large amount of processing
00107   has to be performed to construct the trace message to begin with.
00108 */
00109 PetscErrorCode SysProHasTrace(PetscTruth *flg)
00110 {
00111   PetscFunctionBegin;
00112   if (sysprotrace)
00113     *flg = PETSC_TRUE;
00114   else
00115     *flg = PETSC_FALSE;
00116   PetscFunctionReturn(0);
00117 }
00118