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