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

#include <iostream>

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

#include <map>
#include <dynamic-graph/pool.h>
#include <dynamic-graph/entity.h>
#include <vector>
14
#include "dynamic-graph/python/exception.hh"
15
16

#include <boost/shared_ptr.hpp>
17
#include "dynamic-graph/python/dynamic-graph-py.hh"
18
19
20
21

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

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

24
#if PY_MAJOR_VERSION == 2
Guilhem Saurel's avatar
Format    
Guilhem Saurel committed
25
26
extern PyObject* dgpyError;
#endif
27

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
28
namespace debug {
29

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

Guilhem Saurel's avatar
Guilhem Saurel committed
32
33
34
35
36
37
PyObject* addLoggerFileOutputStream(
#if PY_MAJOR_VERSION >= 3
    PyObject* m, PyObject* args
#else
    PyObject*, PyObject* args
#endif
Guilhem Saurel's avatar
Format    
Guilhem Saurel committed
38
) {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
39
40
41
42
43
44
45
46
47
48
49
50
  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;
  }
51
  CATCH_ALL_EXCEPTIONS(m);
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
52
53
  return Py_BuildValue("");
}
54

Guilhem Saurel's avatar
Guilhem Saurel committed
55
56
57
58
59
60
PyObject* closeLoggerFileOutputStream(
#if PY_MAJOR_VERSION >= 3
    PyObject* m, PyObject*
#else
    PyObject*, PyObject*
#endif
Guilhem Saurel's avatar
Format    
Guilhem Saurel committed
61
) {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
62
63
64
65
66
  try {
    for (std::map<std::string, ofstreamShrPtr>::iterator it = mapOfFiles_.begin(); it != mapOfFiles_.end(); ++it) {
      it->second->close();
    }
  }
67
  CATCH_ALL_EXCEPTIONS(m);
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
68
69
  return Py_BuildValue("");
}
70

Guilhem Saurel's avatar
Guilhem Saurel committed
71
72
73
74
75
76
PyObject* addLoggerCoutOutputStream(
#if PY_MAJOR_VERSION >= 3
    PyObject* m, PyObject*
#else
    PyObject*, PyObject*
#endif
Guilhem Saurel's avatar
Format    
Guilhem Saurel committed
77
) {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
78
79
80
  try {
    dgADD_OSTREAM_TO_RTLOG(std::cout);
  }
81
  CATCH_ALL_EXCEPTIONS(m);
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
82
83
  return Py_BuildValue("");
}
84

Guilhem Saurel's avatar
Guilhem Saurel committed
85
86
87
88
89
90
PyObject* realTimeLoggerDestroy(
#if PY_MAJOR_VERSION >= 3
    PyObject* m, PyObject*
#else
    PyObject*, PyObject*
#endif
Guilhem Saurel's avatar
Format    
Guilhem Saurel committed
91
) {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
92
93
94
  try {
    RealTimeLogger::destroy();
  }
95
  CATCH_ALL_EXCEPTIONS(m);
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
96
97
  return Py_BuildValue("");
}
98

Guilhem Saurel's avatar
Guilhem Saurel committed
99
100
101
102
103
104
PyObject* realTimeLoggerSpinOnce(
#if PY_MAJOR_VERSION >= 3
    PyObject* m, PyObject*
#else
    PyObject*, PyObject*
#endif
Guilhem Saurel's avatar
Format    
Guilhem Saurel committed
105
) {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
106
107
108
  try {
    RealTimeLogger::instance().spinOnce();
  }
109
  CATCH_ALL_EXCEPTIONS(m);
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
110
111
  return Py_BuildValue("");
}
112

Guilhem Saurel's avatar
Guilhem Saurel committed
113
114
115
116
117
118
PyObject* realTimeLoggerInstance(
#if PY_MAJOR_VERSION >= 3
    PyObject* m, PyObject*
#else
    PyObject*, PyObject*
#endif
Guilhem Saurel's avatar
Format    
Guilhem Saurel committed
119
) {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
120
121
122
  try {
    RealTimeLogger::instance();
  }
123
  CATCH_ALL_EXCEPTIONS(m);
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
124
125
  return Py_BuildValue("");
}
126

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
127
128
129
}  // namespace debug
}  // namespace python
}  // namespace dynamicgraph