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 "NamTracer.h"
00035 #include "simlpy/interpreter_hooks.h"
00036 #include "simlpy/Clock.h"
00037
00038 #include <iostream>
00039 #include <fstream>
00040
00055 #ifndef PyMODINIT_FUNC
00058 #define PyMODINIT_FUNC extern "C" void
00059 #endif
00060
00062 static NamTracer* nam_tracer = 0;
00063
00067 static char namtrace_setup_doc[] = "NAM tracing configuration.\n\
00068 \n\
00069 This method should be called before creating any nodes if\n\
00070 nam tracing is desired.\n\
00071 \n\
00072 Calling syntax:\n\
00073 \n\
00074 pydtn.namtrace.setup(<fname>)\n\
00075 \n\
00076 <fname> is either a filename or one of 'stdout' or 'stderr'.\n\
00077 In the former case, the nam tracing information is written\n\
00078 to the file named. In the latter cases, the information is\n\
00079 written to the appropriate stream.\n";
00080
00087 static PyObject*
00088 namtrace_setup( PyObject* self, PyObject* args )
00089 {
00090 if ( 0 != nam_tracer )
00091 {
00092 std::cerr << "nam tracing already enabled" << std::endl;
00093 return 0;
00094 }
00095
00096 const char* fname;
00097 if ( ! PyArg_ParseTuple(args, "s", &fname) )
00098 {
00099 std::cerr << "Incorrect argument list" << std::endl;
00100 return 0;
00101 }
00102 std::string fnstr( fname );
00103 GlobalTracer::enable();
00104 GlobalTracer* gt = GlobalTracer::instance();
00105 if ( 0 == gt )
00106 {
00107 std::cerr << "Could not instantiate the global tracer" << std::endl;
00108 return 0;
00109 }
00110
00111 if ( "stdout" == fnstr )
00112 {
00113 nam_tracer = new NamTracer( std::cout );
00114 }
00115 else if ( "stderr" == fnstr )
00116 {
00117 nam_tracer = new NamTracer( std::cerr );
00118 }
00119 else
00120 {
00121 nam_tracer = new NamTracer( *(new std::ofstream(fname)) );
00122 }
00123 gt->addTracer(nam_tracer);
00124
00125 Py_INCREF(Py_None);
00126 return Py_None;
00127 }
00128
00132 static char namtrace_queue_doc[] = "Configure the queue tracing behavior.\n\
00133 \n\
00134 Calling syntax:\n\
00135 \n\
00136 pydtn.namtrace.queue(<state>)\n\
00137 \n\
00138 <state> is either True or False, and determines whether or\n\
00139 not queue information should be included in the trace.\n";
00140
00147 static PyObject*
00148 namtrace_queue( PyObject* self, PyObject* args )
00149 {
00150 if ( 0 == nam_tracer )
00151 {
00152 std::cerr << "nam tracing must first be enabled" << std::endl;
00153 return 0;
00154 }
00155
00156 bool q;
00157 if ( ! PyArg_ParseTuple(args, "b", &q) )
00158 {
00159 std::cerr << "Incorrect argument list" << std::endl;
00160 return 0;
00161 }
00162 nam_tracer->setQueueState(q);
00163
00164 Py_INCREF(Py_None);
00165 return Py_None;
00166 }
00167
00170 static PyMethodDef ExampleMethods[] =
00171 {
00172 {"setup",namtrace_setup,METH_VARARGS,namtrace_setup_doc},
00173 {"queue",namtrace_queue,METH_VARARGS,namtrace_queue_doc},
00174 {0,0,0,0}
00175 };
00176
00180 PyMODINIT_FUNC
00181 init_namtrace(void)
00182 {
00183 (void) Py_InitModule("_namtrace",ExampleMethods);
00184 }
00185