Author: sebb
Date: Thu Oct 15 13:43:15 2015
New Revision: 1708820

URL: http://svn.apache.org/viewvc?rev=1708820&view=rev
Log:
Experimental file filter to tee stderr to stdout
Intended for use with cronjobs where we want stderr copied to the log as well

Added:
    comdev/projects.apache.org/scripts/cronjobs/errtee.py   (with props)

Added: comdev/projects.apache.org/scripts/cronjobs/errtee.py
URL: 
http://svn.apache.org/viewvc/comdev/projects.apache.org/scripts/cronjobs/errtee.py?rev=1708820&view=auto
==============================================================================
--- comdev/projects.apache.org/scripts/cronjobs/errtee.py (added)
+++ comdev/projects.apache.org/scripts/cronjobs/errtee.py Thu Oct 15 13:43:15 
2015
@@ -0,0 +1,36 @@
+"""
+    Class to copy stderr to stdout.
+
+    This is intended for use in cronjobs,
+    where it is useful to have both stderr and stdout in the log file
+    but still have stderr so that the mailer gets any errors
+    The main script should be run unbuffered (-u)
+
+    Unfortunately Python does not seem to offer the equivalent of:
+    perl -[mM][-]module  execute "use/no module..." before executing program
+    ruby -rlibrary       require the library before executing your script
+    lua  -l name         require library 'name'
+    Thus the module has to be explicitly imported by the user script,
+    and the user must defing the 'ERRTEE' environment variable to enable it
+  
+"""
+import sys
+import os
+
+class ErrTee(object):
+    def write(self, buf):
+        sys.__stderr__.write(buf)
+        sys.stdout.write(buf)
+    def flush(self):
+        sys.__stderr__.flush()
+
+# only enable the override if it is needed
+if 'ERRTEE' in os.environ:
+    sys.stderr=ErrTee()
+
+if __name__ == '__main__': # simple test
+    print("STDOUT1")
+    print("STDERR2", file=sys.stderr)
+    sys.stderr=ErrTee() # enable for testing
+    print("STDERR3", file=sys.stderr)
+    raise Exception("STDERR4")
\ No newline at end of file

Propchange: comdev/projects.apache.org/scripts/cronjobs/errtee.py
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to