debug-py.cc 2.19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright 2019, Olivier Stasse, LAAS-CNRS.
//
// See LICENSE

#include <iostream>

#define ENABLE_RT_LOG
#include <dynamic-graph/real-time-logger.h>

#include <map>
#include <Python.h>
#include <dynamic-graph/pool.h>
#include <dynamic-graph/entity.h>
#include <vector>
#include "exception.hh"

#include <boost/shared_ptr.hpp>
Guilhem Saurel's avatar
Guilhem Saurel committed
18
#include "dynamic-graph-py.hh"
19 20 21 22

typedef boost::shared_ptr<std::ofstream> ofstreamShrPtr;

namespace dynamicgraph {
Guilhem Saurel's avatar
format  
Guilhem Saurel committed
23
namespace python {
24

Guilhem Saurel's avatar
format  
Guilhem Saurel committed
25
namespace debug {
26

Guilhem Saurel's avatar
format  
Guilhem Saurel committed
27
std::map<std::string, ofstreamShrPtr> mapOfFiles_;
28

Guilhem Saurel's avatar
format  
Guilhem Saurel committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
PyObject* addLoggerFileOutputStream(PyObject* /*self*/, PyObject* args) {
  char* filename;
  if (!PyArg_ParseTuple(args, "s", &filename)) return NULL;
  std::string sfilename(filename);
  try {
    std::ofstream* aofs = new std::ofstream;
    ofstreamShrPtr ofs_shrptr = boost::shared_ptr<std::ofstream>(aofs);
    aofs->open(filename, std::ofstream::out);
    dynamicgraph::RealTimeLogger::instance();
    dgADD_OSTREAM_TO_RTLOG(*aofs);
    dgRTLOG() << "Added " << filename << " as an output stream \n";
    mapOfFiles_[sfilename] = ofs_shrptr;
  }
  CATCH_ALL_EXCEPTIONS();
  return Py_BuildValue("");
}
45

Guilhem Saurel's avatar
format  
Guilhem Saurel committed
46 47 48 49 50 51 52 53 54
PyObject* closeLoggerFileOutputStream(PyObject* /*self*/, PyObject* /*args */) {
  try {
    for (std::map<std::string, ofstreamShrPtr>::iterator it = mapOfFiles_.begin(); it != mapOfFiles_.end(); ++it) {
      it->second->close();
    }
  }
  CATCH_ALL_EXCEPTIONS();
  return Py_BuildValue("");
}
55

Guilhem Saurel's avatar
format  
Guilhem Saurel committed
56 57 58 59 60 61 62
PyObject* addLoggerCoutOutputStream(PyObject* /*self*/, PyObject* /*args*/) {
  try {
    dgADD_OSTREAM_TO_RTLOG(std::cout);
  }
  CATCH_ALL_EXCEPTIONS();
  return Py_BuildValue("");
}
63

Guilhem Saurel's avatar
format  
Guilhem Saurel committed
64 65 66 67 68 69 70
PyObject* realTimeLoggerDestroy(PyObject* /*self*/, PyObject* /*args*/) {
  try {
    RealTimeLogger::destroy();
  }
  CATCH_ALL_EXCEPTIONS();
  return Py_BuildValue("");
}
71

Guilhem Saurel's avatar
format  
Guilhem Saurel committed
72 73 74 75 76 77 78
PyObject* realTimeLoggerSpinOnce(PyObject* /*self*/, PyObject* /*args*/) {
  try {
    RealTimeLogger::instance().spinOnce();
  }
  CATCH_ALL_EXCEPTIONS();
  return Py_BuildValue("");
}
79

Guilhem Saurel's avatar
format  
Guilhem Saurel committed
80 81 82 83 84 85 86
PyObject* realTimeLoggerInstance(PyObject* /*self*/, PyObject* /*args*/) {
  try {
    RealTimeLogger::instance();
  }
  CATCH_ALL_EXCEPTIONS();
  return Py_BuildValue("");
}
87

Guilhem Saurel's avatar
format  
Guilhem Saurel committed
88 89 90
}  // namespace debug
}  // namespace python
}  // namespace dynamicgraph