简体   繁体   中英

Capture c++ shared library log entries with ctypes

I have a cplusplus shared library, with ac interface, that writes log entries in stdout. I'm using it in a python application using the ctypes library. The python application uses logging library to write log entries.

What I need to do is to capture the stdout entries of the shared library to write the log entries with the logging module. In other words, I want to redirect the stdout entries of the c library to the logging module, so I can use logging to write to files and console using its handlers.

I found that is possible to capture the stdout ( see this SO question ), but I can access it only when the c module call ends, and hence it is useless for logging. I want a none-blocking way to access the stdout entries.

A minimum example is as follows.

module.cpp (compiled with g++ -fPIC -shared module.cpp -o module.so )

#include <unistd.h>
#include <iostream>

using namespace std;

extern "C" int callme()
{
  cout<<"Hello world\n";
  sleep(2);
  cout<<"Some words\n";
  sleep(2);
  cout<<"Goodby world\n";
  return 0;
}

The python app that calls it:

import ctypes as ct
import logging

format='%(asctime)s - %(levelname)s - %(message)s', level=logging.DEBUG
logging.basicConfig(format=format)

logging.debug('This logging modules works like a charm!')

mymodule = ct.CDLL('./module.so')
mymodule.callme()

logging.info('I want to capture the shared library log entries')
logging.warning('Can I?')

This produces:

2016-02-04 16:16:35,976 - DEBUG - This logging modules works like a charm!
Hello world
Some words
Goodby world
2016-02-04 16:16:39,979 - INFO - I want to capture the shared library log entries
2016-02-04 16:16:39,979 - WARNING - Can I?

I have access to the c++ library, so a solution that needs modifications in the library are also welcome.

You should be able to modify the code from the linked answer by reading from the pipe in a thread while the C module call is running. The following should work, although I haven't tested it with a long-running module call:

def redirected_printed_output(module_call):
    # the pipe would fail for some reason if I didn't write to stdout at some point
    # so I write a space, then backspace (will show as empty in a normal terminal)
    sys.stdout.write(' \b')
    pipe_out, pipe_in = os.pipe()
    # save a copy of stdout
    stdout = os.dup(1)
    # replace stdout with our write pipe
    os.dup2(pipe_in, 1)

    # check if we have more to read from the pipe
    def more_data():
        r, _, _ = select.select([pipe_out], [], [], 0)
        return bool(r)

    # read the pipe, writing to (former) stdout
    def write_pipe_to_stdout():
        while more_data():
            os.write(stdout, os.read(pipe_out, 1024))

    done = False
    def read_loop():
        # rewrite the pipe out to stdout in a loop while the call is running
        while not done:
            write_pipe_to_stdout()
        # Finish the remnants
        write_pipe_to_stdout()

    t = threading.Thread(target=read_loop)
    t.start()

    module_call()

    done = True
    t.join() # wait for the thread to finish

    # put stdout back in place 
    os.dup2(stdout, 1)

I tested it as follows (OSX):

import ctypes
libc = ctypes.CDLL('libc.dylib')
def zomg():
    for i in xrange(5):
        libc.printf('libc stdout: %d\n', i)
        time.sleep(1)

redirected_printed_output(zomg)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM