-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog
executable file
·43 lines (30 loc) · 1.03 KB
/
log
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
#!/usr/bin/python -u
"""
Dead Simple Logger (C) 2018 Public Domain :D
Usage: log command args
Runs command as usual but saves a copy of the command and its (standard) output
to $PWD/command.date.time
"""
import sys
from time import ctime, time
from re import sub
from subprocess import PIPE, Popen, call
from os.path import basename
if not sys.argv[1:]: # no arguments
print "usage: {} program args...".format(sys.argv[0])
sys.exit(1)
outfn = "_".join([basename(sys.argv[1])] + sys.argv[2:])
outfn = "{}.{}.txt".format(sub(r"[^\w\-\.]+", "_", outfn), int(time()))
proc = Popen(sys.argv[1:], stdout=PIPE, stdin = sys.stdin, stderr = sys.stderr)
print "# logging to:", outfn
start = time()
out = open(outfn, "a", 0) # unbuffered
out.write("# {}\n".format(ctime(start)))
out.write("# {}\n".format(" ".join(sys.argv[1:])))
for line in iter(proc.stdout.readline, b''):
print line,
out.write(line)
end = time()
out.write("# {}\n".format(ctime(end)))
out.write("# {} seconds elapsed\n".format(end - start))
sys.exit(proc.returncode)