forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PythonStdoutChannel.cpp
47 lines (36 loc) · 1.56 KB
/
PythonStdoutChannel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
// local includes
#include "MantidPythonInterface/core/PythonStdoutChannel.h"
// 3rd-party includes
#include "MantidPythonInterface/core/GlobalInterpreterLock.h"
#include "MantidPythonInterface/core/WrapPython.h"
#include <boost/format.hpp>
#include <boost/iostreams/categories.hpp> // sink_tag
#include <boost/iostreams/stream.hpp>
#include <iostream> // streamsize
namespace { // anonymous namespace
class PyStdoutSink {
public:
typedef char char_type;
typedef boost::iostreams::sink_tag category;
std::streamsize write(const char *s, std::streamsize n) {
Mantid::PythonInterface::GlobalInterpreterLock gil; // acquire the GIL
// PySys_WriteStdout truncates to 1000 chars
static const std::streamsize MAXSIZE = 1000;
std::streamsize written = std::min(n, MAXSIZE);
PySys_WriteStdout((boost::format("%%.%1%s") % written).str().c_str(), s);
return written;
} // release the GIL
};
// wrapper of that sink to be a stream
PyStdoutSink pyStdoutSinkInstance = PyStdoutSink(); // needs to be initialized separately
boost::iostreams::stream<PyStdoutSink> PyStdoutStream(pyStdoutSinkInstance);
} // anonymous namespace
namespace Poco {
PythonStdoutChannel::PythonStdoutChannel() : ConsoleChannel(PyStdoutStream) {}
} // namespace Poco