| 1 | /*
|
|---|
| 2 | * Project: MoleCuilder
|
|---|
| 3 | * Description: creates and alters molecular systems
|
|---|
| 4 | * Copyright (C) 2013 Frederik Heber. All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | *
|
|---|
| 7 | * This file is part of MoleCuilder.
|
|---|
| 8 | *
|
|---|
| 9 | * MoleCuilder is free software: you can redistribute it and/or modify
|
|---|
| 10 | * it under the terms of the GNU General Public License as published by
|
|---|
| 11 | * the Free Software Foundation, either version 2 of the License, or
|
|---|
| 12 | * (at your option) any later version.
|
|---|
| 13 | *
|
|---|
| 14 | * MoleCuilder is distributed in the hope that it will be useful,
|
|---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 17 | * GNU General Public License for more details.
|
|---|
| 18 | *
|
|---|
| 19 | * You should have received a copy of the GNU General Public License
|
|---|
| 20 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 21 | */
|
|---|
| 22 |
|
|---|
| 23 | /* helper_c_capture.cpp
|
|---|
| 24 | *
|
|---|
| 25 | * This is inspired by the answer on stackoverflow
|
|---|
| 26 | * https://stackoverflow.com/questions/35745541/how-to-get-printed-output-from-ctypes-c-functions-into-jupyter-ipython-notebook
|
|---|
| 27 | * where certain libc functions
|
|---|
| 28 | * are exposed using a python module (with ctypes). This is however very hacky
|
|---|
| 29 | * and as I am using a C++ module anyway, I rather expose these functions cleanly
|
|---|
| 30 | * through an extra library.
|
|---|
| 31 | *
|
|---|
| 32 | * Note:
|
|---|
| 33 | * It was not straight-forward to expose _IO_FILE to boost::python. Hence,
|
|---|
| 34 | * I simply crippled the exposed function to have not arguments and work on
|
|---|
| 35 | * stdout directly.
|
|---|
| 36 | *
|
|---|
| 37 | * See \b create_c_contextmanager.py
|
|---|
| 38 | *
|
|---|
| 39 | * Created on: Apr 15, 2019
|
|---|
| 40 | * Author: heber
|
|---|
| 41 | */
|
|---|
| 42 |
|
|---|
| 43 | // include config.h
|
|---|
| 44 | #ifdef HAVE_CONFIG_H
|
|---|
| 45 | #include <config.h>
|
|---|
| 46 | #endif
|
|---|
| 47 |
|
|---|
| 48 | //#include "CodePatterns/MemDebug.hpp"
|
|---|
| 49 |
|
|---|
| 50 | #include <boost/python.hpp>
|
|---|
| 51 |
|
|---|
| 52 | #include <cstdio>
|
|---|
| 53 | #include <ext/stdio_filebuf.h>
|
|---|
| 54 | #include <iostream>
|
|---|
| 55 |
|
|---|
| 56 | #include "CodePatterns/Log.hpp"
|
|---|
| 57 |
|
|---|
| 58 | using namespace boost::python;
|
|---|
| 59 |
|
|---|
| 60 | void fflush_stdout() { fflush(stdout); }
|
|---|
| 61 | void disable_stdout_buffering() { setbuf(stdout, NULL); }
|
|---|
| 62 | void fflush_stderr() { fflush(stderr); }
|
|---|
| 63 | void disable_stderr_buffering() { setbuf(stderr, NULL); }
|
|---|
| 64 |
|
|---|
| 65 | void log_to_fileno(int fileno) {
|
|---|
| 66 | __gnu_cxx::stdio_filebuf<char> filebuf(fileno, std::ios::in);
|
|---|
| 67 | std::ostream *out = new std::ostream(&filebuf);
|
|---|
| 68 | logger::setOutputStream(out);
|
|---|
| 69 | errorLogger::setOutputStream(out);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | BOOST_PYTHON_MODULE(pyMoleCuilder_capture)
|
|---|
| 73 | {
|
|---|
| 74 | def("fflush_stderr", fflush_stderr, "flushing stderr buffer");
|
|---|
| 75 | def("fflush_stdout", fflush_stdout, "flushing stdout buffer");
|
|---|
| 76 | def("disable_stderr_buffering", disable_stderr_buffering,
|
|---|
| 77 | "disabling the error buffer");
|
|---|
| 78 | def("disable_stdout_buffering", disable_stdout_buffering,
|
|---|
| 79 | "disabling the output buffer");
|
|---|
| 80 | def("log_to_fileno", log_to_fileno, "divert log to a given file handle number");
|
|---|
| 81 | }
|
|---|