Skip to content

Instantly share code, notes, and snippets.

@adutra
Created May 29, 2012 09:39
Show Gist options
  • Save adutra/2823565 to your computer and use it in GitHub Desktop.
Save adutra/2823565 to your computer and use it in GitHub Desktop.
Changes to enable to plug a log framework
/**
* Copyright (C) 2011
* Michael Mosmann <[email protected]>
* Martin Jöhren <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.flapdoodle.embedmongo.io;
public class NamedOutputStreamProcessor implements IStreamProcessor {
private final IStreamProcessor _destination;
private final String _name;
public NamedOutputStreamProcessor(String name, IStreamProcessor destination) {
_name = name;
_destination = destination;
}
@Override
public void process(String block) {
_destination.process(_name + " " + block);
// int idx=block.indexOf('\n');
// if (idx!=-1) {
// _destination.process(block.substring(0,idx+1));
// _destination.process(_name+" ");
// _destination.process(block.substring(idx+1));
// } else {
// _destination.process(block);
// }
}
@Override
public void onProcessed() {
_destination.onProcessed();
}
}
/**
* Copyright (C) 2011
* Michael Mosmann <[email protected]>
* Martin Jöhren <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.flapdoodle.embedmongo.io;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
public class ReaderProcessor extends Thread {
private final BufferedReader _reader;
private final IStreamProcessor _streamProcessor;
protected ReaderProcessor(Reader reader, IStreamProcessor streamProcessor) {
_reader = reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader);
_streamProcessor = streamProcessor;
setDaemon(true);
start();
}
@Override
public void run() {
try {
// int read;
// char[] buf = new char[512];
// while ((read = _reader.read(buf)) != -1) {
// _streamProcessor.process(new String(buf, 0, read));
// }
String line;
while ((line = _reader.readLine()) != null) {
_streamProcessor.process(line);
}
} catch (IOException iox) {
// _logger.log(Level.SEVERE,"out",iox);
}
_streamProcessor.onProcessed();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment