Giter VIP home page Giter VIP logo

pyloghook's Introduction

LogHook for C++ Embedded Python

CMake
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 in the output given by the Python interpreter. This header-only project provvides the functionality to pass the output to any given function.

Features

  • 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

Requirements

If you use Boost, define TYTI_PYLOGHOOK_USE_BOOST before include PyLogHook.h.

Install

It is header only. Just add PyLogHook.h to your project which should already link against boost.python or pybind11.

How-To

Call one of the functions in namespace tyti::pylog.

// in case of using Boost.Python
#define TYTI_PYLOGHOOK_USE_BOOST 
//------------------------------

#include <PyLogHook.h>
void my_stdout(const char* w)
{
    std::cout << w;
}

void my_stderr(const char* 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);

Preconditions

Before calling one of the functions, make sure

  • python is initalized (Py_Initialize())
  • you hold the GIL, if multithreading is activated

Exceptions

If anything went wrong, a Python exception was raised. You can check, if a there is a pending exception via PyErr_Occurred() (See tests).

License

MIT License © Matthias Möller. Made with ♥ in Germany.

pyloghook's People

Contributors

tinytinni avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

s117

pyloghook's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.