-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewire python<->java logging to be generally friendlier to python
Fixes #2734
- Loading branch information
Showing
15 changed files
with
243 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
Integrations/src/main/java/io/deephaven/integrations/python/PyLogOutputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package io.deephaven.integrations.python; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.jpy.PyObject; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Simple output stream that redirects all writes to a python io.TextIOBase type. | ||
*/ | ||
public class PyLogOutputStream extends OutputStream { | ||
private final Supplier<PyObject> rawIoBaseSupplier; | ||
|
||
public PyLogOutputStream(Supplier<PyObject> rawIoBaseSupplier) { | ||
this.rawIoBaseSupplier = rawIoBaseSupplier; | ||
} | ||
|
||
@Override | ||
public void write(int i) throws IOException { | ||
write(new byte[] {(byte) i}); | ||
} | ||
|
||
@Override | ||
public void write(@NotNull byte[] b) throws IOException { | ||
//noinspection PrimitiveArrayArgumentToVarargsMethod | ||
rawIoBaseSupplier.get().callMethod("write", new String(b)); | ||
} | ||
|
||
@Override | ||
public void write(@NotNull byte[] b, int off, int len) throws IOException { | ||
byte[] buffer = new byte[len]; | ||
System.arraycopy(b, off, buffer, 0, len); | ||
write(buffer); | ||
} | ||
|
||
@Override | ||
public void flush() throws IOException { | ||
rawIoBaseSupplier.get().callMethod("flush"); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
rawIoBaseSupplier.get().callMethod("close"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 0 additions & 66 deletions
66
Integrations/src/main/java/io/deephaven/integrations/python/PythonLogAdapter.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...ded-server/java-runtime/src/main/java/io/deephaven/python/server/EmbeddedPyLogModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.deephaven.python.server; | ||
|
||
import dagger.Module; | ||
import dagger.Provides; | ||
import dagger.multibindings.ElementsIntoSet; | ||
import io.deephaven.base.system.StandardStreamState; | ||
import io.deephaven.internal.log.InitSink; | ||
import io.deephaven.internal.log.LoggerFactory; | ||
import io.deephaven.io.log.LogSink; | ||
import io.deephaven.io.logger.LogBuffer; | ||
import io.deephaven.io.logger.LogBufferGlobal; | ||
import io.deephaven.server.log.LogModule; | ||
|
||
import javax.inject.Singleton; | ||
import java.util.Collections; | ||
import java.util.ServiceLoader; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.StreamSupport; | ||
|
||
@Module | ||
public interface EmbeddedPyLogModule { | ||
|
||
@Provides | ||
static LogBuffer providesLogBuffer() { | ||
return LogBufferGlobal.getInstance().orElseThrow(() -> new RuntimeException("No global LogBuffer found")); | ||
} | ||
|
||
@Provides | ||
static LogSink providesLogSink() { | ||
// In contract, this should be a singleton - see LogInit#checkLogSinkIsSingleton() | ||
return LoggerFactory.getLogger(LogModule.class).getSink(); | ||
} | ||
|
||
@Provides | ||
@ElementsIntoSet | ||
static Set<InitSink> providesLoggerSinkSetups() { | ||
return StreamSupport | ||
.stream(ServiceLoader.load(InitSink.class).spliterator(), false) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
static StandardStreamState providesStandardStreamState() { | ||
return new StandardStreamState(Collections.emptySet()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import io | ||
|
||
|
||
class TeeStream(io.TextIOBase): | ||
""" | ||
TextIOBase subclass that splits output between a delegate instance and a set of lambdas. Can delegate some calls to | ||
the provided stream but actually write only to the lambdas. | ||
Useful to adapt any existing file-like object (such as sys.stdout or sys.stderr), but actually let some other | ||
processing take place before writing. | ||
""" | ||
|
||
encoding = 'UTF-8' | ||
closed = False | ||
|
||
@staticmethod | ||
def split(py_stream, java_stream): | ||
return TeeStream( | ||
py_stream, | ||
True, | ||
lambda t: java_stream.write(bytes(t, py_stream.encoding)), | ||
lambda: java_stream.flush(), | ||
lambda: java_stream.close() | ||
) | ||
|
||
@staticmethod | ||
def redirect(py_stream, java_stream): | ||
if hasattr(py_stream, "encoding"): | ||
encoding = py_stream.encoding | ||
else: | ||
encoding = 'UTF-8' | ||
return TeeStream( | ||
py_stream, | ||
True, | ||
lambda t: java_stream.write(bytes(t, encoding)), | ||
lambda: java_stream.flush(), | ||
lambda: java_stream.close() | ||
) | ||
|
||
def __init__(self, orig_stream, should_write_to_orig_stream, write_func, flush_func, close_func): | ||
""" | ||
Creates a new TeeStream to let output be written from out place, but be sent to | ||
multiple places. | ||
Ideally, the stream would be passed as more funcs, but we have to manage correctly | ||
propagating certain non-java details like encoding and isatty. | ||
:param orig_stream: the underlying python stream to use as a prototype | ||
:param should_write_to_orig_stream: True to delegate writes to the original stream, False to only write to the | ||
lambdas that follow | ||
:param write_func: a function to call when data should be written | ||
:param flush_func: a function to call when data should be flushed | ||
:param close_func: a function to call when the stream should be closed | ||
""" | ||
self._stream = orig_stream | ||
self.should_write_to_orig_stream = should_write_to_orig_stream | ||
self.write_func = write_func | ||
self.flush_func = flush_func | ||
self.close_func = close_func | ||
|
||
if hasattr(orig_stream, 'encoding') and orig_stream.encoding is not None: | ||
# Read this in the constructor, since encoding is read as an attr, not a method | ||
self.encoding = orig_stream.encoding | ||
|
||
def isatty(self): | ||
if not hasattr(self._stream, "isatty"): | ||
return False | ||
return self._stream.isatty() | ||
|
||
def fileno(self): | ||
if not hasattr(self._stream, "fileno"): | ||
return None | ||
return self._stream.fileno() | ||
|
||
def write(self, string): | ||
self.write_func(string) | ||
if self.should_write_to_orig_stream: | ||
self._stream.write(string) | ||
|
||
def flush(self): | ||
self.flush_func() | ||
if self.should_write_to_orig_stream: | ||
self._stream.flush() | ||
|
||
def close(self): | ||
self.close_func() | ||
self.closed = True | ||
if self.should_write_to_orig_stream: | ||
self._stream.close() | ||
|
Oops, something went wrong.