Add a pluggable diagnostic output channel#1223
Open
bnaras wants to merge 1 commit into
Open
Conversation
Collaborator
|
please rebase with the dev branch |
Route the library's diagnostic writes through OPENFHE_LOG_ERR / OPENFHE_LOG_OUT (each a std::ostream&) instead of std::cerr / std::cout directly, so an embedding application can redirect OpenFHE's diagnostics without editing call sites. Because the macros expand to std::ostream&, every operator<< overload and manipulator keeps working unchanged. The default sink (std::cerr / std::cout) lives in one translation unit, openfhe_log_default.cpp, compiled only when the new OPENFHE_DEFAULT_LOG_SINK CMake option is ON (the default). With the default sink ON the behavior is byte-for-byte identical to before. Embedders may redirect at runtime via SetOpenFHEErrStream / SetOpenFHEOutStream, or build with -DOPENFHE_DEFAULT_LOG_SINK=OFF and supply their own accessor definitions to keep std::cerr / std::cout out of the compiled archive entirely -- e.g. an R package, where CRAN forbids compiled code that writes to stdout/stderr. Converts the existing std::cerr / std::cout diagnostic sites in core, binfhe, and pke to the new macros. No functional change under the default configuration.
bnaras
force-pushed
the
pr/pluggable-log-io
branch
from
July 19, 2026 14:41
cbe5b3d to
a498cd9
Compare
Author
|
Rebased onto Two notes on what changed in the process:
I verified all eight converted translation units compile and link in a single configuration, and checked the option differentially: with |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
OpenFHE writes diagnostics (warnings, timing dumps, debug notes) directly to
std::cerr/std::coutin a handful ofcore,binfhe, andpkesources. This PR routes those writes through a thin indirection so an embedding application can redirect them, without changing behavior for anyone who doesn't opt in. Such redirection is necessary, for example, for theopenfhe.Rpackage to be hosted on CRAN.It's a no-op by default. With the new
OPENFHE_DEFAULT_LOG_SINKCMake optionON(the default), the diagnostic streams arestd::cerr/std::coutand the output is byte-for-byte identical to before.Motivation
The direct
std::cerr/std::coutwrites don't support two things:std::cerr/std::coutout of the compiled archive. R packages on CRAN may not ship compiled code that writes to stdout/stderr (Writing R Extensions §1.1.3.1); output must go throughREprintf/Rprintf. This isn't possible today without patching the library.Design
src/core/include/utils/openfhe_log.h— declareslbcrypto::OpenFHEErrStream()/OpenFHEOutStream()(returningstd::ostream&) plusSetOpenFHEErrStream()/SetOpenFHEOutStream()for runtime redirection. Two macros,OPENFHE_LOG_ERR/OPENFHE_LOG_OUT, expand to the accessors. Returningstd::ostream&means every existingoperator<<overload and manipulator (std::endl,std::hex, …) keeps working, so call sites change only the stream token.src/core/lib/utils/openfhe_log_default.cpp— the default sink, and the only translation unit in the library that namesstd::cerr/std::cout. Compiled only whenOPENFHE_DEFAULT_LOG_SINKis defined.OPENFHE_DEFAULT_LOG_SINKCMake option, defaultON. ON: the default sink is compiled, diagnostics go tostd::cerr/std::cout, andSet*lets an embedder redirect at runtime. OFF: the default TU compiles to nothing and the embedder supplies its own definitions of the accessors — keepingstd::cerr/std::coutout of the compiled archive.The
std::cerr/std::coutdiagnostic sites incore,binfhe, andpkeare converted to the macros.Behavior / compatibility
OPENFHE_DEFAULT_LOG_SINK=ONreproduces the current behavior exactly.SetOpenFHEErrStream(std::ostream&)/SetOpenFHEOutStream(std::ostream&).-DOPENFHE_DEFAULT_LOG_SINK=OFF+ embedder-provided accessors) is for embedders that must excludestd::cerr/std::coutsymbols from the archive.Testing
Standalone compile/link/symbol check of both configurations:
ON:OPENFHE_LOG_ERR << … << std::endlwrites to stderr,OPENFHE_LOG_OUTto stdout — matchingstd::cerr/std::cout.OFF:nmconfirms the default TU is empty (nocerr/coutsymbols) and converted call sites referenceOpenFHEErrStream()rather thanstd::cerr; the target links when an embedder supplies the accessors.Notes