-
Notifications
You must be signed in to change notification settings - Fork 6
/
py_logger.py
executable file
·46 lines (36 loc) · 1.03 KB
/
py_logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python
"""
py_logger
Write a log of what's what bytes are being written by a command
Use it from the command line: `python py_logger.py ipython`
Use it from another python script:
`from py_logger import run`
`run(['ipython'])
"""
from __future__ import unicode_literals
import os
import sys
import pity
def master_read(fd):
"""Read the output of a terminal writing a log in the middle.
Args:
fd: File descriptor being read.
"""
data = os.read(fd, 1024)
log = open('output.log', 'a')
log.write(data)
return data
def run(argv):
"""Spawn a process with master read writing a log file in between.
Args:
argv: Additional arguments after `python py_logger` command
"""
pity.spawn(argv,
master_read=master_read,
handle_window_size=True)
if __name__ == '__main__':
if len(sys.argv) == 1:
print("To record what bytes ipython writes to stdout/stderr:")
print("python py_logger.py ipython")
else:
run(sys.argv[1:])