-
Notifications
You must be signed in to change notification settings - Fork 225
Route Java console logging through System.out instead of native stdout #1825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2b87330
Route Java console logging through System.out instead of native stdout
ramakrishnap-nv 99a33fc
Fix pre-commit findings: copyright year and clang-format alignment
ramakrishnap-nv 2973059
Patch vendored PSLP to respect verbose=false for its infeasible message
ramakrishnap-nv aeeb1a1
Strip trailing whitespace from the PSLP patch file (pre-commit)
ramakrishnap-nv dba0539
Drop the trailing blank context line from the PSLP patch (pre-commit)
ramakrishnap-nv 1956a4c
Pin PSLP past v0.0.11 to the merged fix commit instead of patching
ramakrishnap-nv 469fc5c
Address CodeRabbit review: add console-log-sink test coverage, fix a …
ramakrishnap-nv cbb0a81
Merge remote-tracking branch 'origin/main' into java-console-log-call…
ramakrishnap-nv 1602ef8
Revert PSLP pin to v0.0.11; split into its own PR
ramakrishnap-nv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,78 @@ | ||
| /* clang-format off */ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| /* clang-format on */ | ||
|
|
||
| #include <utilities/logger.hpp> | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| namespace { | ||
|
|
||
| std::vector<std::string>& captured_lines() | ||
| { | ||
| static std::vector<std::string> lines; | ||
| return lines; | ||
| } | ||
|
|
||
| void capturing_callback(int /* level */, const char* message) | ||
| { | ||
| captured_lines().push_back(message); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| // Covers the console-sink override added for language bindings (Java in particular) whose host | ||
| // runtime cannot safely receive a raw write to the native stdout stream -- see | ||
| // cuopt::set_console_log_callback in logger.hpp. | ||
| class console_log_callback_test : public ::testing::Test { | ||
| protected: | ||
| void TearDown() override | ||
| { | ||
| // Every test must leave the override cleared, or a later test (or a later suite entirely, | ||
| // since the callback is process-global) would silently pick up a stale callback. | ||
| cuopt::set_console_log_callback(nullptr); | ||
| captured_lines().clear(); | ||
| } | ||
| }; | ||
|
|
||
| TEST_F(console_log_callback_test, registered_callback_receives_console_output) | ||
| { | ||
| cuopt::set_console_log_callback(&capturing_callback); | ||
| { | ||
| cuopt::init_logger_t guard("", /* log_to_console = */ true); | ||
| CUOPT_LOG_INFO("hello from console_log_callback_test"); | ||
| } | ||
|
|
||
| ASSERT_FALSE(captured_lines().empty()); | ||
| EXPECT_NE(captured_lines().back().find("hello from console_log_callback_test"), | ||
| std::string::npos); | ||
| } | ||
|
|
||
| TEST_F(console_log_callback_test, nullptr_callback_falls_back_to_stdout_without_crashing) | ||
| { | ||
| cuopt::set_console_log_callback(nullptr); | ||
|
|
||
| EXPECT_NO_THROW({ | ||
| cuopt::init_logger_t guard("", /* log_to_console = */ true); | ||
| CUOPT_LOG_INFO("this goes to std::cout, not a callback"); | ||
| }); | ||
| // No callback was registered, so nothing should have been captured through it. | ||
| EXPECT_TRUE(captured_lines().empty()); | ||
| } | ||
|
|
||
| TEST_F(console_log_callback_test, log_to_console_false_suppresses_both_sinks) | ||
| { | ||
| cuopt::set_console_log_callback(&capturing_callback); | ||
| { | ||
| cuopt::init_logger_t guard("", /* log_to_console = */ false); | ||
| CUOPT_LOG_INFO("should not reach either sink"); | ||
| } | ||
|
|
||
| EXPECT_TRUE(captured_lines().empty()); | ||
| } |
24 changes: 24 additions & 0 deletions
24
java/cuopt/src/main/java/com/nvidia/cuopt/mathematicaloptimization/NativeLogSink.java
This file contains hidden or 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,24 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package com.nvidia.cuopt.mathematicaloptimization; | ||
|
|
||
| /** | ||
| * Receives cuOpt's console log lines from native code and writes them through {@link | ||
| * System#out}, rather than the native library writing to the process's stdout stream directly. | ||
| * | ||
| * <p>A direct native write bypasses {@code System.out}, so it is invisible to anything that | ||
| * intercepts or redirects it -- {@link System#setOut}, a logging framework bridge, or Maven | ||
| * Surefire, which uses the forked JVM's stdout as its own communication channel and can | ||
| * misinterpret an unexpected raw write on it as the forked process having crashed. | ||
| * | ||
| * <p>Called from {@code cuopt_jni.cpp}; not part of the public API. | ||
| */ | ||
| final class NativeLogSink { | ||
| private NativeLogSink() {} | ||
|
|
||
| static void onLogLine(String message) { | ||
| System.out.print(message); | ||
| } | ||
| } |
This file contains hidden or 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
54 changes: 54 additions & 0 deletions
54
java/cuopt/src/test/java/com/nvidia/cuopt/mathematicaloptimization/NativeLogSinkTest.java
This file contains hidden or 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,54 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package com.nvidia.cuopt.mathematicaloptimization; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.PrintStream; | ||
| import java.nio.charset.StandardCharsets; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * Verifies that {@link NativeLogSink#onLogLine} writes through {@link System#out} rather than | ||
| * bypassing it -- the whole point of routing native console log lines through this class instead | ||
| * of a direct native write. See {@link NativeLogSink} for why a direct write is unsafe here. | ||
| */ | ||
| final class NativeLogSinkTest { | ||
|
|
||
| private final PrintStream originalOut = System.out; | ||
|
|
||
| @AfterEach | ||
| void restoreSystemOut() { | ||
| System.setOut(originalOut); | ||
| } | ||
|
|
||
| @Test | ||
| void onLogLineWritesThroughSystemOut() { | ||
| ByteArrayOutputStream captured = new ByteArrayOutputStream(); | ||
| System.setOut(new PrintStream(captured, true, StandardCharsets.UTF_8)); | ||
|
|
||
| NativeLogSink.onLogLine("Solving a problem with 1 constraints, 1 variables\n"); | ||
|
|
||
| assertEquals( | ||
| "Solving a problem with 1 constraints, 1 variables\n", | ||
| captured.toString(StandardCharsets.UTF_8)); | ||
| } | ||
|
|
||
| @Test | ||
| void onLogLineReflectsSystemSetOutRedirection() { | ||
| ByteArrayOutputStream first = new ByteArrayOutputStream(); | ||
| System.setOut(new PrintStream(first, true, StandardCharsets.UTF_8)); | ||
| NativeLogSink.onLogLine("first\n"); | ||
|
|
||
| ByteArrayOutputStream second = new ByteArrayOutputStream(); | ||
| System.setOut(new PrintStream(second, true, StandardCharsets.UTF_8)); | ||
| NativeLogSink.onLogLine("second\n"); | ||
|
|
||
| assertEquals("first\n", first.toString(StandardCharsets.UTF_8)); | ||
| assertEquals("second\n", second.toString(StandardCharsets.UTF_8)); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.