Bureau log redirector (from msoy)
git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5409 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -33,7 +33,6 @@ import com.google.inject.Singleton;
|
||||
|
||||
import com.samskivert.util.Interval;
|
||||
import com.samskivert.util.Invoker;
|
||||
import com.samskivert.util.ProcessLogger;
|
||||
import com.samskivert.util.RunQueue;
|
||||
import com.samskivert.util.StringUtil;
|
||||
|
||||
@@ -47,6 +46,7 @@ import com.threerings.presents.server.PresentsClient;
|
||||
import com.threerings.bureau.data.AgentObject;
|
||||
import com.threerings.bureau.data.BureauCodes;
|
||||
import com.threerings.bureau.data.BureauCredentials;
|
||||
import com.threerings.bureau.util.BureauLogRedirector;
|
||||
|
||||
import static com.threerings.bureau.Log.log;
|
||||
|
||||
@@ -204,7 +204,7 @@ public class BureauRegistry
|
||||
builder.redirectErrorStream(true);
|
||||
Process process = builder.start();
|
||||
// log the output of the process and prefix with bureau id
|
||||
ProcessLogger.copyMergedOutput(log, bureauId, process);
|
||||
new BureauLogRedirector(bureauId, process.getInputStream());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.threerings.bureau.util;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import com.samskivert.io.StreamUtil;
|
||||
import com.samskivert.util.Logger;
|
||||
|
||||
import static com.threerings.bureau.Log.log;
|
||||
|
||||
/**
|
||||
* Captures the output of a bureau and redirects it into a single logger instance using a thread
|
||||
* name equal to the bureau id. The {@link Logger} instance is the one for this class. The intent
|
||||
* is that log4j will be configured to use %t (thread name) to embed the bureau id.
|
||||
*/
|
||||
public class BureauLogRedirector
|
||||
{
|
||||
/**
|
||||
* Creates a new redirector.
|
||||
* @param bureauId the id of the bureau being redirected - this will become the thread name
|
||||
* @param input the stream that is the output of the bureau process
|
||||
*/
|
||||
public BureauLogRedirector (String bureauId, InputStream input)
|
||||
{
|
||||
_bureauId = bureauId;
|
||||
_reader = new BufferedReader(new InputStreamReader(input));
|
||||
Thread thread = new Thread(bureauId) {
|
||||
public void run () {
|
||||
copyLoop();
|
||||
}};
|
||||
thread.setDaemon(true);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
protected void copyLoop ()
|
||||
{
|
||||
String line;
|
||||
try {
|
||||
while ((line = _reader.readLine()) != null) {
|
||||
// this should get prefixed by the thread name
|
||||
_target.info(line);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warning("Failed to read bureau output", "bureauId", _bureauId, e);
|
||||
} finally {
|
||||
StreamUtil.close(_reader);
|
||||
}
|
||||
}
|
||||
|
||||
protected String _bureauId;
|
||||
protected BufferedReader _reader;
|
||||
|
||||
protected static Logger _target = Logger.getLogger(BureauLogRedirector.class);
|
||||
}
|
||||
Reference in New Issue
Block a user