Skip to content

Instantly share code, notes, and snippets.

@klauer
Created May 10, 2023 23:16
Show Gist options
  • Save klauer/3f3c6755a6c6f7307a00d23f9c87f408 to your computer and use it in GitHub Desktop.
Save klauer/3f3c6755a6c6f7307a00d23f9c87f408 to your computer and use it in GitHub Desktop.
wurlitzer for capturing C-level stdout/stderr
import ctypes
import io
import os
from wurlitzer import STDOUT, pipes, sys_pipes
import ophyd
def test():
sig = ophyd.EpicsSignal("simple:A")
sig.wait_for_connection()
print("value", sig.get())
sig.destroy()
out = io.StringIO()
libc = ctypes.CDLL(None)
with sys_pipes():
with pipes(stdout=out, stderr=STDOUT):
test()
print("foobar")
print(12)
libc.puts(b"this comes from C")
os.system("echo and this is from echo")
print('Got combined stdout/stderr: "{0}"'.format(out.getvalue()))
# start two IOCs
$ python -m caproto.ioc_examples.simple --list-pvs &
$ python -m caproto.ioc_examples.simple --list-pvs &
$ python cap.py
Got combined stdout/stderr: "CA.Client.Exception...............................................
Warning: "Identical process variable names on multiple servers"
Context: "Channel: "simple:A", Connecting to: 192.168.2.110:53494, Ignored: pc98125.home:5064"
Source File: ../cac.cpp line 1320
Current Time: Wed May 10 2023 16:14:44.793322496
..................................................................
value 1
foobar
12
this comes from C
and this is from echo
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment