Skip to content

Commit

Permalink
Wrap libc opening.
Browse files Browse the repository at this point in the history
  • Loading branch information
tskisner committed Nov 23, 2020
1 parent 724c8ab commit b644bfa
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions py/desiutil/redirect.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,30 @@
# C file descriptors for stderr and stdout, used in redirection
# context manager.

libc = ctypes.CDLL(None)
c_stdout = None
c_stderr = None
try:
# Linux systems
c_stdout = ctypes.c_void_p.in_dll(libc, 'stdout')
c_stderr = ctypes.c_void_p.in_dll(libc, 'stderr')
except:
try:
# Darwin
c_stdout = ctypes.c_void_p.in_dll(libc, '__stdoutp')
c_stderr = ctypes.c_void_p.in_dll(libc, '__stdoutp')
except:
# Neither!
pass
_libc = None
_c_stdout = None
_c_stderr = None

def get_libc():
"""Helper function to import libc once."""
global _libc
global _c_stdout
global _c_stderr
if _libc is None:
_libc = ctypes.CDLL(None)
try:
# Linux systems
_c_stdout = ctypes.c_void_p.in_dll(_libc, 'stdout')
_c_stderr = ctypes.c_void_p.in_dll(_libc, 'stderr')
except:
try:
# Darwin
_c_stdout = ctypes.c_void_p.in_dll(_libc, '__stdoutp')
_c_stderr = ctypes.c_void_p.in_dll(_libc, '__stdoutp')
except:
# Neither!
pass
return (_libc, _c_stdout, _c_stderr)

@contextmanager
def stdouterr_redirected(to=None, comm=None):
Expand All @@ -62,6 +71,8 @@ def stdouterr_redirected(to=None, comm=None):
comm (mpi4py.MPI.Comm): The optional MPI communicator.
"""
libc, c_stdout, c_stderr = get_libc()

nproc = 1
rank = 0
MPI = None
Expand Down Expand Up @@ -158,7 +169,7 @@ def _close_redirect(handle):

if rank == 0:
log = get_logger()
log.info("Begin log redirection to {} at {}".format(to, time.asctime()))
log.info("Begin log redirection to %s at %s", to, time.asctime())

# Try to open the redirected file.

Expand All @@ -173,7 +184,7 @@ def _close_redirect(handle):
except:
log = get_logger()
log.error(
"Failed to open redirection file %s at %s", pto, time.asctime())
"Failed to open redirection file %s at %s", pto, time.asctime()
)
fail_open = 1

Expand All @@ -185,7 +196,7 @@ def _close_redirect(handle):
if rank == 0:
log = get_logger()
log.error(
"Failed to start redirect to %s at %s", to, time.asctime())
"Failed to start redirect to %s at %s", to, time.asctime()
)

_close_redirect(file)
Expand Down Expand Up @@ -229,7 +240,7 @@ def _close_redirect(handle):

if rank == 0:
log = get_logger()
log.info("End log redirection to %s at %s", to, time.asctime()))
log.info("End log redirection to %s at %s", to, time.asctime())

# flush python handles for good measure
sys.stdout.flush()
Expand Down

0 comments on commit b644bfa

Please sign in to comment.