python_init.cc

00001 // Copyright 2008 Michael Marsh, University of Maryland.
00002 //
00003 // This file is part of pydtn.
00004 //
00005 // pydtn is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // pydtn is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with pydtn.  If not, see <http://www.gnu.org/licenses/>.
00017 //
00018 // The views and conclusions contained in the software and documentation
00019 // are those of the authors and should not be interpreted as representing
00020 // official policies, either expressed or implied, of the University
00021 // of Maryland.
00022 //
00023 // pydtn extends and embeds the Python interpreter, which is
00024 // Copyright 2001-2006 Python Software Foundation, All Rights Reserved,
00025 // and is released under the PSF License Agreement.
00026 //
00027 // RANLUX random number generation uses the Boost library,
00028 // Copyright 1994-2006 by various authors (details in individual files),
00029 // which is released under the Boost Software License, Version 1.0.
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 ); // This lets us use operator==.
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} // sentinel
00183 };
00184 
00188 PyMODINIT_FUNC
00189 init_flowtrace(void)
00190 {
00191    (void) Py_InitModule("_flowtrace",ExampleMethods);
00192 }
00193 

Generated on Mon Mar 24 11:15:45 2008 for Pydtn Simulator by  doxygen 1.5.4