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 "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 ); // This lets us use operator==.
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} // sentinel
00175 };
00176 
00180 PyMODINIT_FUNC
00181 init_namtrace(void)
00182 {
00183    (void) Py_InitModule("_namtrace",ExampleMethods);
00184 }
00185 

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