[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/winservice/service.py add output redirection for windows services

Brian Lloyd brian at zope.com
Fri Jul 8 11:46:16 EDT 2005


Log message for revision 31035:
  add output redirection for windows services

Changed:
  U   Zope3/trunk/src/zope/app/winservice/service.py

-=-
Modified: Zope3/trunk/src/zope/app/winservice/service.py
===================================================================
--- Zope3/trunk/src/zope/app/winservice/service.py	2005-07-08 12:07:48 UTC (rev 31034)
+++ Zope3/trunk/src/zope/app/winservice/service.py	2005-07-08 15:46:15 UTC (rev 31035)
@@ -16,9 +16,7 @@
 Windows NT/2K service installer/controller for Zope/ZEO/ZRS instances.
 """
 
-import time
-import os
-
+import sys, os, time
 import pywintypes
 import win32serviceutil
 import win32service
@@ -58,7 +56,14 @@
         # Create an event which we will use to wait on.
         # The "service stop" request will set this event.
         self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
+        self.redirectOutput()
 
+    def redirectOutput(self):
+        sys.stdout.close()
+        sys.stderr.close()
+        sys.stdout = NullOutput()
+        sys.stderr = NullOutput()
+
     def SvcStop(self):
         # Before we do anything, tell the SCM we are starting the stop process.
         self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
@@ -189,5 +194,35 @@
             servicemanager.PYS_SERVICE_STOPPED,
             (self._svc_name_, ' (%s) ' % self._svc_display_name_))
 
+
+class NullOutput:
+    """A stdout / stderr replacement that discards everything."""
+
+    def noop(self, *args, **kw):
+        pass
+
+    write = writelines = close = seek = flush = truncate = noop
+
+    def __iter__(self):
+        return self
+
+    def next(self):
+        raise StopIteration
+
+    def isatty(self):
+        return False
+
+    def tell(self):
+        return 0
+
+    def read(self, *args, **kw):
+        return ''
+
+    readline = read
+
+    def readlines(self, *args, **kw):
+        return []
+   
+
 if __name__=='__main__':
     win32serviceutil.HandleCommandLine(Service)



More information about the Zope3-Checkins mailing list