- Notifications
You must be signed in to change notification settings - Fork1
Redirects text send to sys.stdout/stderr from Python to a user defined C/C++ function.
License
TinyTinni/PyLogHook
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Redirects text send to sys.stdout/stderr from Python to a user defined C/C++ function.
When you write your own interpreter for embedded python, you are maybe interested inthe output given by the Python interpreter.This header-only project provvides the functionality to passthe output to any given function.
- redirect stdout/stderr to a plain function, lambda or Functor
- supports GUI programs without default console
- doesn't add any function to pythons global namespace
- doesn't add any additional python module
If you use Boost, define TYTI_PYLOGHOOK_USE_BOOST before include PyLogHook.h.
It is header only. Just addPyLogHook.h to your project which should already link against boost.python or pybind11.
Call one of the functions in namespacetyti::pylog
.
// in case of using Boost.Python#defineTYTI_PYLOGHOOK_USE_BOOST//------------------------------#include<PyLogHook.h>voidmy_stdout(constchar* w){ std::cout << w;}voidmy_stderr(constchar* w){ std::cerr << w;}...//somewhere e.g. in your main function, after Py_Initialize()tyti::pylog::redirect_stdout(my_stdout);tyti::pylog::redirect_stderr(my_stderr);
Before calling one of the functions, make sure
- python is initalized (
Py_Initialize()
) - you hold the GIL, if multithreading is activated
If anything went wrong, a Python exception was raised.You can check, if a there is a pending exception viaPyErr_Occurred()
(Seetests).
MIT License © Matthias Möller. Made with ♥ in Germany.