00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "Python.h"
00032 #include "structmember.h"
00033
00034 #include "FlowTracer.h"
00035 #include "simlpy/interpreter_hooks.h"
00036 #include "simlpy/Clock.h"
00037
00038 #include <iostream>
00039 #include <fstream>
00040
00098 #ifndef PyMODINIT_FUNC
00101 #define PyMODINIT_FUNC extern "C" void
00102 #endif
00103
00105 static FlowTracer* flow_tracer = 0;
00106
00110 static char flowtrace_setup_doc[] = "Flow tracing configuration.\n\
00111 \n\
00112 This method should be called before creating any nodes if\n\
00113 flow tracing is desired.\n\
00114 \n\
00115 Calling syntax:\n\
00116 \n\
00117 pydtn.flowtrace.setup(<fname>)\n\
00118 \n\
00119 <fname> is either a filename or one of 'stdout' or 'stderr'.\n\
00120 In the former case, the flow tracing information is written\n\
00121 to the file named. In the latter cases, the information is\n\
00122 written to the appropriate stream.\n";
00123
00135 static PyObject*
00136 flowtrace_setup( PyObject* self, PyObject* args )
00137 {
00138 if ( 0 != flow_tracer )
00139 {
00140 std::cerr << "flow tracing already enabled" << std::endl;
00141 return 0;
00142 }
00143
00144 const char* fname;
00145 if ( ! PyArg_ParseTuple(args, "s", &fname) )
00146 {
00147 std::cerr << "Incorrect argument list" << std::endl;
00148 return 0;
00149 }
00150 std::string fnstr( fname );
00151 GlobalTracer::enable();
00152 GlobalTracer* gt = GlobalTracer::instance();
00153 if ( 0 == gt )
00154 {
00155 std::cerr << "Could not instantiate the global tracer" << std::endl;
00156 return 0;
00157 }
00158
00159 if ( "stdout" == fnstr )
00160 {
00161 flow_tracer = new FlowTracer( std::cout );
00162 }
00163 else if ( "stderr" == fnstr )
00164 {
00165 flow_tracer = new FlowTracer( std::cerr );
00166 }
00167 else
00168 {
00169 flow_tracer = new FlowTracer( *(new std::ofstream(fname)) );
00170 }
00171 gt->addTracer(flow_tracer);
00172
00173 Py_INCREF(Py_None);
00174 return Py_None;
00175 }
00176
00179 static PyMethodDef ExampleMethods[] =
00180 {
00181 {"setup",flowtrace_setup,METH_VARARGS,flowtrace_setup_doc},
00182 {0,0,0,0}
00183 };
00184
00188 PyMODINIT_FUNC
00189 init_flowtrace(void)
00190 {
00191 (void) Py_InitModule("_flowtrace",ExampleMethods);
00192 }
00193