Permalink
Browse files

Move StreamLogger to stoneridge module

It's now used in multiple places, so we need to move it somewhere
shareable.
  • Loading branch information...
1 parent 21f941c commit f57830e88b7b4abb470f3b652a06879aeef9208c @nwgh nwgh committed Mar 6, 2013
Showing with 30 additions and 17 deletions.
  1. +29 −0 stoneridge.py
  2. +1 −17 windows/srwebmq.py
View
@@ -123,6 +123,35 @@ def __init__(self, args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
**kwargs)
+class StreamLogger(object):
+ """Redirect a stream to a logger
+ """
+ def __init__(self, logger):
+ self.logger = logger
+
+ def write(self, buf):
+ for line in buf.rstrip().splitlines():
+ self.logger.log(logging.DEBUG, line.rstrip())
+
+ @staticmethod
+ def bottle_inject():
+ """Do some nasty hackery to make sure everything bottle prints goes to
+ our log, too.
+ """
+ # We do the import here, because we don't want to import bottle into
+ # the stoneridge namespace unless the process is already using bottle,
+ # which will be evident by the fact that it's asking us to inject this
+ # stream logger into bottle!
+ import bottle
+ streamlogger = StreamLogger(logging.getLogger())
+
+ # Redirecting sys.stdout and sys.stderr is ok, because anything that
+ # calls this is a daemon process that shouldn't be printing anything
+ # to the console, anyway.
+ sys.stdout = sys.stderr = streamlogger
+ bottle._stdout = bottle._stderr = streamlogger.write
+
+
_cp = None
_srconf = None
_runconf = None
View
@@ -6,25 +6,13 @@
import bottle
import logging
import sqlite3
-import sys
import stoneridge
conn = None # Persistent connection for sqlite file
-class StreamLogger(object):
- """Redirect a stream to a logger
- """
- def __init__(self, logger):
- self.logger = logger
-
- def write(self, buf):
- for line in buf.rstrip().splitlines():
- self.logger.log(logging.DEBUG, line.rstrip())
-
-
@bottle.route('/get_next')
def get_next():
cur = conn.cursor()
@@ -49,11 +37,7 @@ def daemon():
dbfile = stoneridge.get_config('mqproxy', 'db')
conn = sqlite3.connect(dbfile)
- # Do some nasty hackery to make sure everything bottle prints goes to our
- # log, too
- streamlogger = StreamLogger(logging.getLogger())
- sys.stdout = sys.stderr = streamlogger
- bottle._stdout = bottle._stderr = streamlogger.write
+ stoneridge.StreamLogger.bottle_inject()
port = stoneridge.get_config_int('mqproxy', 'port')
bottle.run(host='0.0.0.0', port=port)

0 comments on commit f57830e

Please sign in to comment.