summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbin/taler-log-adapter26
1 files changed, 22 insertions, 4 deletions
diff --git a/bin/taler-log-adapter b/bin/taler-log-adapter
index 4d28c1a..01faf73 100755
--- a/bin/taler-log-adapter
+++ b/bin/taler-log-adapter
@@ -15,6 +15,13 @@
#
# @author Florian Dold
+"""
+Wrapper for programs that log to stderr.
+
+Redirects logs to a file specified by a path with strfmt-style placeholders
+in them.
+"""
+
from subprocess import Popen, PIPE
import sys
import os
@@ -25,22 +32,33 @@ import time
def handler(signum, frame):
if p:
os.kill(p.pid, signal.SIGINT)
-p = None
+
+def touchp(path):
+ dir = os.path.dirname(path)
+ if dir:
+ os.makedirs(dir, exist_ok=True)
+
if len(sys.argv) < 3:
print("Usage: {} logfile prog_and_args...".format(sys.argv[0]), file=sys.stderr)
sys.exit(-1)
+
+p = None
signal.signal(signal.SIGINT, handler)
p = Popen(sys.argv[2:], stderr=PIPE, shell=False)
+
log = sys.argv[1]
-dir = os.path.dirname(log)
-if dir:
- os.makedirs(os.path.dirname(log), exist_ok=True)
+last_name = None
+
while p.poll() is None:
full_name = time.strftime(log)
+ if full_name != last_name:
+ touchp(full_name)
+ last_name = full_name
last_read = p.stderr.readline()
if last_read == '':
break
with open(full_name, "ab") as f:
f.write(last_read)
+
status = p.wait()
sys.exit(status)